8000 [PropertyAccess] fix adder/setter priority by karser · Pull Request #29350 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] fix adder/setter priority #29350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[PropertyAccess] fix adder/setter priority
  • Loading branch information
karser authored and nicolas-grekas committed Nov 29, 2018
commit ea447cbd692ea75a3cb29c44f8ad73e8a0010a4d
39 changes: 22 additions & 17 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
10000
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,8 @@ private function writeCollection($zval, $property, $collection, $addMethod, $rem
*/
private function getWriteAccessInfo($class, $property, $value)
{
$key = str_replace('\\', '.', $class).'..'.$property;
$useAdderAndRemover = \is_array($value) || $value instanceof \Traversable;
$key = str_replace('\\', '.', $class).'..'.$property.'..'.(int) $useAdderAndRemover;

if (isset($this->writePropertyCache[$key])) {
return $this->writePropertyCache[$key];
Expand All @@ -707,6 +708,16 @@ private function getWriteAccessInfo($class, $property, $value)
$camelized = $this->camelize($property);
$singulars = (array) Inflector::singularize($camelized);

if ($useAdderAndRemover && !\in_array($camelized, $singulars)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, this does not work for words where singular and plural are the same (aircraft for example)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the added test in #29355

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, there is a lot of exceptions That are Both Plural and Singular. Although I'm still confused why an empty array is passed to getWriteAccessInfo(). Just to implicitly allow adder/remover?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably something like that, though I don't know for sure. Maybe the git history reveals some more background information.

$methods = $this->findAdderAndRemover($reflClass, $singulars);

if (null !== $methods) {
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_ADDER_AND_REMOVER;
$access[self::ACCESS_ADDER] = $methods[0];
$access[self::ACCESS_REMOVER] = $methods[1];
}
}

if (!isset($access[self::ACCESS_TYPE])) {
$setter = 'set'.$camelized;
$getsetter = lcfirst($camelized); // jQuery style, e.g. read: last(), write: last($item)
Expand All @@ -728,22 +739,16 @@ private function getWriteAccessInfo($class, $property, $value)
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_MAGIC;
$access[self::ACCESS_NAME] = $setter;
} elseif (null !== $methods = $this->findAdderAndRemover($reflClass, $singulars)) {
if (\is_array($value) || $value instanceof \Traversable) {
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_ADDER_AND_REMOVER;
$access[self::ACCESS_ADDER] = $methods[0];
$access[self::ACCESS_REMOVER] = $methods[1];
} else {
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
$access[self::ACCESS_NAME] = sprintf(
'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
'the new value must be an array or an instance of \Traversable, '.
'"%s" given.',
$property,
$reflClass->name,
implode('()", "', $methods),
\is_object($value) ? \get_class($value) : \gettype($value)
);
}
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
$access[self::ACCESS_NAME] = sprintf(
'The property "%s" in class "%s" can be defined with the methods "%s()" but '.
'the new value must be an array or an instance of \Traversable, '.
'"%s" given.',
$property,
$reflClass->name,
implode('()", "', $methods),
\is_object($value) ? \get_class($value) : \gettype($value)
);
} else {
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_NOT_FOUND;
$access[self::ACCESS_NAME] = sprintf(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyAccess\Tests\Fixtures;

class TestAdderVersusSetter
{
/** @var array */
private $emails = array();

/**
* @param array $emails
*/
public function setEmails($emails)
{
throw new \RuntimeException('Setter must NOT be called while adder exists');
}

/**
* @return array
*/
public function getEmails()
{
return $this->emails;
}

/**
* @param string $email
*/
public function addEmail($email)
{
$this->emails[] = $email;
}

/**
* @param string $email
*/
public function removeEmail($email)
{
$this->emails = array_diff($this->emails, array($email));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\Tests\Fixtures\ReturnTyped;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestAdderVersusSetter;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClass;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassIsWritable;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClassMagicCall;
Expand Down Expand Up @@ -722,4 +723,14 @@ public function testWriteToPluralPropertyWhileSingularOneExists()
self::assertEquals(array('test@email.com'), $object->getEmails());
self::assertNull($object->getEmail());
}

public function testAdderHasHigherPriorityThanPluralSetter()
{
$object = new TestAdderVersusSetter();

$this->propertyAccessor->isWritable($object, 'emails'); //cache access info
$this->propertyAccessor->setValue($object, 'emails', array('test@email.com'));

self::assertEquals(array('test@email.com'), $object->getEmails());
}
}
0