Skip to content

Commit 14cb29e

Browse files
authored
Ignore numeric variables (#61)
* Tests: Add test for numeric variables * Ignore numeric variables
1 parent aae12c8 commit 14cb29e

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,10 @@ protected function checkForStaticDeclaration(File $phpcsFile, $stackPtr, $varNam
608608
return true;
609609
}
610610

611+
protected function checkForNumericVariable($varName) {
612+
return is_numeric(substr($varName, 0, 1));
613+
}
614+
611615
protected function checkForForeachLoopVar(File $phpcsFile, $stackPtr, $varName, $currScope) {
612616
$tokens = $phpcsFile->getTokens();
613617
$token = $tokens[$stackPtr];
@@ -827,6 +831,11 @@ protected function processVariable(File $phpcsFile, $stackPtr) {
827831
return;
828832
}
829833

834+
// Are we a numeric variable used for constructs like preg_replace?
835+
if ($this->checkForNumericVariable($varName)) {
836+
return;
837+
}
838+
830839
// OK, we don't appear to be a write to the var, assume we're a read.
831840
$this->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $currScope);
832841
}
@@ -855,9 +864,16 @@ protected function processVariableInString(File $phpcsFile, $stackPtr) {
855864
if ($this->checkForThisWithinClass($phpcsFile, $stackPtr, $varName, $currScope)) {
856865
continue;
857866
}
867+
858868
if ($this->checkForSuperGlobal($phpcsFile, $stackPtr, $varName, $currScope)) {
859869
continue;
860870
}
871+
872+
// Are we a numeric variable used for constructs like preg_replace?
873+
if ($this->checkForNumericVariable($varName)) {
874+
continue;
875+
}
876+
861877
$this->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $currScope);
862878
}
863879
}

VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,4 +688,16 @@ public function testUnusedArgumentsBeforeUsedArgumentsAreIgnoredByDefault() {
688688
];
689689
$this->assertEquals($expectedWarnings, $lines);
690690
}
691+
692+
public function testPregReplaceIgnoresNumericVariables() {
693+
$fixtureFile = $this->getFixture('PregReplaceFixture.php');
694+
$phpcsFile = $this->prepareLocalFileForSniffs($this->getSniffFiles(), $fixtureFile);
695+
$phpcsFile->process();
696+
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
697+
$expectedWarnings = [
698+
15,
699+
20,
700+
];
701+
$this->assertEquals($expectedWarnings, $lines);
702+
}
691703
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
function doPregReplaceWithIgnoredVariable($subject) {
4+
$selector = 'good morning';
5+
return preg_replace('/(hello \w+)/', "$selector$1", $subject, 1);
6+
}
7+
8+
function doPregReplaceWithZeroIgnoredVariable($subject) {
9+
$selector = 'good morning';
10+
return preg_replace('/(hello \w+)/', "$selector$0", $subject, 1);
11+
}
12+
13+
function doPregReplaceWithNonIgnoredVariable($subject) {
14+
$selector = 'good morning';
15+
return preg_replace('/(hello \w+)/', "$selector$bad", $subject, 1);
16+
}
17+
18+
function doPregReplaceWithNonIgnoredAndIgnoredVariable($subject) {
19+
$selector = 'good morning';
20+
return preg_replace('/(hello \w+)/', "$selector$1$bad", $subject, 1);
21+
}

0 commit comments

Comments
 (0)