Skip to content

Commit

Permalink
PHP 8.2 | PSR12/NullableTypeDeclaration: allow for nullable true/false
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrfnl committed Nov 9, 2023
1 parent 22a707b commit 7338523
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class NullableTypeDeclarationSniff implements Sniff
T_SELF => true,
T_PARENT => true,
T_STATIC => true,
T_NULL => true,
T_FALSE => true,
T_TRUE => true,
];


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ class testInstanceOf() {

// PHP 8.0: static return type.
function testStatic() : ? static {}

// PHP 8.2: nullable true/false.
function fooG(): ? true {}
function fooH(): ?
false {}

// Fatal error: null cannot be marked as nullable, but that's not the concern of this sniff.
function fooI(): ? null {}
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,10 @@ class testInstanceOf() {

// PHP 8.0: static return type.
function testStatic() : ?static {}

// PHP 8.2: nullable true/false.
function fooG(): ?true {}
function fooH(): ?false {}

// Fatal error: null cannot be marked as nullable, but that's not the concern of this sniff.
function fooI(): ?null {}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ protected function getErrorList()
58 => 2,
59 => 2,
87 => 1,
90 => 1,
91 => 1,
95 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 7338523

Please sign in to comment.