Skip to content

Commit 529043f

Browse files
committed
PHP 8.0 | Generic/UnusedFunctionParameter: add support for constructor property promotion
When constructor property promotion is used, the properties declared in the class constructor should never be marked as unused by this sniff. Includes tests. Fixes 3269
1 parent 46715ed commit 529043f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public function process(File $phpcsFile, $stackPtr)
9090
}
9191

9292
foreach ($methodParams as $param) {
93+
if (isset($param['property_visibility']) === true) {
94+
// Ignore. Constructor property promotion.
95+
continue;
96+
}
97+
9398
$params[$param['name']] = $stackPtr;
9499
}
95100

src/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,16 @@ function moreParamFirst(Exception $foo, LogicException $bar) {
137137
function moreParamSecond(LogicException $bar, Exception $foo) {
138138
return 'foobar' . $bar;
139139
}
140+
// phpcs:set Generic.CodeAnalysis.UnusedFunctionParameter ignoreTypeHints[]
141+
142+
class ConstructorPropertyPromotionNoContentInMethod {
143+
public function __construct(protected int $id) {}
144+
}
145+
146+
class ConstructorPropertyPromotionWithContentInMethod {
147+
public function __construct(protected int $id, $toggle = true) {
148+
if ($toggle === true) {
149+
doSomething();
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)