sirbrillig / phpcs-variable-analysis

Find undefined and unused variables with the PHP Codesniffer static analysis tool.
Other
135 stars 14 forks source link

Support constructor promotion with readonly properties (UnusedVariable) #290

Closed defunctl closed 1 year ago

defunctl commented 1 year ago

The following throws this error:

phpcs: VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable: Unused function parameter $message.

<?php declare(strict_types=1);

namespace MyProject;

final class Manager {

    public function __construct(
        private readonly string $message
    ) {}

    public function getMessage(): string {
        return $this->message;
    }

}

PHP Version: 8.1 Variable Analysis Version: v2.11.10

sirbrillig commented 1 year ago

Interesting! Constructor promotion support was added in https://github.com/sirbrillig/phpcs-variable-analysis/pull/266 but I bet that the readonly token could be breaking the logic.

jrfnl commented 1 year ago

@sirbrillig FYI, in case it helps: the PHPCS native File::getMethodParameters() method has support for both, but using that would require some changes in how things are being sniffed (or detection of a function declaration based on the parentheses surrounding parameters and using the method that way).

jrfnl commented 1 year ago

Just ran into this one myself now ;-)

sirbrillig commented 1 year ago

https://github.com/sirbrillig/phpcs-variable-analysis/pull/294 should fix this.

jrfnl commented 1 year ago

Much appreciated @sirbrillig !