From 0f118deecfe69a459df39c6d159d9f8ec1f1f55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Feh=C3=A9r=20Zolt=C3=A1n?= Date: Tue, 12 Apr 2016 15:52:31 +0200 Subject: [PATCH 1/5] added: allowUndefinedFunctionParameters --- Sniffs/CodeAnalysis/VariableAnalysisSniff.php | 38 ++++++++++++------- ruleset.xml | 10 +++++ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index e822ad3..c218f9f 100644 --- a/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -330,7 +330,7 @@ class Generic_Sniffs_CodeAnalysis_VariableAnalysisSniff implements PHP_CodeSniff 'yaz_hits' => array(2), 'yaz_scan_result' => array(2), 'yaz_wait' => array(1), - ); + ); /** * Allows an install to extend the list of known pass-by-reference functions @@ -356,6 +356,8 @@ class Generic_Sniffs_CodeAnalysis_VariableAnalysisSniff implements PHP_CodeSniff */ public $validUnusedVariableNames = null; + public $validUndefinedVariableNames = null; + /** * Returns an array of tokens this test wants to listen for. * @@ -373,6 +375,12 @@ public function register() { $this->validUnusedVariableNames = preg_split('/\s+/', trim($this->validUnusedVariableNames)); } + + if (!empty($this->validUndefinedVariableNames)) { + $this->validUndefinedVariableNames = + preg_split('/\s+/', trim($this->validUndefinedVariableNames)); + } + return array( T_VARIABLE, T_DOUBLE_QUOTED_STRING, @@ -429,7 +437,7 @@ function scopeKey($currScope) { $currScope = 'file'; } return ($this->currentFile ? $this->currentFile->getFilename() : 'unknown file') . - ':' . $currScope; + ':' . $currScope; } // Warning: this is an autovivifying get @@ -487,8 +495,8 @@ function markVariableDeclaration($varName, $scopeType, $typeHint, $stackPtr, $cu VariableInfo::$scopeTypeDescriptions[$varInfo->scopeType], "\${$varName}", VariableInfo::$scopeTypeDescriptions[$scopeType], - ) - ); + ) + ); } } $varInfo->scopeType = $scopeType; @@ -534,9 +542,11 @@ function markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $cu if ($this->isVariableUndefined($varName, $stackPtr, $currScope) === true) { // We haven't been defined by this point. - $phpcsFile->addWarning("Variable %s is undefined.", $stackPtr, - 'UndefinedVariable', - array("\${$varName}")); + if ($this->validUndefinedVariableNames && !in_array($varName, $this->validUndefinedVariableNames)) { + $phpcsFile->addWarning("Variable %s is undefined.", $stackPtr, + 'UndefinedVariable', + array("\${$varName}")); + } } return true; } @@ -746,7 +756,7 @@ protected function checkForFunctionPrototype( $openPtr - 1, null, true, null, true); if (($functionPtr !== false) && (($tokens[$functionPtr]['code'] === T_FUNCTION) || - ($tokens[$functionPtr]['code'] === T_CLOSURE))) { + ($tokens[$functionPtr]['code'] === T_CLOSURE))) { // TODO: typeHint $this->markVariableDeclaration($varName, 'param', null, $stackPtr, $functionPtr); // Are we pass-by-reference? @@ -871,7 +881,7 @@ protected function checkForSuperGlobal( '_ENV', 'argv', 'argc', - ))) { + ))) { return true; } @@ -1047,7 +1057,7 @@ protected function checkForStaticDeclaration( T_DOUBLE_COLON, T_START_HEREDOC, T_HEREDOC, T_END_HEREDOC, T_START_NOWDOC, T_NOWDOC, T_END_NOWDOC, - ), + ), $stackPtr - 1, null, true, null, true); if (($staticPtr === false) || ($tokens[$staticPtr]['code'] !== T_STATIC)) { //if ($varName == 'static4') { @@ -1477,8 +1487,8 @@ protected function processScopeClose( array( VariableInfo::$scopeTypeDescriptions[$varInfo->scopeType], "\${$varInfo->name}", - ) - ); + ) + ); } if (isset($varInfo->firstInitialized)) { $phpcsFile->addWarning( @@ -1488,8 +1498,8 @@ protected function processScopeClose( array( VariableInfo::$scopeTypeDescriptions[$varInfo->scopeType], "\${$varInfo->name}", - ) - ); + ) + ); } } } diff --git a/ruleset.xml b/ruleset.xml index efa66ce..7568004 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -59,6 +59,16 @@ dummy "/> + + From bb8094d0f7f6f5558c3b631cd040a0eab67e492d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Feh=C3=A9r=20Zolt=C3=A1n?= Date: Tue, 12 Apr 2016 15:52:56 +0200 Subject: [PATCH 2/5] added: composer.json --- composer.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..e9fa23f --- /dev/null +++ b/composer.json @@ -0,0 +1,9 @@ +{ + "name": "ingatlancom/variable-analysis", + "description": "Provides PHP CodeSniffer sniffs to find unused and undefined variables.", + "require": { + "php": ">=5.3.0", + "squizlabs/php_codesniffer": ">=1.5.3" + }, + "license": "BSD" +} \ No newline at end of file From 4f943b5422de3a00d4bdbb7d9b13deca334b9c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Feh=C3=A9r=20Zolt=C3=A1n?= Date: Tue, 12 Apr 2016 18:55:43 +0200 Subject: [PATCH 3/5] fixed: now works without the list of validUndefinedVariableNames --- Sniffs/CodeAnalysis/VariableAnalysisSniff.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index c218f9f..61b2cd1 100644 --- a/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -356,6 +356,10 @@ class Generic_Sniffs_CodeAnalysis_VariableAnalysisSniff implements PHP_CodeSniff */ public $validUnusedVariableNames = null; + /** + * A list of names of variables that you want to ignore from + * undefined variable warnings + */ public $validUndefinedVariableNames = null; /** @@ -542,7 +546,7 @@ function markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $cu if ($this->isVariableUndefined($varName, $stackPtr, $currScope) === true) { // We haven't been defined by this point. - if ($this->validUndefinedVariableNames && !in_array($varName, $this->validUndefinedVariableNames)) { + if (!$this->validUndefinedVariableNames || !in_array($varName, $this->validUndefinedVariableNames)) { $phpcsFile->addWarning("Variable %s is undefined.", $stackPtr, 'UndefinedVariable', array("\${$varName}")); From 79e217edd6e48556348fe9557e451f459920e3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Feh=C3=A9r=20Zolt=C3=A1n?= Date: Tue, 12 Apr 2016 18:59:49 +0200 Subject: [PATCH 4/5] fixed: ruleset example --- ruleset.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ruleset.xml b/ruleset.xml index 7568004..86ccc15 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -58,17 +58,17 @@ junk dummy "/> - - + From 8bbac3f714bbb3d02b73fa4ec96a01192b2c0c63 Mon Sep 17 00:00:00 2001 From: Zoltan Feher Date: Mon, 5 Mar 2018 12:57:55 +0100 Subject: [PATCH 5/5] spdx-compatible license --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index e9fa23f..536ef1b 100644 --- a/composer.json +++ b/composer.json @@ -5,5 +5,5 @@ "php": ">=5.3.0", "squizlabs/php_codesniffer": ">=1.5.3" }, - "license": "BSD" -} \ No newline at end of file + "license": "BSD-2-Clause" +}