Skip to content

Commit

Permalink
PHP 8.0 | Squiz/ObjectInstantiation: prevent false positives on match…
Browse files Browse the repository at this point in the history
…/fn expressions

This change ensures that when `new Class` is returned by a match control structure or an arrow function, the sniff will not throw a false positive.

Includes unit tests.
  • Loading branch information
jrfnl committed Feb 23, 2021
1 parent 8b0137a commit 3148f1b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function process(File $phpcsFile, $stackPtr)
$allowedTokens = [
T_EQUAL => true,
T_DOUBLE_ARROW => true,
T_FN_ARROW => true,
T_MATCH_ARROW => true,
T_THROW => true,
T_RETURN => true,
T_INLINE_THEN => true,
Expand Down
11 changes: 11 additions & 0 deletions src/Standards/Squiz/Tests/Objects/ObjectInstantiationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,16 @@ function foo() { return new MyClass(); }

$doodad = $x ? new Foo : new Bar;

function returnFn() {
$fn = fn($x) => new MyClass();
}

function returnMatch() {
$match = match($x) {
0 => new MyClass()
}
}

// Intentional parse error. This must be the last test in the file.
function new
?>

0 comments on commit 3148f1b

Please sign in to comment.