Skip to content

Commit

Permalink
StaticClosureSniff: Work on fn() too
Browse files Browse the repository at this point in the history
Bug: T328529
Change-Id: Ia8f1d8de3183e3e55af5ff88d8ce451e2fb2a9fc
  • Loading branch information
reedy authored and jenkins-bot committed Jan 10, 2025
1 parent 7bf21d7 commit 9479c6b
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion MediaWiki/Sniffs/Usage/StaticClosureSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class StaticClosureSniff implements Sniff {
* @inheritDoc
*/
public function register(): array {
return [ T_CLOSURE ];
return [ T_CLOSURE, T_FN ];
}

/**
Expand Down
2 changes: 2 additions & 0 deletions MediaWiki/Tests/files/Usage/nullable_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function testClosure(): void {
}

public function testArrowFunctions(): void {
// phpcs:ignore MediaWiki.Usage.StaticClosure.StaticClosure
$c = fn ( MyClass $x = null ) => $x;
}

Expand Down Expand Up @@ -66,6 +67,7 @@ public function testClosure(): void {
}

public function testArrowFunctions(): void {
// phpcs:ignore MediaWiki.Usage.StaticClosure.StaticClosure
$c = fn ( ?MyClass $x ) => $x;
}
}
2 changes: 1 addition & 1 deletion MediaWiki/Tests/files/Usage/nullable_type.php.expect
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
| | (MediaWiki.Usage.NullableType.ExplicitNullableTypes)
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")
40 | 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
2 changes: 2 additions & 0 deletions MediaWiki/Tests/files/Usage/nullable_type.php.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class FailedExamples {
}

public function testArrowFunctions(): void {
// phpcs:ignore MediaWiki.Usage.StaticClosure.StaticClosure
$c = fn ( ?MyClass $x = null ) => $x;
}

Expand Down Expand Up @@ -66,6 +67,7 @@ class PassedExamples {
}

public function testArrowFunctions(): void {
// phpcs:ignore MediaWiki.Usage.StaticClosure.StaticClosure
$c = fn ( ?MyClass $x ) => $x;
}
}
3 changes: 3 additions & 0 deletions MediaWiki/Tests/files/Usage/static_closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function doStuff() {
return $val === 1;
} );

array_filter( $a, static fn ( $val ) => $val === 1 );
array_filter( $a, fn ( $val ) => $val === 1 );

// Class context needed
array_filter( $a, function ( $val ) {
return parent::doParentStuff();
Expand Down
15 changes: 8 additions & 7 deletions MediaWiki/Tests/files/Usage/static_closure.php.expect
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
21 | ERROR | [ ] Only one object structure is allowed in a file
| | (Generic.Files.OneObjectStructurePerFile.MultipleFound)
41 | WARNING | [x] Use static closure (MediaWiki.Usage.StaticClosure.StaticClosure)
64 | ERROR | [x] Cannot not use static closure in class context
46 | WARNING | [x] Use static closure (MediaWiki.Usage.StaticClosure.StaticClosure)
67 | ERROR | [x] Cannot not use static closure in class context
| | (MediaWiki.Usage.StaticClosure.NonStaticClosure)
77 | WARNING | [x] Use static closure (MediaWiki.Usage.StaticClosure.StaticClosure)
88 | WARNING | [x] Use static closure (MediaWiki.Usage.StaticClosure.StaticClosure)
88 | ERROR | [x] There must not be a space before the colon in a return type declaration
80 | WARNING | [x] Use static closure (MediaWiki.Usage.StaticClosure.StaticClosure)
91 | WARNING | [x] Use static closure (MediaWiki.Usage.StaticClosure.StaticClosure)
91 | ERROR | [x] There must not be a space before the colon in a return type declaration
| | (PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeColon)
89 | WARNING | [x] Use static closure (MediaWiki.Usage.StaticClosure.StaticClosure)
89 | ERROR | [x] There must not be a space before the colon in a return type declaration
92 | WARNING | [x] Use static closure (MediaWiki.Usage.StaticClosure.StaticClosure)
92 | ERROR | [x] There must not be a space before the colon in a return type declaration
| | (PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeColon)
PHPCBF CAN FIX THE 7 MARKED SNIFF VIOLATIONS AUTOMATICALLY
PHPCBF CAN FIX THE 8 MARKED SNIFF VIOLATIONS AUTOMATICALLY
3 changes: 3 additions & 0 deletions MediaWiki/Tests/files/Usage/static_closure.php.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class Foo extends Bar {
return $val === 1;
} );

array_filter( $a, static fn ( $val ) => $val === 1 );
array_filter( $a, static fn ( $val ) => $val === 1 );

// Class context needed
array_filter( $a, function ( $val ) {
return parent::doParentStuff();
Expand Down
2 changes: 2 additions & 0 deletions MediaWiki/Tests/files/WhiteSpace/space_after_closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
};

array_filter( $a, static fn ( $val ) => $val === 1 );
// phpcs:ignore MediaWiki.Usage.StaticClosure.StaticClosure
array_filter( $a, fn ( $val ) => $val === 1 );

$bad = static function() {
Expand All @@ -23,4 +24,5 @@
};

array_filter( $a, static fn( $val ) => $val === 1 );
// phpcs:ignore MediaWiki.Usage.StaticClosure.StaticClosure
array_filter( $a, fn( $val ) => $val === 1 );
10 changes: 5 additions & 5 deletions MediaWiki/Tests/files/WhiteSpace/space_after_closure.php.expect
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
13 | ERROR | [x] A single space should be after the function keyword in closures
14 | ERROR | [x] A single space should be after the function keyword in closures
| | (MediaWiki.WhiteSpace.SpaceAfterClosure.NoWhitespaceAfterClosure)
17 | ERROR | [x] A single space should be after the function keyword in closures
18 | ERROR | [x] A single space should be after the function keyword in closures
| | (MediaWiki.WhiteSpace.SpaceAfterClosure.WrongWhitespaceAfterClosure)
21 | ERROR | [x] A single space should be after the function keyword in closures
22 | ERROR | [x] A single space should be after the function keyword in closures
| | (MediaWiki.WhiteSpace.SpaceAfterClosure.NoWhitespaceAfterClosure)
25 | ERROR | [x] A single space should be after the fn keyword in closures
| | (MediaWiki.WhiteSpace.SpaceAfterClosure.NoWhitespaceAfterArrow)
26 | ERROR | [x] A single space should be after the fn keyword in closures
| | (MediaWiki.WhiteSpace.SpaceAfterClosure.NoWhitespaceAfterArrow)
28 | ERROR | [x] A single space should be after the fn keyword in closures
| | (MediaWiki.WhiteSpace.SpaceAfterClosure.NoWhitespaceAfterArrow)
PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ $good_with_arg = static function ( $arg ) {
};

array_filter( $a, static fn ( $val ) => $val === 1 );
// phpcs:ignore MediaWiki.Usage.StaticClosure.StaticClosure
array_filter( $a, fn ( $val ) => $val === 1 );

$bad = static function () {
Expand All @@ -23,4 +24,5 @@ $bad_with_arg = static function ( $param ) {
};

array_filter( $a, static fn ( $val ) => $val === 1 );
// phpcs:ignore MediaWiki.Usage.StaticClosure.StaticClosure
array_filter( $a, fn ( $val ) => $val === 1 );

0 comments on commit 9479c6b

Please sign in to comment.