Skip to content

Respect ignoring undefined variables in else blocks #239

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Jul 6, 2021
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
66 changes: 66 additions & 0 deletions Tests/VariableAnalysisSniff/IfConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ public function testIfConditionWarnings() {
101,
159,
166,
176,
179,
];
$this->assertEquals($expectedWarnings, $lines);
}

public function testIfConditionWarningsWithValidUndefinedVariableNames() {
$fixtureFile = $this->getFixture('FunctionWithIfConditionFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'validUndefinedVariableNames',
'second'
);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'true'
);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
15,
36,
47,
58,
70,
82,
98,
159,
166,
176,
179,
];
$this->assertEquals($expectedWarnings, $lines);
}
Expand Down Expand Up @@ -60,6 +93,39 @@ public function testInlineIfConditionWarnings() {
88,
130,
136,
152,
154,
];
$this->assertEquals($expectedWarnings, $lines);
}

public function testInlineIfConditionWarningsWithValidUndefinedVariableNames() {
$fixtureFile = $this->getFixture('FunctionWithInlineIfConditionFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'validUndefinedVariableNames',
'second'
);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'true'
);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
14,
34,
44,
54,
64,
74,
86,
130,
136,
152,
154,
];
$this->assertEquals($expectedWarnings, $lines);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,16 @@ function loopAndPushWithUndefinedArray($parts) {
}
return $suggestions;
}

function definedInsideElseIfBlockUndefinedInsideElseBlockDifferentName($first) {
$name = 'human';
if ($first) {
$words = "hello {$name}";
} elseif ($name) {
$third = true; // unused variable $third
} else {
$words = "bye {$name}";
echo $third; // undefined variable $third
}
echo $words;
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,14 @@ function ifElseConditionWithInlineAssignAndUseInsideElse() {
else
echo $q;
}

function definedInsideElseIfBlockUndefinedInsideElseBlockDifferentName($first) {
$name = 'human';
if ($first)
$words = "hello {$name}";
elseif ($name)
$third = true; // unused variable $third
else
echo $third; // undefined variable $third
echo $words;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1518,8 +1518,10 @@ protected function processVaribleInsideElse(File $phpcsFile, $stackPtr, $varName
}

if (count($assignmentsInsideAttachedBlocks) === count($allAssignmentIndices)) {
Helpers::debug("variable $varName inside else looks undefined");
$this->warnAboutUndefinedVariable($phpcsFile, $varName, $stackPtr);
if (! $varInfo->ignoreUndefined) {
Helpers::debug("variable $varName inside else looks undefined");
$this->warnAboutUndefinedVariable($phpcsFile, $varName, $stackPtr);
}
return;
}

Expand Down