Comments (5)
Thanks for the report!
It seems it has to do with array destructuring the result, because if changed to $bar it disappears.
I think you're right because this passes:
static function foo(DBC $db, int $notificationId): void {
list($bar) = $db->queryGetRowIndexed('SELECT blah... ', [ 'n.notificationId' => $notificationId ]);
echo $bar;
}
Whereas this fails:
static function foo(DBC $db, int $notificationId): void {
[$bar] = $db->queryGetRowIndexed('SELECT blah... ', [ 'n.notificationId' => $notificationId ]);
echo $bar;
}
Even though both are destructuring. What's really weird is that the warning is Unused function parameter $notificationId.
which has nothing to do with $bar
.
Here's a more minimal case with the same bug:
function foo(int $baz) {
[$bar] = doSomething([$baz]);
return $bar;
}
Investigating...
from phpcs-variable-analysis.
#318 should fix the issue you found, although you'll still get an unused variable warning in your first example unless you do something with $bar
like print it or return it since otherwise $bar
is not used.
As for the second issue,
function dotToMultiArray( array $array ): array {
$multi = [];
foreach($array as $key => $value){
$level = &$multi; // $level is assigned
foreach(explode(".", $key) as $node)
$level = &$level[$node]; // $level is assigned again
$level = $value; // $level is assigned a third time
}
return $multi; // We ignore $level and return $multi
}
In this case, $level
is marked as unused because it is. It's only ever assigned a value but that value is never used for anything. Maybe I'm misunderstanding something?
I think that example is equivalent to:
function dotToMultiArray( array $array ): array {
$multi = [];
return $multi;
}
from phpcs-variable-analysis.
...although you'll still get an unused variable warning in your first example unless you do something with $bar like print it or return it since otherwise $bar is not used.
Yes, the function wasn't complete and $bar
is used afterwards. Thanks for the quick fix!
Re the second case, $level
is indeed used, look carefully. It is used as a pointer to traverse a multi-dimensional array (first and second) and set the value of one of its nodes (third). Probably the assignments by reference are confusing the linter.
from phpcs-variable-analysis.
Interesting... Does that actually work to run $level = $value
and assign to a sub element of $multi
? I thought that assigning directly to the variable would overwrite the reference itself? Is it because the reference is pointing to an array element and not the array?
from phpcs-variable-analysis.
Oh! It does. Wild. I made #319 to investigate that one.
from phpcs-variable-analysis.
Related Issues (20)
- Support of array desctructing HOT 2
- Incorrect warning for arguments to static methods that return "new static()" HOT 3
- Incorrect warning for arguments are thrown on static arrow functions HOT 1
- False-positive `UndefinedVariable` for static variable inside an anonymous function HOT 4
- Incorrect warning about redeclaration of function parameter as static variable HOT 2
- Variable $this is undefined in enum HOT 1
- Support constructor promotion with readonly properties (UnusedVariable) HOT 5
- method call called 'enum' causes an "Cannot find enum start at position" error HOT 2
- Unused function parameter in lambda function HOT 4
- Ternary operator inside arrow function HOT 3
- Incorrect warning Variable $var is undefined. HOT 6
- Undefined variable error introduced in v2.11.15 HOT 2
- Feature Request: don't report on unused parameters for deprecated functions
- Variable reported as unused on an assignment by reference HOT 2
- Assignment of an array element counts as a read
- `squizlabs/PHP_CodeSniffer` is abandoned - Replace with `PHPCSStandards/PHP_CodeSniffer` when the time is right HOT 3
- Assignment to array element reference does not count as a write
- Handle constructor promotion with Nullable types HOT 2
- Variable not marked as used in short open echo tag
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpcs-variable-analysis.