From 5cb8312785b795513a0e3027f67c86531c855e02 Mon Sep 17 00:00:00 2001 From: Fabian Haase Date: Sat, 21 Aug 2021 16:06:03 +0200 Subject: [PATCH] [PropertyAccess] Fix Regression in PropertyAccessor::isWritable() --- .../PropertyAccess/PropertyAccessor.php | 10 ++-------- .../Tests/PropertyAccessorArrayAccessTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php index 5ece2c17e87bb..583bd886911fe 100644 --- a/src/Symfony/Component/PropertyAccess/PropertyAccessor.php +++ b/src/Symfony/Component/PropertyAccess/PropertyAccessor.php @@ -313,10 +313,8 @@ public function isWritable($objectOrArray, $propertyPath) if (!$zval[self::VALUE] instanceof \ArrayAccess && !\is_array($zval[self::VALUE])) { return false; } - } else { - if (!$this->isPropertyWritable($zval[self::VALUE], $propertyPath->getElement($i))) { - return false; - } + } elseif (!\is_object($zval[self::VALUE]) || !$this->isPropertyWritable($zval[self::VALUE], $propertyPath->getElement($i))) { + return false; } if (\is_object($zval[self::VALUE])) { @@ -663,10 +661,6 @@ private function getWriteInfo(string $class, string $property, $value): Property */ private function isPropertyWritable(object $object, string $property): bool { - if (!\is_object($object)) { - return false; - } - $mutatorForArray = $this->getWriteInfo(\get_class($object), $property, []); if (PropertyWriteInfo::TYPE_NONE !== $mutatorForArray->getType() || ($object instanceof \stdClass && property_exists($object, $property))) { diff --git a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorArrayAccessTest.php b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorArrayAccessTest.php index 71e71a7838600..98d6eb57d5936 100644 --- a/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorArrayAccessTest.php +++ b/src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorArrayAccessTest.php @@ -38,6 +38,14 @@ public function getValidPropertyPaths() ]; } + public function getInvalidPropertyPaths() + { + return [ + [$this->getContainer(['firstName' => 'Bernhard']), 'firstName', 'Bernhard'], + [$this->getContainer(['person' => $this->getContainer(['firstName' => 'Bernhard'])]), 'person.firstName', 'Bernhard'], + ]; + } + /** * @dataProvider getValidPropertyPaths */ @@ -83,4 +91,12 @@ public function testIsWritable($collection, $path) { $this->assertTrue($this->propertyAccessor->isWritable($collection, $path)); } + + /** + * @dataProvider getInvalidPropertyPaths + */ + public function testIsNotWritable($collection, $path) + { + $this->assertFalse($this->propertyAccessor->isWritable($collection, $path)); + } }