Skip to content

Commit ecc23f3

Browse files
committed
PHP 8.2 | PSR12/NullableTypeDeclaration: allow for nullable true/false
As of PHP 8.2, `true`, `false` and `null` will be allowed as stand-alone types. The `true` and the `false` types are allowed to be nullable, the `null` type is not (but that's not the concern of the sniff). Also see: https://3v4l.org/ZpfID This adjusts the sniff to take these new types into account. Includes unit tests. Refs: * https://wiki.php.net/rfc/null-false-standalone-types * https://wiki.php.net/rfc/true-type
1 parent b7c3778 commit ecc23f3

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

src/Standards/PSR12/Sniffs/Functions/NullableTypeDeclarationSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class NullableTypeDeclarationSniff implements Sniff
2929
T_SELF => true,
3030
T_PARENT => true,
3131
T_STATIC => true,
32+
T_NULL => true,
33+
T_FALSE => true,
34+
T_TRUE => true,
3235
];
3336

3437

src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,11 @@ class testInstanceOf() {
8585

8686
// PHP 8.0: static return type.
8787
function testStatic() : ? static {}
88+
89+
// PHP 8.2: nullable true/false.
90+
function fooG(): ? true {}
91+
function fooH(): ?
92+
false {}
93+
94+
// Fatal error: null cannot be marked as nullable, but that's not the concern of this sniff.
95+
function fooI(): ? null {}

src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,10 @@ class testInstanceOf() {
8383

8484
// PHP 8.0: static return type.
8585
function testStatic() : ?static {}
86+
87+
// PHP 8.2: nullable true/false.
88+
function fooG(): ?true {}
89+
function fooH(): ?false {}
90+
91+
// Fatal error: null cannot be marked as nullable, but that's not the concern of this sniff.
92+
function fooI(): ?null {}

src/Standards/PSR12/Tests/Functions/NullableTypeDeclarationUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ protected function getErrorList()
4141
58 => 2,
4242
59 => 2,
4343
87 => 1,
44+
90 => 1,
45+
91 => 1,
46+
95 => 1,
4447
];
4548

4649
}//end getErrorList()

0 commit comments

Comments
 (0)