Skip to content

Commit a9a8d6b

Browse files
committed
[PropertyAccess] added a check if the property name plural. So it uses adder/remover only if the property name is actual plural.
1 parent 9559fe9 commit a9a8d6b

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,17 @@ private function getWriteAccessInfo($class, $property, $value)
706706
$access[self::ACCESS_HAS_PROPERTY] = $reflClass->hasProperty($property);
707707
$camelized = $this->camelize($property);
708708
$singulars = (array) Inflector::singularize($camelized);
709+
$plural = empty(array_intersect([$camelized], $singulars));
710+
711+
if ($plural && \is_array($value) || $value instanceof \Traversable) {
712+
$methods = $this->findAdderAndRemover($reflClass, $singulars);
713+
714+
if (null !== $methods) {
715+
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_ADDER_AND_REMOVER;
716+
$access[self::ACCESS_ADDER] = $methods[0];
717+
$access[self::ACCESS_REMOVER] = $methods[1];
718+
}
719+
}
709720

710721
if (!isset($access[self::ACCESS_TYPE])) {
711722
$setter = 'set'.$camelized;
@@ -728,22 +739,16 @@ private function getWriteAccessInfo($class, $property, $value)
728739
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_MAGIC;
729740
$access[self::ACCESS_NAME] = $setter;
730741
} elseif (null !== $methods = $this->findAdderAndRemover($reflClass, $singulars)) {
731-
if (\is_array($value) || $value instanceof \Traversable) {
732-
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_ADDER_AND_REMOVER;
733-
$access[self::ACCESS_ADDER] = $methods[0];
734-
$access[self::ACCESS_REMOVER] = $methods[1];
735-
} else {
736-
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
737-
$access[self::ACCESS_NAME] = sprintf(
738-
'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
739-
'the new value must be an array or an instance of \Traversable, '.
740-
'"%s" given.',
741-
$property,
742-
$reflClass->name,
743-
implode('()", "', $methods),
744-
\is_object($value) ? \get_class($value) : \gettype($value)
745-
);
746-
}
742+
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
743+
$access[self::ACCESS_NAME] = sprintf(
744+
'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
745+
'the new value must be an array or an instance of \Traversable, '.
746+
'"%s" given.',
747+
$property,
748+
$reflClass->name,
749+
implode('()", "', $methods),
750+
\is_object($value) ? \get_class($value) : \gettype($value)
751+
);
747752
} else {
748753
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
749754
$access[self::ACCESS_NAME] = sprintf(

0 commit comments

Comments
 (0)