Skip to content

Commit 3148f1b

Browse files
committed
PHP 8.0 | Squiz/ObjectInstantiation: prevent false positives on match/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.
1 parent 8b0137a commit 3148f1b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Standards/Squiz/Sniffs/Objects/ObjectInstantiationSniff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function process(File $phpcsFile, $stackPtr)
5050
$allowedTokens = [
5151
T_EQUAL => true,
5252
T_DOUBLE_ARROW => true,
53+
T_FN_ARROW => true,
54+
T_MATCH_ARROW => true,
5355
T_THROW => true,
5456
T_RETURN => true,
5557
T_INLINE_THEN => true,

src/Standards/Squiz/Tests/Objects/ObjectInstantiationUnitTest.inc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,16 @@ function foo() { return new MyClass(); }
1313

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

16+
function returnFn() {
17+
$fn = fn($x) => new MyClass();
18+
}
19+
20+
function returnMatch() {
21+
$match = match($x) {
22+
0 => new MyClass()
23+
}
24+
}
25+
26+
// Intentional parse error. This must be the last test in the file.
1627
function new
1728
?>

0 commit comments

Comments
 (0)