Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 547c99e

Browse filesBrowse files
bug #36305 [PropertyInfo][ReflectionExtractor] Check the array mutator prefixes last when the property is singular (fancyweb)
This PR was merged into the 3.4 branch. Discussion ---------- [PropertyInfo][ReflectionExtractor] Check the array mutator prefixes last when the property is singular | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #36079 | License | MIT | Doc PR | - Check the related tickets that have a very descriptive example. If the property is singular, we should prioritize non array mutator prefixes and do the opposite for plural property. It relies on some guessing but it actually fixes real world scenarios. Commits ------- b4df2b9 [PropertyInfo][ReflectionExtractor] Check the array mutator prefixes last when the property is singular
2 parents 995ef18 + b4df2b9 commit 547c99e
Copy full SHA for 547c99e

File tree

3 files changed

+21
-1
lines changed
Filter options

3 files changed

+21
-1
lines changed

‎src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
6161
*/
6262
private $arrayMutatorPrefixes;
6363

64+
private $arrayMutatorPrefixesFirst;
65+
private $arrayMutatorPrefixesLast;
66+
6467
/**
6568
* @param string[]|null $mutatorPrefixes
6669
* @param string[]|null $accessorPrefixes
@@ -72,6 +75,9 @@ public function __construct(array $mutatorPrefixes = null, array $accessorPrefix
7275
$this->mutatorPrefixes = null !== $mutatorPrefixes ? $mutatorPrefixes : self::$defaultMutatorPrefixes;
7376
$this->accessorPrefixes = null !== $accessorPrefixes ? $accessorPrefixes : self::$defaultAccessorPrefixes;
7477
$this->arrayMutatorPrefixes = null !== $arrayMutatorPrefixes ? $arrayMutatorPrefixes : self::$defaultArrayMutatorPrefixes;
78+
79+
$this->arrayMutatorPrefixesFirst = array_merge($this->arrayMutatorPrefixes, array_diff($this->mutatorPrefixes, $this->arrayMutatorPrefixes));
80+
$this->arrayMutatorPrefixesLast = array_reverse($this->arrayMutatorPrefixesFirst);
7581
}
7682

7783
/**
@@ -330,7 +336,9 @@ private function getMutatorMethod($class, $property)
330336
$ucProperty = ucfirst($property);
331337
$ucSingulars = (array) Inflector::singularize($ucProperty);
332338

333-
foreach ($this->mutatorPrefixes as $prefix) {
339+
$mutatorPrefixes = \in_array($ucProperty, $ucSingulars, true) ? $this->arrayMutatorPrefixesLast : $this->arrayMutatorPrefixesFirst;
340+
341+
foreach ($mutatorPrefixes as $prefix) {
334342
$names = [$ucProperty];
335343
if (\in_array($prefix, $this->arrayMutatorPrefixes)) {
336344
$names = array_merge($names, $ucSingulars);

‎src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function testGetProperties()
6161
'realParent',
6262
'xTotals',
6363
'YT',
64+
'date',
6465
'c',
6566
'd',
6667
'e',
@@ -96,6 +97,7 @@ public function testGetPropertiesWithCustomPrefixes()
9697
'foo4',
9798
'foo5',
9899
'files',
100+
'date',
99101
'c',
100102
'd',
101103
'e',
@@ -156,6 +158,8 @@ public function typesProvider()
156158
['staticSetter', null],
157159
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')]],
158160
['realParent', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy')]],
161+
['date', [new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTime::class)]],
162+
['dates', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTime::class))]],
159163
];
160164
}
161165

‎src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,12 @@ public function getXTotals()
190190
public function getYT()
191191
{
192192
}
193+
194+
public function setDate(\DateTime $date)
195+
{
196+
}
197+
198+
public function addDate(\DateTime $date)
199+
{
200+
}
193201
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.