[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 $arrayMutatorPrefixes;
private $arrayMutatorPrefixesFirst;
private $arrayMutatorPrefixesLast;
/** /**
* @param string[]|null $mutatorPrefixes * @param string[]|null $mutatorPrefixes
* @param string[]|null $accessorPrefixes * @param string[]|null $accessorPrefixes
@ -72,6 +75,9 @@ class ReflectionExtractor implements PropertyListExtractorInterface, PropertyTyp
$this->mutatorPrefixes = null !== $mutatorPrefixes ? $mutatorPrefixes : self::$defaultMutatorPrefixes; $this->mutatorPrefixes = null !== $mutatorPrefixes ? $mutatorPrefixes : self::$defaultMutatorPrefixes;
$this->accessorPrefixes = null !== $accessorPrefixes ? $accessorPrefixes : self::$defaultAccessorPrefixes; $this->accessorPrefixes = null !== $accessorPrefixes ? $accessorPrefixes : self::$defaultAccessorPrefixes;
$this->arrayMutatorPrefixes = null !== $arrayMutatorPrefixes ? $arrayMutatorPrefixes : self::$defaultArrayMutatorPrefixes; $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); $ucProperty = ucfirst($property);
$ucSingulars = (array) Inflector::singularize($ucProperty); $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]; $names = [$ucProperty];
if (\in_array($prefix, $this->arrayMutatorPrefixes)) { if (\in_array($prefix, $this->arrayMutatorPrefixes)) {
$names = array_merge($names, $ucSingulars); $names = array_merge($names, $ucSingulars);

View File

@ -61,6 +61,7 @@ class ReflectionExtractorTest extends TestCase
'realParent', 'realParent',
'xTotals', 'xTotals',
'YT', 'YT',
'date',
'c', 'c',
'd', 'd',
'e', 'e',
@ -96,6 +97,7 @@ class ReflectionExtractorTest extends TestCase
'foo4', 'foo4',
'foo5', 'foo5',
'files', 'files',
'date',
'c', 'c',
'd', 'd',
'e', 'e',
@ -156,6 +158,8 @@ class ReflectionExtractorTest extends TestCase
['staticSetter', null], ['staticSetter', null],
['self', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy')]], ['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')]], ['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 getYT()
{ {
} }
public function setDate(\DateTime $date)
{
}
public function addDate(\DateTime $date)
{
}
} }