From 1ab281861bddc3f93f8a03103b772bc768b1ab38 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Mon, 23 Mar 2020 17:33:08 +0100 Subject: [PATCH 1/3] [GH-36] Bugfix: TypedNoDefaultReflectionProperty::setValue NULL when null allowed. --- .../TypedNoDefaultReflectionProperty.php | 2 +- .../TypedNoDefaultReflectionPropertyTest.php | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/Common/Reflection/TypedNoDefaultReflectionProperty.php b/lib/Doctrine/Common/Reflection/TypedNoDefaultReflectionProperty.php index 25a6c29..14e4437 100644 --- a/lib/Doctrine/Common/Reflection/TypedNoDefaultReflectionProperty.php +++ b/lib/Doctrine/Common/Reflection/TypedNoDefaultReflectionProperty.php @@ -31,7 +31,7 @@ public function getValue($object = null) */ public function setValue($object, $value = null) { - if ($value === null) { + if ($value === null && ! $this->getType()->allowsNull()) { $propertyName = $this->getName(); $unsetter = function () use ($propertyName) { diff --git a/tests/Doctrine/Tests_PHP74/Common/Reflection/TypedNoDefaultReflectionPropertyTest.php b/tests/Doctrine/Tests_PHP74/Common/Reflection/TypedNoDefaultReflectionPropertyTest.php index 64a89ca..44738e9 100644 --- a/tests/Doctrine/Tests_PHP74/Common/Reflection/TypedNoDefaultReflectionPropertyTest.php +++ b/tests/Doctrine/Tests_PHP74/Common/Reflection/TypedNoDefaultReflectionPropertyTest.php @@ -32,9 +32,26 @@ public function testSetValueNull() : void $object = new TypedFoo(); $object->setId(1); + self::assertTrue($reflection->isInitialized($object)); + + $reflection->setValue($object, null); + + self::assertNull($reflection->getValue($object)); + self::assertFalse($reflection->isInitialized($object)); + } + + public function testSetValueNullOnNullableProperty() : void + { + $reflection = new TypedNoDefaultReflectionProperty(TypedNullableFoo::class, 'value'); + $reflection->setAccessible(true); + + $object = new TypedNullableFoo(); + $reflection->setValue($object, null); self::assertNull($reflection->getValue($object)); + self::assertTrue($reflection->isInitialized($object)); + self::assertNull($object->getValue()); } } @@ -52,3 +69,18 @@ public function setId($id) $this->id = $id; } } + +class TypedNullableFoo +{ + private ?string $value; + + public function setValue($value) + { + $this->value = $value; + } + + public function getValue() + { + return $this->value; + } +} From 1a5837f909a81f10f5ee4d0d299d42a53403bc0b Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Mon, 23 Mar 2020 20:41:18 +0100 Subject: [PATCH 2/3] [Build] Attempt at fix. --- .scrutinizer.yml | 2 +- .travis.yml | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 626a478..757f885 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -16,7 +16,7 @@ build: - phpcs-run dependencies: override: - - composer install -noa + - COMPOSER_ROOT_VERSION=1.2 composer install -noa tools: external_code_coverage: diff --git a/.travis.yml b/.travis.yml index b8bf157..2121f22 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,10 @@ dist: trusty sudo: false language: php +env: + global: + - COMPOSER_ROOT_VERSION=1.2 + cache: directories: - $HOME/.composer/cache From fbc77cc1c8ac834d33a773640469c3d3a66d0538 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Wed, 25 Mar 2020 10:09:58 +0100 Subject: [PATCH 3/3] [GH-36] Be on the safe side and check property has a type. --- .../Common/Reflection/TypedNoDefaultReflectionProperty.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/Common/Reflection/TypedNoDefaultReflectionProperty.php b/lib/Doctrine/Common/Reflection/TypedNoDefaultReflectionProperty.php index 14e4437..4f8eece 100644 --- a/lib/Doctrine/Common/Reflection/TypedNoDefaultReflectionProperty.php +++ b/lib/Doctrine/Common/Reflection/TypedNoDefaultReflectionProperty.php @@ -31,7 +31,7 @@ public function getValue($object = null) */ public function setValue($object, $value = null) { - if ($value === null && ! $this->getType()->allowsNull()) { + if ($value === null && $this->hasType() && ! $this->getType()->allowsNull()) { $propertyName = $this->getName(); $unsetter = function () use ($propertyName) {