Commit 0cb81e0
committed
bug symfony#65621 [PropertyInfo] Do not prefer a static named constructor as the property mutator (nicolas-grekas)
This PR was merged into the 6.4 branch.
Discussion
----------
[PropertyInfo] Do not prefer a static named constructor as the property mutator
| Q | A
| ------------- | ---
| Branch? | 6.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Issues | -
| License | MIT
Write-side follow-up to symfony#65619. `getWriteInfo()` selects the jQuery-style bare mutator `foo($value)` (only when `enable_getter_setter_extraction` is on, which PropertyAccess always enables) before the property itself and the magic methods, without checking `isStatic()`. A static method named after the property that accepts one argument, like a named constructor:
```php
class Money
{
public int $value = 0;
public static function value(int $value): self
{
$money = new self();
$money->value = $value;
return $money;
}
}
```
is picked as the mutator for `value`. A static method cannot mutate the instance and PropertyAccess discards its return value, so `$propertyAccessor->setValue($money, 'value', 42)` calls the factory, throws away the result and leaves `$money->value` untouched: the write is silently lost while a writable public property sits right there.
This patch demotes a static method named after the property behind every instance-based candidate: the prefixed mutators, the property itself, `__set()` and `__call()`. It remains a valid last-resort mutator, so a class exposing only such a static method keeps resolving to it and `isWritable()` still reports true; a static mutator can legitimately update static state. Prefixed mutators (`set`, `add`/`remove`) may still resolve to a static method as before.
Resulting write priority for property `foo` with `enable_getter_setter_extraction` on: constructor parameter, adder/remover pair, `setFoo()` and the other prefixed mutators, `foo($value)` if non-static, the `foo` property, `__set()`, `__call()`, and finally `foo($value)` if static.
With this and symfony#65619, the read and write paths agree again on the overlap case: a public property shadowed by a same-named static method is both read from and written to directly.
Commits
-------
4bed9af [PropertyInfo] Do not prefer a static named constructor as the property mutator3 files changed
+87-2Lines changed: 87 additions & 2 deletions
File tree
Expand file treeCollapse file tree
Open diff view settings
Filter options
- src/Symfony/Component/PropertyInfo
- Extractor
- Tests
- Extractor
- Fixtures
Expand file treeCollapse file tree
Open diff view settings
Collapse file
src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php+19-2Lines changed: 19 additions & 2 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
385 | 385 | |
386 | 386 | |
387 | 387 | |
| 388 | + |
| 389 | + |
388 | 390 | |
389 | 391 | |
390 | 392 | |
391 | 393 | |
392 | 394 | |
393 | | - |
| 395 | + |
| 396 | + |
| 397 | + |
| 398 | + |
| 399 | + |
| 400 | + |
394 | 401 | |
395 | 402 | |
396 | 403 | |
| ||
400 | 407 | |
401 | 408 | |
402 | 409 | |
403 | | - |
| 410 | + |
| 411 | + |
| 412 | + |
| 413 | + |
| 414 | + |
404 | 415 | |
405 | 416 | |
406 | 417 | |
| ||
444 | 455 | |
445 | 456 | |
446 | 457 | |
| 458 | + |
| 459 | + |
| 460 | + |
| 461 | + |
| 462 | + |
| 463 | + |
447 | 464 | |
448 | 465 | |
449 | 466 | |
|
Collapse file
src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php+26Lines changed: 26 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
23 | 23 | |
24 | 24 | |
25 | 25 | |
| 26 | + |
26 | 27 | |
27 | 28 | |
28 | 29 | |
| ||
728 | 729 | |
729 | 730 | |
730 | 731 | |
| 732 | + |
| 733 | + |
| 734 | + |
| 735 | + |
| 736 | + |
| 737 | + |
| 738 | + |
| 739 | + |
| 740 | + |
| 741 | + |
| 742 | + |
| 743 | + |
| 744 | + |
| 745 | + |
| 746 | + |
| 747 | + |
| 748 | + |
| 749 | + |
| 750 | + |
| 751 | + |
| 752 | + |
| 753 | + |
| 754 | + |
| 755 | + |
| 756 | + |
731 | 757 | |
732 | 758 | |
733 | 759 | |
|
Collapse file
src/Symfony/Component/PropertyInfo/Tests/Fixtures/DummyWithStaticMutator.php
Copy file name to clipboard+42Lines changed: 42 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
0 commit comments