-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Symfony version(s) affected
7.3.1
Description
I'm in the process of migrating my codebase from using getter/setter methods to using property hooks as it greatly improves the conciseness of my code. In some cases, I have properties that are quite a bit more permissive in what they accept from setters than what their getters return. Historically, PropertyInfo extractors like ReflectionExtractor
look for mutators first, and then return their types preferentially over those of getters or the property itself; this has the side effect of making denormalization target the (more permissive) setter's types.
Currently, ReflectionExtractor
doesn't appear to look at the new ReflectionProperty::getSettableType
type at all. This means that the same code written with property hooks instead of getters/setters yields different type results, which may affect downstream code in unexpected ways.
How to reproduce
Without property hooks:
<?php
class Foo {
private string $bar;
public function getBar(): string {
return $this->bar;
}
public function setBar(string|null $value): void {
$this->bar = $value ?? 'Some Default';
}
}
$reflectionExtractor = new Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor();
var_dump($reflectionExtractor->getType(Foo::class, 'bar'));
Yields a NullableType
accepting both string and null.
With property hooks:
<?php
class Foo {
public string $bar {
set (string|null $value) => $value ?? 'Some Default';
}
}
$reflectionExtractor = new Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor();
var_dump($reflectionExtractor->getType(Foo::class, 'bar'));
Yields a BuiltinType
of string with no nullable, as the setter hook is never checked.
Possible Solution
No response
Additional Context
No response