Skip to content
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

Treat writing to a by-reference foreach loop variable as a read #221

Merged
merged 5 commits into from
Dec 30, 2020
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
3 changes: 3 additions & 0 deletions Tests/VariableAnalysisSniff/VariableAnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ public function testFunctionWithReferenceWarnings() {
59,
60,
64,
81,
];
$this->assertEquals($expectedWarnings, $lines);
}
Expand All @@ -312,6 +313,7 @@ public function testFunctionWithReferenceWarningsAllowsCustomFunctions() {
40,
46,
64,
81,
];
$this->assertEquals($expectedWarnings, $lines);
}
Expand Down Expand Up @@ -339,6 +341,7 @@ public function testFunctionWithReferenceWarningsAllowsWordPressFunctionsIfSet()
46,
59,
60,
81,
];
$this->assertEquals($expectedWarnings, $lines);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,13 @@ function function_with_array_walk($userNameParts) {
$value = ucfirst($value);
});
}

function function_with_foreach_with_reference($derivatives, $base_plugin_definition) {
foreach ($derivatives as &$entry) {
$entry .= $base_plugin_definition;
}
foreach ($derivatives as &$unused) { // unused variable
$base_plugin_definition .= '1';
}
return $derivatives;
}
7 changes: 7 additions & 0 deletions VariableAnalysis/Lib/VariableInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class VariableInfo {
*/
public $referencedVariableScope;

/**
* True if the variable is a reference but one created at runtime
*
* @var bool
*/
public $isDynamicReference = false;

/**
* Stack pointer of first declaration
*
Expand Down
16 changes: 16 additions & 0 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ protected function markVariableAssignment($varName, $stackPtr, $currScope) {
return;
}
$varInfo->firstInitialized = $stackPtr;
Helpers::debug('markVariableAssignment: marked as initialized', $varName);
}

/**
Expand Down Expand Up @@ -944,6 +945,14 @@ protected function processVariableAsAssignment(File $phpcsFile, $stackPtr, $varN

Helpers::debug('processVariableAsAssignment: marking as assignment in scope', $currScope);
$this->markVariableAssignment($varName, $stackPtr, $currScope);

// If the left-hand-side of the assignment (the variable we are examining)
// is itself a reference, then that counts as a read as well as a write.
$varInfo = $this->getOrCreateVariableInfo($varName, $currScope);
if ($varInfo->isDynamicReference) {
Helpers::debug('processVariableAsAssignment: also marking as a use because variable is a reference');
$this->markVariableRead($varName, $stackPtr, $currScope);
}
}

/**
Expand Down Expand Up @@ -1164,6 +1173,13 @@ protected function processVariableAsForeachLoopVar(File $phpcsFile, $stackPtr, $
$varInfo->isForeachLoopAssociativeValue = true;
}

// Are we pass-by-reference?
$referencePtr = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true, null, true);
if (($referencePtr !== false) && ($tokens[$referencePtr]['code'] === T_BITWISE_AND)) {
Helpers::debug("processVariableAsForeachLoopVar: found foreach loop variable assigned by reference");
$varInfo->isDynamicReference = true;
}

return true;
}

Expand Down