Skip to content

Commit 41d5657

Browse files
committed
Allow empty constructor function blocks when using property promotion
1 parent 8085e03 commit 41d5657

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ public function process(File $phpcsFile, $stackPtr)
3737
return;
3838
}
3939

40+
// Ignore empty constructor function blocks when using property promotion
41+
if ($tokens[$stackPtr]['code'] === T_FUNCTION &&
42+
strpos($phpcsFile->getDeclarationName($stackPtr), '__construct') === 0 &&
43+
count($phpcsFile->getMethodParameters($stackPtr)) > 0 &&
44+
array_reduce($phpcsFile->getMethodParameters($stackPtr), static function ($result, $methodParam) {
45+
return $result && isset($methodParam['property_visibility']);
46+
}, true)) {
47+
48+
return;
49+
}
50+
4051
parent::process($phpcsFile, $stackPtr);
4152
}//end process()
4253
}

Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.inc

+12
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,15 @@ function emptyFunction () { /*Empty function block*/ }
7676
function nonEmptyFunction () { return true; }
7777

7878
function aroundEmptyFunction ($foo, $bar) { }
79+
80+
class Foo {
81+
public function __construct(private $bar) {}
82+
}
83+
84+
class Foo2 {
85+
public function __construct() {}
86+
}
87+
88+
class Foo3 {
89+
public function __construct(private $bar, $baz) {}
90+
}

Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function getErrorList()
2929
68 => 1,
3030
72 => 2,
3131
74 => 1,
32+
85 => 1,
33+
89 => 1
3234
];
3335
}
3436

0 commit comments

Comments
 (0)