Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #37 from doctrine/GH-36-BugfixTypedNullable
Browse files Browse the repository at this point in the history
[GH-36] Bugfix: TypedNoDefaultReflectionProperty::setValue NULL when null allowed
  • Loading branch information
beberlei authored Mar 27, 2020
2 parents b699ecc + fbc77cc commit 55e7191
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ build:
- phpcs-run
dependencies:
override:
- composer install -noa
- COMPOSER_ROOT_VERSION=1.2 composer install -noa

tools:
external_code_coverage:
Expand Down
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ dist: trusty
sudo: false
language: php

env:
global:
- COMPOSER_ROOT_VERSION=1.2

cache:
directories:
- $HOME/.composer/cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function getValue($object = null)
*/
public function setValue($object, $value = null)
{
if ($value === null) {
if ($value === null && $this->hasType() && ! $this->getType()->allowsNull()) {
$propertyName = $this->getName();

$unsetter = function () use ($propertyName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand All @@ -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;
}
}

0 comments on commit 55e7191

Please sign in to comment.