Skip to content

Commit

Permalink
NullableTypeSniff: Also check closure and arrow functions
Browse files Browse the repository at this point in the history
Bug: T362014
Follow-Up: Ide15839e98a6229c22584d1c1c88c690982e1d7a
Change-Id: I6581b9a41e5c7993b053eee6f962e5675dccb0c2
  • Loading branch information
umherirrender committed Oct 24, 2024
1 parent 477ac00 commit 21db407
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion MediaWiki/Sniffs/Usage/NullableTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class NullableTypeSniff implements Sniff {
* @inheritDoc
*/
public function register(): array {
return [ T_FUNCTION ];
return [ T_FUNCTION, T_CLOSURE, T_FN ];
}

/**
Expand Down
20 changes: 20 additions & 0 deletions MediaWiki/Tests/files/Usage/nullable_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public function nullableAfterRequired2( $foo, MyClass $x = null, MyClass $y = nu
*/
public function nullableOnly( MyClass $x = null ) {
}

public function testClosure(): void {
$c = static function ( MyClass $x = null ) {
return $x;
};
}

public function testArrowFunctions(): void {
$c = fn ( MyClass $x = null ) => $x;
}
}

class PassedExamples {
Expand All @@ -45,4 +55,14 @@ public function genericNullable( ?MyClass $x, $foo ) {
*/
public function docOptionalArgNullable( $foo, ?MyClass $x ) {
}

public function testClosure(): void {
$c = static function ( ?MyClass $x ) {
return $x;
};
}

public function testArrowFunctions(): void {
$c = fn ( ?MyClass $x ) => $x;
}
}
6 changes: 5 additions & 1 deletion MediaWiki/Tests/files/Usage/nullable_type.php.expect
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
| | (MediaWiki.Usage.NullableType.ExplicitNullableTypes)
29 | ERROR | [x] Use PHP 8.4 compatible syntax for explicit nullable types ("?MyClass $x = null")
| | (MediaWiki.Usage.NullableType.ExplicitNullableTypes)
PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
33 | ERROR | [x] Use PHP 8.4 compatible syntax for explicit nullable types ("?MyClass $x = null")
| | (MediaWiki.Usage.NullableType.ExplicitNullableTypes)
39 | ERROR | [x] Use PHP 8.4 compatible syntax for explicit nullable types ("?MyClass $x = null")
| | (MediaWiki.Usage.NullableType.ExplicitNullableTypes)
PHPCBF CAN FIX THE 7 MARKED SNIFF VIOLATIONS AUTOMATICALLY
20 changes: 20 additions & 0 deletions MediaWiki/Tests/files/Usage/nullable_type.php.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class FailedExamples {
*/
public function nullableOnly( ?MyClass $x = null ) {
}

public function testClosure(): void {
$c = static function ( ?MyClass $x = null ) {
return $x;
};
}

public function testArrowFunctions(): void {
$c = fn ( ?MyClass $x = null ) => $x;
}
}

class PassedExamples {
Expand All @@ -45,4 +55,14 @@ class PassedExamples {
*/
public function docOptionalArgNullable( $foo, ?MyClass $x ) {
}

public function testClosure(): void {
$c = static function ( ?MyClass $x ) {
return $x;
};
}

public function testArrowFunctions(): void {
$c = fn ( ?MyClass $x ) => $x;
}
}

0 comments on commit 21db407

Please sign in to comment.