[PropertyInfo][ReflectionExtractor] Check the array mutator prefixes last when the property is singular

This commit is contained in:
Thomas Calvet 2020-04-01 19:12:29 +02:00
parent 5da141b8d0
commit b4df2b9dff
3 changed files with 21 additions and 1 deletions

View File

@ -61,6 +61,9 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
*/
private $arrayMutatorPrefixes;
private $arrayMutatorPrefixesFirst;
private $arrayMutatorPrefixesLast;
/**
* @param string[]|null $mutatorPrefixes
* @param string[]|null $accessorPrefixes
@ -72,6 +75,9 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
$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);
}
/**
@ -330,7 +336,9 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
$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);

View File

@ -61,6 +61,7 @@ class ReflectionExtractorTest extends TestCase
'realParent',
'xTotals',
'YT',
'date',
'c',
'd',
'e',
@ -96,6 +97,7 @@ class ReflectionExtractorTest extends TestCase
'foo4',
'foo5',
'files',
'date',
'c',
'd',
'e',
@ -156,6 +158,8 @@ class ReflectionExtractorTest extends TestCase
['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))]],
];
}

View File

@ -190,4 +190,12 @@ class Dummy extends ParentDummy
public function getYT()
{
}
public function setDate(\DateTime $date)
{
}
public function addDate(\DateTime $date)
{
}
}