Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Recognize that variable vars are not assignments #88

Merged
merged 2 commits into from
May 10, 2019
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
22 changes: 22 additions & 0 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ protected function checkForAssignment(File $phpcsFile, $stackPtr, $varName, $cur
return false;
}

// Is this a variable variable? If so, it's not an assignment to the current variable.
if ($this->checkForVariableVariable($phpcsFile, $stackPtr, $varName, $currScope)) {
Helpers::debug('found variable variable');
return false;
}

// Plain ol' assignment. Simpl(ish).
$writtenPtr = Helpers::findWhereAssignExecuted($phpcsFile, $assignPtr);
if ($writtenPtr === false) {
Expand All @@ -526,6 +532,22 @@ protected function checkForAssignment(File $phpcsFile, $stackPtr, $varName, $cur
return true;
}

protected function checkForVariableVariable(File $phpcsFile, $stackPtr, $varName, $currScope) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];

if (!isset($tokens[$stackPtr - 1]['code'])) {
return false;
}
if ($tokens[$stackPtr - 1]['code'] === T_DOLLAR) {
return true;
}
if ($tokens[$stackPtr - 1]['code'] === T_OPEN_CURLY_BRACKET && isset($tokens[$stackPtr - 2]['code']) && $tokens[$stackPtr - 2]['code'] === T_DOLLAR) {
return true;
}
return false;
}

protected function checkForListShorthandAssignment(File $phpcsFile, $stackPtr, $varName, $currScope) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
Expand Down
24 changes: 24 additions & 0 deletions VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,30 @@ public function testVariableFunctionCallsCountAsUsage() {
$this->assertEquals($expectedErrors, $lines);
}

public function testVariableVariables() {
$fixtureFile = $this->getFixture('VariableVariablesFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
4,
10,
16,
22,
29,
35,
41,
47,
48,
52,
53,
];
$this->assertEquals($expectedWarnings, $lines);
$lines = $this->getErrorLineNumbersFromFile($phpcsFile);
$expectedErrors = [];
$this->assertEquals($expectedErrors, $lines);
}

public function testTraitsAllowPropertyDefinitions() {
$fixtureFile = $this->getFixture('TraitWithPropertiesFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

function usedVariableVariableInReturn() {
$foo = true; // this is used but it requires execution to know that
$varName = 'foo';
return $$varName;
}

function usedVariableVariableInEcho() {
$foo = true; // this is used but it requires execution to know that
$varName = 'foo';
echo $$varName;
}

function usedVariableVariableInLeftAssignment() {
$foo = true; // the below is assignment, not a read, so this should be a warning
$marName = 'foo';
$$marName = false;
}

function usedVariableVariableInRightAssignment() {
$foo = true; // this is used but it requires execution to know that
$varName = 'foo';
$bar = $$varName;
echo $bar;
}

function usedVariableVariableInEchoWithBraces() {
$foo = true; // this is used but it requires execution to know that
$varName = 'foo';
echo ${$varName};
}

function usedVariableVariableInLeftAssignmentWithBraces() {
$foo = true; // this is used but it requires execution to know that
$varName = 'foo';
${$varName} = false;
}

function usedVariableVariableInEchoWithBracesInString() {
$foo = true; // this is used but it requires execution to know that
$varName = 'foo';
echo "${$varName}";
}

function undefinedVariableVariableInEcho() {
$foo = true; // this should be a warning
echo $$varName; // this should be a warning
}

function usedVariableVariableTwoLevels() {
$foo = 'hello'; // this is used but it requires execution to know that
$varNameOne = 'foo'; // this is used but it requires execution to know that
$varNameTwo = 'varNameOne';
return $$$varNameTwo;
}