Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Please note that this README is for VariableAnalysis v3. For documentation about

- Warns if variables are used without being defined. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable`)
- Warns if variables are used for an array push shortcut without being defined. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedArrayVariable`)
- Warns if variables are used inside `unset()` without being defined. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedUnsetVariable`)
- Warns if variables are set or declared but never used. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable`)
- Warns if function parameters are declared but never used. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedParameter`)
- Warns if function parameters are declared but never used before other parameters that are used. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedParameterBeforeUsed`)
Expand Down
29 changes: 29 additions & 0 deletions Tests/VariableAnalysisSniff/UnsetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace VariableAnalysis\Tests\VariableAnalysisSniff;

use VariableAnalysis\Tests\BaseTestCase;

class UnsetTest extends BaseTestCase {
public function testUnsetReportsUndefinedVariables() {
$fixtureFile = $this->getFixture('UnsetFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
// Technically, these are not illegal, but they may be typos. See https://github.com/sirbrillig/phpcs-variable-analysis/issues/174
$expectedWarnings = [
6,
11,
];
$this->assertEquals($expectedWarnings, $lines);
}

public function testUnsetHasCorrectSniffCodes() {
$fixtureFile = $this->getFixture('UnsetFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->process();

$warnings = $phpcsFile->getWarnings();
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedUnsetVariable', $warnings[6][7][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedUnsetVariable', $warnings[11][9][0]['source']);
}
}
12 changes: 12 additions & 0 deletions Tests/VariableAnalysisSniff/fixtures/UnsetFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$foo = 'hello';

unset($foo);
unset($bar); // undefined variable $bar

function unset_loop($array) {
foreach ($array as $value) {
}
unset($key, $value); // undefined variable $key
}
21 changes: 21 additions & 0 deletions VariableAnalysis/Lib/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,4 +830,25 @@ public static function isVariableArrayPushShortcut(File $phpcsFile, $stackPtr) {

return true;
}

/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return bool
*/
public static function isVariableInsideUnset(File $phpcsFile, $stackPtr) {
$functionIndex = self::getFunctionIndexForFunctionCallArgument($phpcsFile, $stackPtr);
if (! is_int($functionIndex)) {
return false;
}
$tokens = $phpcsFile->getTokens();
if (! isset($tokens[$functionIndex])) {
return false;
}
if ($tokens[$functionIndex]['content'] === 'unset') {
return true;
}
return false;
}
}
24 changes: 24 additions & 0 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,19 @@ protected function markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $sta
$this->markVariableRead($varName, $stackPtr, $currScope);
if ($this->isVariableUndefined($varName, $stackPtr, $currScope) === true) {
Helpers::debug("variable $varName looks undefined");

if (Helpers::isVariableArrayPushShortcut($phpcsFile, $stackPtr)) {
$this->warnAboutUndefinedArrayPushShortcut($phpcsFile, $varName, $stackPtr);
// Mark the variable as defined if it's of the form `$x[] = 1;`
$this->markVariableAssignment($varName, $stackPtr, $currScope);
return;
}

if (Helpers::isVariableInsideUnset($phpcsFile, $stackPtr)) {
$this->warnAboutUndefinedUnset($phpcsFile, $varName, $stackPtr);
return;
}

$this->warnAboutUndefinedVariable($phpcsFile, $varName, $stackPtr);
}
}
Expand Down Expand Up @@ -1773,6 +1780,7 @@ protected function warnAboutUndefinedVariable(File $phpcsFile, $varName, $stackP
["\${$varName}"]
);
}

/**
* @param File $phpcsFile
* @param string $varName
Expand All @@ -1788,4 +1796,20 @@ protected function warnAboutUndefinedArrayPushShortcut(File $phpcsFile, $varName
["\${$varName}"]
);
}

/**
* @param File $phpcsFile
* @param string $varName
* @param int $stackPtr
*
* @return void
*/
protected function warnAboutUndefinedUnset(File $phpcsFile, $varName, $stackPtr) {
$phpcsFile->addWarning(
"Variable %s inside unset call is undefined.",
$stackPtr,
'UndefinedUnsetVariable',
["\${$varName}"]
);
}
}