PHPCSStandards / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
906 stars 55 forks source link

QA: review stray variables after loops #521

Open jrfnl opened 4 months ago

jrfnl commented 4 months ago

Inspired by the conversation in this PR review thread.

PHPCS uses for/foreach/while loops a lot and quite often the variables created for those loops are not destroyed after the loop has finished.

This can cause confusion is the variable name is re-used for something else later on in the same code flow. It can also cause problems if a variable name is re-used and conditionally set later on in the same code flow as the wrong variable may end up being used.

To prevent this it would be good to review all code for these code patterns and to add unset()s where appropriate.

Examples:

for ($i = $stackPtr; $i < $phpcsFile->numTokens; $i++) {
    // Do something...
}
+unset($i);
foreach ($patterns as $pattern => $type) {
    // Do something...
}
+unset($pattern, $type);
while (($ptr = $phpcsFile->findNext(Tokens::$ooScopeTokens, ($ptr + 1)) !== false) {
    // Do something...
}
+unset($ptr);

Notes:

Planning and how to contribute

This task doesn't need to be done in one go. Anyone who wants to contribute to this task can claim a single file, or a group of files to review by leaving a comment in this thread.

We'll keep track of what's (being) done by listing what's been claimed/reviewed below.

Claimed

Nothing yet

Done

Nothing yet

Excluded from this task