From 0198042f981c4771796d591930d4fe466f25bfc7 Mon Sep 17 00:00:00 2001 From: Ruben Gomez Date: Wed, 13 May 2020 13:53:40 +0200 Subject: [PATCH 1/2] Added property validUndefinedVariableRegexp to VariableAnalysisSniff --- .../Sniffs/CodeAnalysis/VariableAnalysisSniff.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index 265a72a4..4759e24d 100644 --- a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -90,6 +90,15 @@ class VariableAnalysisSniff implements Sniff { */ public $validUndefinedVariableNames = null; + /** + * A PHP regexp string for variables that you want to ignore from undefined + * variable warnings. For example, to ignore the variables `$_junk` and + * `$_unused`, this could be set to `'/^_/'`. + * + * @var string|null + */ + public $validUndefinedVariableRegexp= null; + /** * Allows unused arguments in a function definition if they are * followed by an argument which is used. @@ -264,6 +273,9 @@ protected function getOrCreateVariableInfo($varName, $currScope) { if (in_array($varName, $validUndefinedVariableNames)) { $scopeInfo->variables[$varName]->ignoreUndefined = true; } + if (isset($this->validUndefinedVariableRegexp) && preg_match($this->validUndefinedVariableRegexp, $varName) === 1) { + $scopeInfo->variables[$varName]->ignoreUndefined = true; + } } return $scopeInfo->variables[$varName]; } From b6cb8767d4b257505280fdefb9e6eb8bb3d60d09 Mon Sep 17 00:00:00 2001 From: Ruben Gomez Date: Sat, 6 Jun 2020 22:42:34 +0200 Subject: [PATCH 2/2] Tests: Add test for validUndefinedVariableRegexp --- .../VariableAnalysisTest.php | 19 +++++++++++++++++++ .../CodeAnalysis/VariableAnalysisSniff.php | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Tests/VariableAnalysisSniff/VariableAnalysisTest.php b/Tests/VariableAnalysisSniff/VariableAnalysisTest.php index e9cf2b11..c259a528 100644 --- a/Tests/VariableAnalysisSniff/VariableAnalysisTest.php +++ b/Tests/VariableAnalysisSniff/VariableAnalysisTest.php @@ -793,6 +793,25 @@ public function testValidUndefinedVariableNamesIgnoresUndefinedProperties() { $this->assertEquals($expectedWarnings, $lines); } + public function testValidUndefinedVariableRegexpIgnoresUndefinedProperties() { + $fixtureFile = $this->getFixture('ClassReferenceFixture.php'); + $phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile); + $phpcsFile->ruleset->setSniffProperty( + 'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff', + 'validUndefinedVariableRegexp', + '/^undefined_/' + ); + $phpcsFile->process(); + $lines = $this->getWarningLineNumbersFromFile($phpcsFile); + $expectedWarnings = [ + 12, + 13, + 24, + 25 + ]; + $this->assertEquals($expectedWarnings, $lines); + } + public function testUnusedArgumentsBeforeUsedArgumentsAreFoundIfFalse() { $fixtureFile = $this->getFixture('UnusedAfterUsedFixture.php'); $phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile); diff --git a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index 4759e24d..90ad91db 100644 --- a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -97,7 +97,7 @@ class VariableAnalysisSniff implements Sniff { * * @var string|null */ - public $validUndefinedVariableRegexp= null; + public $validUndefinedVariableRegexp = null; /** * Allows unused arguments in a function definition if they are