Skip to content

Commit 2abc593

Browse files
radimvaculikhrach
authored andcommitted
Support nullable classes definition with '?'
1 parent feaa3e6 commit 2abc593

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/Entity/Reflection/MetadataParser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ protected function parseAnnotationTypes(PropertyMetadata $property, string $type
225225
if (($type[0] ?? '') === '?') {
226226
$isNullable = true;
227227
$typeLower = substr($typeLower, 1);
228+
$type = substr($type, 1);
228229
}
229230
if (strpos($type, '[') !== false) { // string[]
230231
$type = 'array';

tests/cases/unit/Entity/Reflection/PropertyMetadata.isValid().phpt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ $dic = require_once __DIR__ . '/../../../../bootstrap.php';
3434
* @property scalar $scalar
3535
* @property mixed $mixed
3636
* @property ArrayHash $type
37-
* @property bool|NULL $nullable
37+
* @property bool|NULL $nullable1
38+
* @property ?\DateTimeImmutable $nullable2
3839
*/
3940
class ValidationTestEntity
4041
{
@@ -282,7 +283,7 @@ class PropertyMetadataIsValidTest extends TestCase
282283

283284
public function testNullable(): void
284285
{
285-
$property = $this->metadata->getProperty('nullable');
286+
$property = $this->metadata->getProperty('nullable1');
286287

287288
$val = null;
288289
Assert::true($property->isValid($val));
@@ -293,6 +294,17 @@ class PropertyMetadataIsValidTest extends TestCase
293294
$val = 0;
294295
Assert::true($property->isValid($val));
295296
Assert::false($val);
297+
298+
$property = $this->metadata->getProperty('nullable2');
299+
300+
$val = null;
301+
Assert::true($property->isValid($val));
302+
303+
$val = false;
304+
Assert::false($property->isValid($val));
305+
306+
$val = new DateTimeImmutable;
307+
Assert::true($property->isValid($val));
296308
}
297309

298310

0 commit comments

Comments
 (0)