Skip to content

Commit

Permalink
Ignore numeric variables (#61)
Browse files Browse the repository at this point in the history
* Tests: Add test for numeric variables

* Ignore numeric variables
  • Loading branch information
sirbrillig authored Jan 2, 2019
1 parent aae12c8 commit 14cb29e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,10 @@ protected function checkForStaticDeclaration(File $phpcsFile, $stackPtr, $varNam
return true;
}

protected function checkForNumericVariable($varName) {
return is_numeric(substr($varName, 0, 1));
}

protected function checkForForeachLoopVar(File $phpcsFile, $stackPtr, $varName, $currScope) {
$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr];
Expand Down Expand Up @@ -827,6 +831,11 @@ protected function processVariable(File $phpcsFile, $stackPtr) {
return;
}

// Are we a numeric variable used for constructs like preg_replace?
if ($this->checkForNumericVariable($varName)) {
return;
}

// OK, we don't appear to be a write to the var, assume we're a read.
$this->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $currScope);
}
Expand Down Expand Up @@ -855,9 +864,16 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) {
if ($this->checkForThisWithinClass($phpcsFile, $stackPtr, $varName, $currScope)) {
continue;
}

if ($this->checkForSuperGlobal($phpcsFile, $stackPtr, $varName, $currScope)) {
continue;
}

// Are we a numeric variable used for constructs like preg_replace?
if ($this->checkForNumericVariable($varName)) {
continue;
}

$this->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $currScope);
}
}
Expand Down
12 changes: 12 additions & 0 deletions VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,4 +688,16 @@ public function testUnusedArgumentsBeforeUsedArgumentsAreIgnoredByDefault() {
];
$this->assertEquals($expectedWarnings, $lines);
}

public function testPregReplaceIgnoresNumericVariables() {
$fixtureFile = $this->getFixture('PregReplaceFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
15,
20,
];
$this->assertEquals($expectedWarnings, $lines);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

function doPregReplaceWithIgnoredVariable($subject) {
$selector = 'good morning';
return preg_replace('/(hello \w+)/', "$selector$1", $subject, 1);
}

function doPregReplaceWithZeroIgnoredVariable($subject) {
$selector = 'good morning';
return preg_replace('/(hello \w+)/', "$selector$0", $subject, 1);
}

function doPregReplaceWithNonIgnoredVariable($subject) {
$selector = 'good morning';
return preg_replace('/(hello \w+)/', "$selector$bad", $subject, 1);
}

function doPregReplaceWithNonIgnoredAndIgnoredVariable($subject) {
$selector = 'good morning';
return preg_replace('/(hello \w+)/', "$selector$1$bad", $subject, 1);
}

0 comments on commit 14cb29e

Please sign in to comment.