Skip to content

Commit

Permalink
bug #36305 [PropertyInfo][ReflectionExtractor] Check the array mutato…
Browse files Browse the repository at this point in the history
…r 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
  • Loading branch information
nicolas-grekas committed Apr 6, 2020
2 parents 995ef18 + b4df2b9 commit 547c99e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
Expand Up @@ -61,6 +61,9 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
*/
private $arrayMutatorPrefixes;

private $arrayMutatorPrefixesFirst;
private $arrayMutatorPrefixesLast;

/**
* @param string[]|null $mutatorPrefixes
* @param string[]|null $accessorPrefixes
Expand All @@ -72,6 +75,9 @@ public function __construct(array $mutatorPrefixes = null, array $accessorPrefix
$this->mutatorPrefixes = null !== $mutatorPrefixes ? $mutatorPrefixes : self::$defaultMutatorPrefixes;
$this->accessorPrefixes = null !== $accessorPrefixes ? $accessorPrefixes : self::$defaultAccessorPrefixes;
$this->arrayMutatorPrefixes = null !== $arrayMutatorPrefixes ? $arrayMutatorPrefixes : self::$defaultArrayMutatorPrefixes;

$this->arrayMutatorPrefixesFirst = array_merge($this->arrayMutatorPrefixes, array_diff($this->mutatorPrefixes, $this->arrayMutatorPrefixes));
$this->arrayMutatorPrefixesLast = array_reverse($this->arrayMutatorPrefixesFirst);
}

/**
Expand Down Expand Up @@ -330,7 +336,9 @@ private function getMutatorMethod($class, $property)
$ucProperty = ucfirst($property);
$ucSingulars = (array) Inflector::singularize($ucProperty);

foreach ($this->mutatorPrefixes as $prefix) {
$mutatorPrefixes = \in_array($ucProperty, $ucSingulars, true) ? $this->arrayMutatorPrefixesLast : $this->arrayMutatorPrefixesFirst;

foreach ($mutatorPrefixes as $prefix) {
$names = [$ucProperty];
if (\in_array($prefix, $this->arrayMutatorPrefixes)) {
$names = array_merge($names, $ucSingulars);
Expand Down
Expand Up @@ -61,6 +61,7 @@ public function testGetProperties()
'realParent',
'xTotals',
'YT',
'date',
'c',
'd',
'e',
Expand Down Expand Up @@ -96,6 +97,7 @@ public function testGetPropertiesWithCustomPrefixes()
'foo4',
'foo5',
'files',
'date',
'c',
'd',
'e',
Expand Down Expand Up @@ -156,6 +158,8 @@ public function typesProvider()
['staticSetter', null],
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')]],
['realParent', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy')]],
['date', [new Type(Type::BUILTIN_TYPE_OBJECT, false, \DateTime::class)]],
['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))]],
];
}

Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php
Expand Up @@ -190,4 +190,12 @@ public function getXTotals()
public function getYT()
{
}

public function setDate(\DateTime $date)
{
}

public function addDate(\DateTime $date)
{
}
}

0 comments on commit 547c99e

Please sign in to comment.