Skip to content

Commit

Permalink
Convert validUnusedVariableNames to array late
Browse files Browse the repository at this point in the history
Previously the property was converted to an array when the Sniff was
registered. This could cause issues if the property was changed (I know
it's unlikely but it can happen in tests pretty easily) after
registration. Furthermore, it was convered to an array and then used to
replace the original string property, which introduces risk by both
mutating a property during an object lifecycle and even changing its type.

With this change, we never change the property from a string, and
instead we convert it to an array just before it is used.
  • Loading branch information
sirbrillig committed Apr 20, 2018
1 parent 45e0466 commit 1552b4b
Showing 1 changed file with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ class VariableAnalysisSniff implements Sniff {
public $validUnusedVariableNames = null;

public function register() {
if (!empty($this->validUnusedVariableNames)) {
$this->validUnusedVariableNames =
preg_split('/\s+/', trim($this->validUnusedVariableNames));
}
return [
T_VARIABLE,
T_DOUBLE_QUOTED_STRING,
Expand Down Expand Up @@ -124,7 +120,10 @@ protected function getOrCreateVariableInfo($varName, $currScope) {
$scopeInfo = $this->getOrCreateScopeInfo($currScope);
if (!isset($scopeInfo->variables[$varName])) {
$scopeInfo->variables[$varName] = new VariableInfo($varName);
if ($this->validUnusedVariableNames && in_array($varName, $this->validUnusedVariableNames)) {
$validUnusedVariableNames = (empty($this->validUnusedVariableNames))
? []
: preg_split('/\s+/', trim($this->validUnusedVariableNames));
if (in_array($varName, $validUnusedVariableNames)) {
$scopeInfo->variables[$varName]->ignoreUnused = true;
}
}
Expand Down

0 comments on commit 1552b4b

Please sign in to comment.