Skip to content

Commit 4c3d9d3

Browse files
committed
TypeString::isNullable(): correctly handle FQN null
Includes tests.
1 parent 0c75908 commit 4c3d9d3

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

PHPCSUtils/Utils/TypeString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public static function isNullable($typeString)
218218

219219
// Check for nullable union type.
220220
$matched = \preg_match(
221-
'`(?<before>^|[^|&(?\s]+\s*\|)\s*null\s*(?<after>\|\s*[^|&)?\s]+|$)`i',
221+
'`(?<before>^|[^|&(?\s]+\s*\|)\s*[\\\\]?null\s*(?<after>\|\s*[^|&)?\s]+|$)`i',
222222
$typeString,
223223
$matches
224224
);

Tests/Utils/TypeString/IsTypeTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ public static function dataTypeStrings()
319319
'dnf' => false,
320320
],
321321
],
322+
'nullable union type: \true|\null (FQN)' => [
323+
'type' => '\true|\null',
324+
'expected' => [
325+
'singular' => false,
326+
'nullable' => true,
327+
'union' => true,
328+
'intersection' => false,
329+
'dnf' => false,
330+
],
331+
],
322332

323333
'intersection type: UnqualifiedName&Package\Partially&\Vendor\FullyQualified&namespace\Relative\Name' => [
324334
'type' => 'UnqualifiedName&Package\Partially&\Vendor\FullyQualified&namespace\Relative\Name',
@@ -391,6 +401,36 @@ public static function dataTypeStrings()
391401
'dnf' => true,
392402
],
393403
],
404+
'DNF type: FQN null at end' => [
405+
'type' => '(Foo&Bar)|\null',
406+
'expected' => [
407+
'singular' => false,
408+
'nullable' => true,
409+
'union' => false,
410+
'intersection' => false,
411+
'dnf' => true,
412+
],
413+
],
414+
'DNF type: FQN null in the middle' => [
415+
'type' => '(Foo&Bar)|\null|(Baz&Countable)',
416+
'expected' => [
417+
'singular' => false,
418+
'nullable' => true,
419+
'union' => false,
420+
'intersection' => false,
421+
'dnf' => true,
422+
],
423+
],
424+
'DNF type: FQN null at start and whitespace rich' => [
425+
'type' => ' \null | ( Foo & Bar & \Baz )',
426+
'expected' => [
427+
'singular' => false,
428+
'nullable' => true,
429+
'union' => false,
430+
'intersection' => false,
431+
'dnf' => true,
432+
],
433+
],
394434
];
395435
}
396436

0 commit comments

Comments
 (0)