4 Commits
1.2.0 ... 1.2.1

Author SHA1 Message Date
Benjamin Eberlei
55e71912df Merge pull request #37 from doctrine/GH-36-BugfixTypedNullable
[GH-36] Bugfix: TypedNoDefaultReflectionProperty::setValue NULL when null allowed
2020-03-27 12:06:43 +01:00
Benjamin Eberlei
fbc77cc1c8 [GH-36] Be on the safe side and check property has a type. 2020-03-25 10:09:58 +01:00
Benjamin Eberlei
1a5837f909 [Build] Attempt at fix. 2020-03-23 20:41:18 +01:00
Benjamin Eberlei
1ab281861b [GH-36] Bugfix: TypedNoDefaultReflectionProperty::setValue NULL when null allowed. 2020-03-23 17:33:08 +01:00
4 changed files with 38 additions and 2 deletions

View File

@@ -16,7 +16,7 @@ build:
- phpcs-run
dependencies:
override:
- composer install -noa
- COMPOSER_ROOT_VERSION=1.2 composer install -noa
tools:
external_code_coverage:

View File

@@ -2,6 +2,10 @@ dist: trusty
sudo: false
language: php
env:
global:
- COMPOSER_ROOT_VERSION=1.2
cache:
directories:
- $HOME/.composer/cache

View File

@@ -31,7 +31,7 @@ class TypedNoDefaultReflectionProperty extends ReflectionProperty
*/
public function setValue($object, $value = null)
{
if ($value === null) {
if ($value === null && $this->hasType() && ! $this->getType()->allowsNull()) {
$propertyName = $this->getName();
$unsetter = function () use ($propertyName) {

View File

@@ -32,9 +32,26 @@ class TypedNoDefaultReflectionPropertyTest extends TestCase
$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 @@ class TypedFoo
$this->id = $id;
}
}
class TypedNullableFoo
{
private ?string $value;
public function setValue($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}