A typical code smell known as unused function parameters refers to parameters declared in a function but not used anywhere within the function’s body. While this might seem harmless at first glance, it can lead to confusion and potential errors in your code. Disregarding the values passed to such parameters, the function’s behavior will be the same, but the programmer’s intention won’t be clearly expressed anymore. Therefore, removing function parameters that are not being utilized is considered best practice.
Functions in classes that override a class or implement interfaces are ignored.
class C extends B {
function doSomething($a, $b) { // no issue reported on $b
compute($a);
}
}
Additionally, methods with specific Magento plugin signatures are ignored because Magento requires these parameters:
before and having at least 3 parameters around and having at least 4 parameters after and having at least 2 parameters
class ExamplePlugin {
public function beforeAction($subject, $arg1, $arg2) {
// no issue on unused parameters, required by Magento plugin signature
return;
}
public function aroundAction($subject, callable $proceed, $arg1, $arg2) {
// no issue on unused parameters, required by Magento plugin signature
return;
}
public function afterAction($subject, $result) {
// no issue on unused parameters, required by Magento plugin signature
return;
}
}
Having unused function parameters in your code can lead to confusion and misunderstanding of a developer’s intention. They reduce code readability and introduce the potential for errors. To avoid these problems, developers should remove unused parameters from function declarations.
function doSomething($a, $b) { // "$a" is unused
return compute($b);
}
function doSomething($b) {
return compute($b);
}