1.2.469. Fn Argument Variable Confusion¶
Avoid using local variables as arrow function arguments.
When a local variable name is used as an argument’s name in an arrow function, the local variable from the original scope is not imported. They are now two distinct variables.
When the local variable is not listed as argument, it is then imported in the arrow function.
<?php
function foo() {
$locale = 1;
// Actually ignores the argument, and returns the local variable ``$locale``.
$fn2 = fn ($argument) => $locale;
// Seems similar to above, but returns the incoming argument
$fn2 = fn ($locale) => $locale;
}
?>
See also Arrow functions.
1.2.469.1. Connex PHP features¶
1.2.469.1.1. Suggestions¶
Change the name of the local variable
Change the name of the argument
1.2.469.1.2. Specs¶
Short name |
Functions/FnArgumentVariableConfusion |
Rulesets |
|
Exakat since |
2.1.0 |
PHP Version |
With PHP 7.4 and more recent |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |