Skip to content

Commit

Permalink
PHP 8.0 | Generic/UnusedFunctionParameter: add support for constructo…
Browse files Browse the repository at this point in the history
…r 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
  • Loading branch information
jrfnl committed Mar 16, 2021
1 parent 46715ed commit 529043f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public function process(File $phpcsFile, $stackPtr)
}

foreach ($methodParams as $param) {
if (isset($param['property_visibility']) === true) {
// Ignore. Constructor property promotion.
continue;
}

$params[$param['name']] = $stackPtr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,16 @@ function moreParamFirst(Exception $foo, LogicException $bar) {
function moreParamSecond(LogicException $bar, Exception $foo) {
return 'foobar' . $bar;
}
// phpcs:set Generic.CodeAnalysis.UnusedFunctionParameter ignoreTypeHints[]

class ConstructorPropertyPromotionNoContentInMethod {
public function __construct(protected int $id) {}
}

class ConstructorPropertyPromotionWithContentInMethod {
public function __construct(protected int $id, $toggle = true) {
if ($toggle === true) {
doSomething();
}
}
}

0 comments on commit 529043f

Please sign in to comment.