forked from vimeo/psalm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for references and improve UnusedVariable checks (fixes v…
- Loading branch information
1 parent
d52c3f0
commit bcc31ca
Showing
33 changed files
with
954 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
docs/running_psalm/issues/ReferenceReusedFromConfusingScope.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# ReferenceReusedFromConfusingScope | ||
|
||
Emitted when a reference assigned in a potentially confusing scope is reused later. | ||
Since PHP doesn't scope loops and ifs the same way most other languages do, a common issue is the reuse of a variable | ||
declared in such a scope. Usually it doesn't matter, because a reassignment to an already-defined variable will just | ||
reassign it, but if the already-defined variable is a reference it will change the value of the referred to variable. | ||
|
||
```php | ||
<?php | ||
|
||
$arr = [1, 2, 3]; | ||
foreach ($arr as &$i) { | ||
++$i; | ||
} | ||
|
||
// ...more code, after which the usage of $i as a reference has been forgotten by the programmer | ||
|
||
for ($i = 0; $i < 10; ++$i) { | ||
echo $i; | ||
} | ||
// $arr is now [2, 3, 10] instead of the expected [2, 3, 4] | ||
``` | ||
|
||
To fix the issue, unset the reference after using it in such a scope: | ||
|
||
```php | ||
<?php | ||
|
||
$arr = [1, 2, 3]; | ||
foreach ($arr as &$i) { | ||
++$i; | ||
} | ||
unset($i); | ||
|
||
for ($i = 0; $i < 10; ++$i) { | ||
echo $i; | ||
} | ||
// $arr is correctly [2, 3, 4] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.