Commit efe12db
committed
feature #59529 [PropertyInfo] Allow defining accessors and mutators via an attribute (HypeMC)
This PR was merged into the 8.2 branch.
Discussion
----------
[PropertyInfo] Allow defining accessors and mutators via an attribute
| Q | A
| ------------- | ---
| Branch? | 8.2
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues | -
| License | MIT
A continuation of #38515.
`ReflectionExtractor` finds accessors and mutators by convention: it tries each configured prefix against the property name, and asks the inflector whether that name is singular to decide between a whole-collection setter and an element-wise adder. Code that does not follow the convention is invisible to it, and the only way out today is a custom extractor.
`#[WithAccessors]` lets a property name its methods instead:
```php
class Foo
{
#[WithAccessors(getter: 'giveProp', setter: 'receiveProp', adder: 'pushProp', remover: 'popProp')]
private array $prop;
public function giveProp(): array {}
public function receiveProp(array $prop): void {}
public function pushProp(string $prop): void {}
public function popProp(string $prop): void {}
}
```
All four arguments are optional, and at least one must be given. `adder` and `remover` go together: naming one without the other throws. A setter may sit next to an adder and remover, the same way `setTags()` can coexist with `addTag()`/`removeTag()` under the existing conventions.
Naming a method that does not exist throws a `MappingException` when the metadata is read, rather than falling back to discovery and silently resolving something else.
The attribute is read on the declaring class and inherited, so a private property annotated on a parent applies to its children.
Named methods are used as given. The prefix gating, the `is`/`has`/`can` handling and the singular/plural guessing all belong to discovery and are skipped entirely, so results do not shift with the extractor's configured prefixes. When both a setter and an adder are named, the adder determines the type, which yields the element type rather than the bare collection.
Documentation should cover: the four arguments and that at least one is required, the adder/remover pairing rule, that a setter and an adder can be declared together and what each is used for, the `MappingException` on a missing method, inheritance from a parent's property, and that a named method bypasses the naming conventions.
Commits
-------
8459d37 [PropertyInfo] Allow defining accessors and mutators via an attribute14 files changed
+677-15Lines changed: 677 additions & 15 deletions
File tree
Expand file treeCollapse file tree
Open diff view settings
Filter options
- src/Symfony/Component/PropertyInfo
- Attribute
- Exception
- Extractor
- Tests
- Attribute
- Extractor
- Fixtures/WithAccessors
Expand file treeCollapse file tree
Open diff view settings
Collapse file
src/Symfony/Component/PropertyInfo/Attribute/WithAccessors.php
Copy file name to clipboard+32Lines changed: 32 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 | + |
Collapse file
src/Symfony/Component/PropertyInfo/CHANGELOG.md
Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/CHANGELOG.md+5Lines changed: 5 additions & 0 deletions
- Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1 | 1 | |
2 | 2 | |
3 | 3 | |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
4 | 9 | |
5 | 10 | |
6 | 11 | |
|
Collapse file
src/Symfony/Component/PropertyInfo/Exception/ExceptionInterface.php
Copy file name to clipboard+16Lines changed: 16 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 | + |
Collapse file
src/Symfony/Component/PropertyInfo/Exception/LogicException.php
Copy file name to clipboard+16Lines changed: 16 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 | + |
Collapse file
src/Symfony/Component/PropertyInfo/Exception/MappingException.php
Copy file name to clipboard+27Lines changed: 27 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 | + |
Collapse file
src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php
Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php+178-13Lines changed: 178 additions & 13 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
11 | 11 | |
12 | 12 | |
13 | 13 | |
| 14 | + |
| 15 | + |
14 | 16 | |
15 | 17 | |
16 | 18 | |
| ||
85 | 87 | |
86 | 88 | |
87 | 89 | |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
88 | 94 | |
89 | 95 | |
90 | 96 | |
| ||
141 | 147 | |
142 | 148 | |
143 | 149 | |
144 | | - |
| 150 | + |
145 | 151 | |
146 | 152 | |
147 | 153 | |
| ||
162 | 168 | |
163 | 169 | |
164 | 170 | |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
165 | 175 | |
166 | 176 | |
167 | 177 | |
| ||
287 | 297 | |
288 | 298 | |
289 | 299 | |
| 300 | + |
| 301 | + |
| 302 | + |
| 303 | + |
290 | 304 | |
291 | 305 | |
292 | 306 | |
| ||
332 | 346 | |
333 | 347 | |
334 | 348 | |
| 349 | + |
| 350 | + |
| 351 | + |
| 352 | + |
| 353 | + |
| 354 | + |
335 | 355 | |
336 | 356 | |
337 | 357 | |
| ||
375 | 395 | |
376 | 396 | |
377 | 397 | |
| 398 | + |
| 399 | + |
| 400 | + |
| 401 | + |
| 402 | + |
| 403 | + |
| 404 | + |
| 405 | + |
| 406 | + |
| 407 | + |
| 408 | + |
| 409 | + |
| 410 | + |
| 411 | + |
| 412 | + |
| 413 | + |
| 414 | + |
| 415 | + |
| 416 | + |
| 417 | + |
| 418 | + |
| 419 | + |
| 420 | + |
378 | 421 | |
379 | 422 | |
380 | 423 | |
381 | 424 | |
382 | 425 | |
383 | | - |
384 | 426 | |
385 | 427 | |
386 | 428 | |
| ||
396 | 438 | |
397 | 439 | |
398 | 440 | |
399 | | - |
400 | | - |
401 | | - |
402 | | - |
| 441 | + |
| 442 | + |
| 443 | + |
| 444 | + |
| 445 | + |
403 | 446 | |
404 | | - |
405 | | - |
406 | | - |
| 447 | + |
| 448 | + |
| 449 | + |
407 | 450 | |
408 | | - |
409 | | - |
| 451 | + |
| 452 | + |
410 | 453 | |
411 | | - |
| 454 | + |
| 455 | + |
412 | 456 | |
413 | 457 | |
414 | 458 | |
| ||
648 | 692 | |
649 | 693 | |
650 | 694 | |
651 | | - |
| 695 | + |
652 | 696 | |
| 697 | + |
| 698 | + |
| 699 | + |
| 700 | + |
653 | 701 | |
654 | 702 | |
655 | 703 | |
| ||
852 | 900 | |
853 | 901 | |
854 | 902 | |
| 903 | + |
| 904 | + |
| 905 | + |
| 906 | + |
| 907 | + |
| 908 | + |
| 909 | + |
| 910 | + |
| 911 | + |
| 912 | + |
| 913 | + |
| 914 | + |
| 915 | + |
| 916 | + |
| 917 | + |
| 918 | + |
| 919 | + |
| 920 | + |
| 921 | + |
| 922 | + |
| 923 | + |
| 924 | + |
| 925 | + |
| 926 | + |
| 927 | + |
| 928 | + |
| 929 | + |
| 930 | + |
| 931 | + |
| 932 | + |
| 933 | + |
| 934 | + |
| 935 | + |
| 936 | + |
| 937 | + |
| 938 | + |
| 939 | + |
| 940 | + |
| 941 | + |
| 942 | + |
| 943 | + |
| 944 | + |
| 945 | + |
| 946 | + |
| 947 | + |
| 948 | + |
| 949 | + |
| 950 | + |
| 951 | + |
| 952 | + |
| 953 | + |
| 954 | + |
| 955 | + |
| 956 | + |
| 957 | + |
| 958 | + |
| 959 | + |
| 960 | + |
| 961 | + |
| 962 | + |
| 963 | + |
| 964 | + |
| 965 | + |
| 966 | + |
| 967 | + |
| 968 | + |
| 969 | + |
| 970 | + |
| 971 | + |
| 972 | + |
| 973 | + |
| 974 | + |
| 975 | + |
| 976 | + |
| 977 | + |
| 978 | + |
| 979 | + |
| 980 | + |
| 981 | + |
| 982 | + |
| 983 | + |
| 984 | + |
| 985 | + |
| 986 | + |
| 987 | + |
| 988 | + |
| 989 | + |
| 990 | + |
| 991 | + |
| 992 | + |
| 993 | + |
| 994 | + |
| 995 | + |
| 996 | + |
| 997 | + |
| 998 | + |
| 999 | + |
| 1000 | + |
| 1001 | + |
| 1002 | + |
| 1003 | + |
| 1004 | + |
| 1005 | + |
| 1006 | + |
| 1007 | + |
| 1008 | + |
| 1009 | + |
| 1010 | + |
| 1011 | + |
| 1012 | + |
| 1013 | + |
| 1014 | + |
| 1015 | + |
| 1016 | + |
| 1017 | + |
| 1018 | + |
| 1019 | + |
855 | 1020 | |
0 commit comments