Skip to content

Commit

Permalink
Support nullable classes definition with '?'
Browse files Browse the repository at this point in the history
  • Loading branch information
radimvaculik authored and hrach committed Sep 7, 2022
1 parent feaa3e6 commit 2abc593
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Entity/Reflection/MetadataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ protected function parseAnnotationTypes(PropertyMetadata $property, string $type
if (($type[0] ?? '') === '?') {
$isNullable = true;
$typeLower = substr($typeLower, 1);
$type = substr($type, 1);
}
if (strpos($type, '[') !== false) { // string[]
$type = 'array';
Expand Down
16 changes: 14 additions & 2 deletions tests/cases/unit/Entity/Reflection/PropertyMetadata.isValid().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ $dic = require_once __DIR__ . '/../../../../bootstrap.php';
* @property scalar $scalar
* @property mixed $mixed
* @property ArrayHash $type
* @property bool|NULL $nullable
* @property bool|NULL $nullable1
* @property ?\DateTimeImmutable $nullable2
*/
class ValidationTestEntity
{
Expand Down Expand Up @@ -282,7 +283,7 @@ class PropertyMetadataIsValidTest extends TestCase

public function testNullable(): void
{
$property = $this->metadata->getProperty('nullable');
$property = $this->metadata->getProperty('nullable1');

$val = null;
Assert::true($property->isValid($val));
Expand All @@ -293,6 +294,17 @@ class PropertyMetadataIsValidTest extends TestCase
$val = 0;
Assert::true($property->isValid($val));
Assert::false($val);

$property = $this->metadata->getProperty('nullable2');

$val = null;
Assert::true($property->isValid($val));

$val = false;
Assert::false($property->isValid($val));

$val = new DateTimeImmutable;
Assert::true($property->isValid($val));
}


Expand Down

0 comments on commit 2abc593

Please sign in to comment.