10000 [PropertyAccess] Throw an InvalidArgumentException when the type do not match by dunglas · Pull Request #17738 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyAccess] Throw an InvalidArgumentException when the type do not match #17738

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 10 commits 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
47 changes: 43 additions & 4 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ private function writeIndex(&$array, $index, $value)
*
* @throws NoSuchPropertyException If the property does not exist or is not
* public.
* @throws \TypeError
*/
private function writeProperty(&$object, $property, $value)
{
Expand All @@ -553,7 +554,7 @@ private function writeProperty(&$object, $property, $value)
$access = $this->getWriteAccessInfo($object, $property, $value);

if (self::ACCESS_TYPE_METHOD === $access[self::ACCESS_TYPE]) {
$object->{$access[self::ACCESS_NAME]}($value);
$this->callMethod($object, $access[self::ACCESS_NAME], $value);
} elseif (self::ACCESS_TYPE_PROPERTY === $access[self::ACCESS_TYPE]) {
$object->{$access[self::ACCESS_NAME]} = $value;
} elseif (self::ACCESS_TYPE_ADDER_AND_REMOVER === $access[self::ACCESS_TYPE]) {
Expand All @@ -567,12 +568,48 @@ private function writeProperty(&$object, $property, $value)

$object->$property = $value;
} elseif (self::ACCESS_TYPE_MAGIC === $access[self::ACCESS_TYPE]) {
$object->{$access[self::ACCESS_NAME]}($value);
$this->callMethod($object, $access[self::ACCESS_NAME], $value);
} else {
throw new NoSuchPropertyException($access[self::ACCESS_NAME]);
}
}

/**
* Throws a {@see \TypeError} as in PHP 7 when using PHP 5.
*
* @param object $object
* @param string $method
* @param mixed $value
*
* @throws \TypeError
* @throws \Exception
*/
private function callMethod($object, $method, $value) {
Copy link
Member

Choose a reason for hiding this comment

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

{ should be on the next line

8000
if (PHP_MAJOR_VERSION >= 7) {
$object->{$method}($value);

return;
}

set_error_handler(function ($errno, $errstr) use ($object, $method) {
if (E_RECOVERABLE_ERROR === $errno && false !== strpos($errstr, sprintf('passed to %s::%s() must', get_class($object), $method))) {
throw new \TypeError($errstr);
}

return false;
});

try {
$object->{$method}($value);
restore_error_handler();
} catch (\Exception $e) {
// Cannot use finally in 5.5 because of https://bugs.php.net/bug.php?id=67047
restore_error_handler();

throw $e;
}
}

/**
* Adjusts a collection-valued property by calling add*() and remove*()
* methods.
Expand All @@ -582,6 +619,8 @@ private function writeProperty(&$object, $property, $value)
* @param array|\Traversable $collection The collection to write
* @param string $addMethod The add*() method
* @param string $removeMethod The remove*() method
*
* @throws \TypeError
*/
private function writeCollection($object, $property, $collection, $addMethod, $removeMethod)
{
Expand Down Expand Up @@ -613,11 +652,11 @@ private function writeCollection($object, $property, $collection, $addMethod, $r
}

foreach ($itemToRemove as $item) {
$object->{$removeMethod}($item);
$this->callMethod($object, $removeMethod, $item);
}

foreach ($itemsToAdd as $item) {
$object->{$addMethod}($item);
$this->callMethod($object, $addMethod, $item);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ interface PropertyAccessorInterface
* @throws Exception\AccessException If a property/index does not exist or is not public
* @throws Exception\UnexpectedTypeException If a value within the path is neither object
* nor array
* @throws \TypeError If a the type of the value does not match the type
* of the parameter of the mutator method
*/
public function setValue(&$objectOrArray, $propertyPath, $value);

Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/PropertyAccess/Tests/Fixtures/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TestClass
private $publicIsAccessor;
private $publicHasAccessor;
private $publicGetter;
private $date;

public function __construct($value)
{
Expand Down Expand Up @@ -173,4 +174,14 @@ public function getPublicGetter()
{
return $this->publicGetter;
}

public function setDate(\DateTimeInterface $date)
{
$this->date = $date;
}

public function getDate()
{
return $this->date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,21 @@ public function testIsWritableForReferenceChainIssue($object, $path, $value)
{
$this->assertEquals($value, $this->propertyAccessor->isWritable($object, $path));
}

/**
* @expectedException \TypeError
*/
public function testThrowTypeError()
{
$this->propertyAccessor->setValue(new TestClass('Kévin'), 'date', 'This is a string, \DateTime excepted.');
}

public function testSetTypeHint()
{
$date = new \DateTimeImmutable();
$object = new TestClass('Kévin');

$this->propertyAccessor->setValue($object, 'date', $date);
$this->assertSame($date, $object->getDate());
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/PropertyAccess/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
}
],
"require": {
"php": ">=5.5.9"
"php": ">=5.5.9",
"symfony/polyfill-php70": "~1.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\PropertyAccess\\": "" },
Expand Down
0