phpcs-variable-analysis icon indicating copy to clipboard operation
phpcs-variable-analysis copied to clipboard

Unused parameters in methods in abstract classes

Open jrfnl opened this issue 5 years ago • 3 comments

Abstract classes can contain abstract methods which are required to be overloaded, but they can also contain empty methods which are optional to overload.

abstract class Foo
{
    // Required to be overloaded.
    abstract public function bar($param);

    // Optional to be overloaded.
    public function baz($param) {
    }
}

This standard will throw a Unused function parameter $param.(VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable) warning for the second method bax().

While technically correct, IMHO it would be sane to make an exception for these kind of "optional overload" (empty) methods in abstract classes.

jrfnl avatar Feb 13 '20 02:02 jrfnl

I agree. Maybe something like "if the variable is an argument of a function that is a method of an abstract class and the method body is empty, then do not mark unused".

sirbrillig avatar Feb 13 '20 23:02 sirbrillig

Just thinking - maybe it should be: ""if the variable is an argument of a function that is a method of an abstract class and the method body is empty or only contains return;, then do not mark unused"."

jrfnl avatar Feb 14 '20 17:02 jrfnl

Indeed, I think I'm running into an abstract class method defined as abstract public function decompress( $compressed_item, $compressed_data_layer );, where we don't use $compressed_data_layer, but need to include it to match overload signature.

lkraav avatar Oct 16 '20 13:10 lkraav