From ad2451fc4f6647f9fa80ef75bfa36b804cd844fa Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Wed, 5 Jun 2024 19:00:47 +0200 Subject: [PATCH] CS/QA: remove unused variables (#446) * test(phcs): Check for unused variables in PHPCS it self * Manually fixed all unused varaibles * Revert Slevomat dependency * Revert array_keys changes * revert changes that should stay --- src/Config.php | 2 +- src/Filters/Filter.php | 2 +- src/Runner.php | 4 ++-- .../Generic/Sniffs/Files/InlineHTMLSniff.php | 2 +- .../PHP/CharacterBeforePHPOpeningTagSniff.php | 2 +- .../Sniffs/Commenting/FunctionCommentSniff.php | 2 +- .../Sniffs/Commenting/VariableCommentSniff.php | 2 +- .../MultiLineFunctionDeclarationSniff.php | 1 - .../Squiz/Sniffs/PHP/CommentedOutCodeSniff.php | 17 ++++++----------- tests/Core/File/GetMemberPropertiesTest.php | 6 +++--- tests/Core/Ruleset/ExplainTest.php | 4 ++-- tests/Core/Ruleset/RuleInclusionTest.php | 2 -- tests/Core/Ruleset/SetSniffPropertyTest.php | 8 ++++---- .../Core/Tokenizer/PHP/BackfillFnTokenTest.php | 2 -- .../PHP/NamespacedNameSingleTokenTest.php | 2 +- .../PHP/StableCommentWhitespaceTest.php | 2 +- .../PHP/StableCommentWhitespaceWinTest.php | 2 +- tests/Standards/AllSniffs.php | 2 -- 18 files changed, 26 insertions(+), 38 deletions(-) diff --git a/src/Config.php b/src/Config.php index fd60a0822c..16788a3fe1 100644 --- a/src/Config.php +++ b/src/Config.php @@ -1550,7 +1550,7 @@ public function setConfigData($key, $value, $temp=false) // standards paths are added to the autoloader. if ($key === 'installed_paths') { $installedStandards = Standards::getInstalledStandardDetails(); - foreach ($installedStandards as $name => $details) { + foreach ($installedStandards as $details) { Autoload::addSearchPath($details['path'], $details['namespace']); } } diff --git a/src/Filters/Filter.php b/src/Filters/Filter.php index 5259345de5..4935e57b58 100644 --- a/src/Filters/Filter.php +++ b/src/Filters/Filter.php @@ -200,7 +200,7 @@ protected function shouldProcessFile($path) // complete extension list and make sure one is allowed. $extensions = []; array_shift($fileParts); - foreach ($fileParts as $part) { + while (empty($fileParts) === false) { $extensions[] = implode('.', $fileParts); array_shift($fileParts); } diff --git a/src/Runner.php b/src/Runner.php index 97b868f98b..507e97bffe 100644 --- a/src/Runner.php +++ b/src/Runner.php @@ -319,11 +319,11 @@ public function init() // Create this class so it is autoloaded and sets up a bunch // of PHP_CodeSniffer-specific token type constants. - $tokens = new Tokens(); + new Tokens(); // Allow autoloading of custom files inside installed standards. $installedStandards = Standards::getInstalledStandardDetails(); - foreach ($installedStandards as $name => $details) { + foreach ($installedStandards as $details) { Autoload::addSearchPath($details['path'], $details['namespace']); } diff --git a/src/Standards/Generic/Sniffs/Files/InlineHTMLSniff.php b/src/Standards/Generic/Sniffs/Files/InlineHTMLSniff.php index 1d79a0ea76..d90b893016 100644 --- a/src/Standards/Generic/Sniffs/Files/InlineHTMLSniff.php +++ b/src/Standards/Generic/Sniffs/Files/InlineHTMLSniff.php @@ -54,7 +54,7 @@ public function process(File $phpcsFile, $stackPtr) { // Allow a byte-order mark. $tokens = $phpcsFile->getTokens(); - foreach ($this->bomDefinitions as $bomName => $expectedBomHex) { + foreach ($this->bomDefinitions as $expectedBomHex) { $bomByteLength = (strlen($expectedBomHex) / 2); $htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength)); if ($htmlBomHex === $expectedBomHex && strlen($tokens[0]['content']) === $bomByteLength) { diff --git a/src/Standards/Generic/Sniffs/PHP/CharacterBeforePHPOpeningTagSniff.php b/src/Standards/Generic/Sniffs/PHP/CharacterBeforePHPOpeningTagSniff.php index 66f73860ed..61ff4f2f0e 100644 --- a/src/Standards/Generic/Sniffs/PHP/CharacterBeforePHPOpeningTagSniff.php +++ b/src/Standards/Generic/Sniffs/PHP/CharacterBeforePHPOpeningTagSniff.php @@ -56,7 +56,7 @@ public function process(File $phpcsFile, $stackPtr) if ($stackPtr > 0) { // Allow a byte-order mark. $tokens = $phpcsFile->getTokens(); - foreach ($this->bomDefinitions as $bomName => $expectedBomHex) { + foreach ($this->bomDefinitions as $expectedBomHex) { $bomByteLength = (strlen($expectedBomHex) / 2); $htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength)); if ($htmlBomHex === $expectedBomHex) { diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php index 6432baa126..0b19732e02 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php @@ -86,7 +86,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart) // Check return type (can be multiple, separated by '|'). $typeNames = explode('|', $returnType); $suggestedNames = []; - foreach ($typeNames as $i => $typeName) { + foreach ($typeNames as $typeName) { $suggestedName = Common::suggestType($typeName); if (in_array($suggestedName, $suggestedNames, true) === false) { $suggestedNames[] = $suggestedName; diff --git a/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php index 8c7cc663f5..6b36b1eca8 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php @@ -136,7 +136,7 @@ public function processMemberVar(File $phpcsFile, $stackPtr) // Check var type (can be multiple, separated by '|'). $typeNames = explode('|', $varType); $suggestedNames = []; - foreach ($typeNames as $i => $typeName) { + foreach ($typeNames as $typeName) { $suggestedName = Common::suggestType($typeName); if (in_array($suggestedName, $suggestedNames, true) === false) { $suggestedNames[] = $suggestedName; diff --git a/src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php b/src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php index d6a0eb5abf..0b6bbe8d53 100644 --- a/src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php @@ -215,7 +215,6 @@ public function processBracket($phpcsFile, $openBracket, $tokens, $type='functio }//end if // Each line between the brackets should contain a single parameter. - $lastComma = null; for ($i = ($openBracket + 1); $i < $closeBracket; $i++) { // Skip brackets, like arrays, as they can contain commas. if (isset($tokens[$i]['bracket_opener']) === true) { diff --git a/src/Standards/Squiz/Sniffs/PHP/CommentedOutCodeSniff.php b/src/Standards/Squiz/Sniffs/PHP/CommentedOutCodeSniff.php index 6255afc09b..d9e78ba6c5 100644 --- a/src/Standards/Squiz/Sniffs/PHP/CommentedOutCodeSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/CommentedOutCodeSniff.php @@ -224,23 +224,18 @@ public function process(File $phpcsFile, $stackPtr) ]; $emptyTokens += Tokens::$phpcsCommentTokens; - $numComment = 0; - $numPossible = 0; $numCode = 0; $numNonWhitespace = 0; for ($i = 0; $i < $numTokens; $i++) { - if (isset($emptyTokens[$stringTokens[$i]['code']]) === true) { - // Looks like comment. - $numComment++; - } else if (isset(Tokens::$comparisonTokens[$stringTokens[$i]['code']]) === true - || isset(Tokens::$arithmeticTokens[$stringTokens[$i]['code']]) === true - || $stringTokens[$i]['code'] === T_GOTO_LABEL - ) { + // Do not count comments. + if (isset($emptyTokens[$stringTokens[$i]['code']]) === false // Commented out HTML/XML and other docs contain a lot of these // characters, so it is best to not use them directly. - $numPossible++; - } else { + && isset(Tokens::$comparisonTokens[$stringTokens[$i]['code']]) === false + && isset(Tokens::$arithmeticTokens[$stringTokens[$i]['code']]) === false + && $stringTokens[$i]['code'] !== T_GOTO_LABEL + ) { // Looks like code. $numCode++; } diff --git a/tests/Core/File/GetMemberPropertiesTest.php b/tests/Core/File/GetMemberPropertiesTest.php index f8e73bc6fd..117c5fea0c 100644 --- a/tests/Core/File/GetMemberPropertiesTest.php +++ b/tests/Core/File/GetMemberPropertiesTest.php @@ -1147,7 +1147,7 @@ public function testNotClassPropertyException($identifier) $this->expectExceptionMessage('$stackPtr is not a class member var'); $variable = $this->getTargetToken($identifier, T_VARIABLE); - $result = self::$phpcsFile->getMemberProperties($variable); + self::$phpcsFile->getMemberProperties($variable); }//end testNotClassPropertyException() @@ -1184,8 +1184,8 @@ public function testNotAVariableException() $this->expectException('PHP_CodeSniffer\Exceptions\RuntimeException'); $this->expectExceptionMessage('$stackPtr must be of type T_VARIABLE'); - $next = $this->getTargetToken('/* testNotAVariable */', T_RETURN); - $result = self::$phpcsFile->getMemberProperties($next); + $next = $this->getTargetToken('/* testNotAVariable */', T_RETURN); + self::$phpcsFile->getMemberProperties($next); }//end testNotAVariableException() diff --git a/tests/Core/Ruleset/ExplainTest.php b/tests/Core/Ruleset/ExplainTest.php index b82ca2a739..c0b6ca8ec7 100644 --- a/tests/Core/Ruleset/ExplainTest.php +++ b/tests/Core/Ruleset/ExplainTest.php @@ -249,8 +249,8 @@ public function testExplainWillExplainEachStandardSeparately() $this->expectOutputString($expected); - $runner = new Runner(); - $exitCode = $runner->runPHPCS(); + $runner = new Runner(); + $runner->runPHPCS(); }//end testExplainWillExplainEachStandardSeparately() diff --git a/tests/Core/Ruleset/RuleInclusionTest.php b/tests/Core/Ruleset/RuleInclusionTest.php index e55ee94894..28ba7ef52a 100644 --- a/tests/Core/Ruleset/RuleInclusionTest.php +++ b/tests/Core/Ruleset/RuleInclusionTest.php @@ -435,8 +435,6 @@ public function testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFail { $this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs'); - $sniffObject = self::$ruleset->sniffs[$sniffClass]; - $hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName); $errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass); $this->assertFalse($hasProperty, $errorMsg); diff --git a/tests/Core/Ruleset/SetSniffPropertyTest.php b/tests/Core/Ruleset/SetSniffPropertyTest.php index ea8a9af98c..b4ed885f7c 100644 --- a/tests/Core/Ruleset/SetSniffPropertyTest.php +++ b/tests/Core/Ruleset/SetSniffPropertyTest.php @@ -145,7 +145,7 @@ public function testSetPropertyThrowsErrorOnInvalidProperty() // Set up the ruleset. $standard = __DIR__.'/SetPropertyThrowsErrorOnInvalidPropertyTest.xml'; $config = new ConfigDouble(["--standard=$standard"]); - $ruleset = new Ruleset($config); + new Ruleset($config); }//end testSetPropertyThrowsErrorOnInvalidProperty() @@ -169,7 +169,7 @@ public function testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute() // Set up the ruleset. $standard = __DIR__.'/SetPropertyNotAllowedViaAttributeTest.xml'; $config = new ConfigDouble(["--standard=$standard"]); - $ruleset = new Ruleset($config); + new Ruleset($config); }//end testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute() @@ -187,7 +187,7 @@ public function testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStand // Set up the ruleset. $standard = __DIR__.'/SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandardTest.xml'; $config = new ConfigDouble(["--standard=$standard"]); - $ruleset = new Ruleset($config); + new Ruleset($config); }//end testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandard() @@ -205,7 +205,7 @@ public function testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCateg // Set up the ruleset. $standard = __DIR__.'/SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategoryTest.xml'; $config = new ConfigDouble(["--standard=$standard"]); - $ruleset = new Ruleset($config); + new Ruleset($config); }//end testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategory() diff --git a/tests/Core/Tokenizer/PHP/BackfillFnTokenTest.php b/tests/Core/Tokenizer/PHP/BackfillFnTokenTest.php index f850b94fea..d0137532a7 100644 --- a/tests/Core/Tokenizer/PHP/BackfillFnTokenTest.php +++ b/tests/Core/Tokenizer/PHP/BackfillFnTokenTest.php @@ -714,8 +714,6 @@ public function testTernary() */ public function testTernaryWithTypes() { - $tokens = $this->phpcsFile->getTokens(); - $token = $this->getTargetToken('/* testTernaryWithTypes */', T_FN); $this->backfillHelper($token); $this->scopePositionTestHelper($token, 15, 27); diff --git a/tests/Core/Tokenizer/PHP/NamespacedNameSingleTokenTest.php b/tests/Core/Tokenizer/PHP/NamespacedNameSingleTokenTest.php index 52380c6470..699804b0d3 100644 --- a/tests/Core/Tokenizer/PHP/NamespacedNameSingleTokenTest.php +++ b/tests/Core/Tokenizer/PHP/NamespacedNameSingleTokenTest.php @@ -42,7 +42,7 @@ public function testIdentifierTokenization($testMarker, $expectedTokens) $tokens = $this->phpcsFile->getTokens(); $identifier = $this->getTargetToken($testMarker, constant($expectedTokens[0]['type'])); - foreach ($expectedTokens as $key => $tokenInfo) { + foreach ($expectedTokens as $tokenInfo) { $this->assertSame( constant($tokenInfo['type']), $tokens[$identifier]['code'], diff --git a/tests/Core/Tokenizer/PHP/StableCommentWhitespaceTest.php b/tests/Core/Tokenizer/PHP/StableCommentWhitespaceTest.php index 6a58ccbbd7..ec287745f2 100644 --- a/tests/Core/Tokenizer/PHP/StableCommentWhitespaceTest.php +++ b/tests/Core/Tokenizer/PHP/StableCommentWhitespaceTest.php @@ -38,7 +38,7 @@ public function testCommentTokenization($testMarker, $expectedTokens) $tokens = $this->phpcsFile->getTokens(); $comment = $this->getTargetToken($testMarker, Tokens::$commentTokens); - foreach ($expectedTokens as $key => $tokenInfo) { + foreach ($expectedTokens as $tokenInfo) { $this->assertSame( constant($tokenInfo['type']), $tokens[$comment]['code'], diff --git a/tests/Core/Tokenizer/PHP/StableCommentWhitespaceWinTest.php b/tests/Core/Tokenizer/PHP/StableCommentWhitespaceWinTest.php index dbb103060a..989bcd0f47 100644 --- a/tests/Core/Tokenizer/PHP/StableCommentWhitespaceWinTest.php +++ b/tests/Core/Tokenizer/PHP/StableCommentWhitespaceWinTest.php @@ -35,7 +35,7 @@ public function testCommentTokenization($testMarker, $expectedTokens) $tokens = $this->phpcsFile->getTokens(); $comment = $this->getTargetToken($testMarker, Tokens::$commentTokens); - foreach ($expectedTokens as $key => $tokenInfo) { + foreach ($expectedTokens as $tokenInfo) { $this->assertSame( constant($tokenInfo['type']), $tokens[$comment]['code'], diff --git a/tests/Standards/AllSniffs.php b/tests/Standards/AllSniffs.php index 8644ca39e3..1e273e28eb 100644 --- a/tests/Standards/AllSniffs.php +++ b/tests/Standards/AllSniffs.php @@ -48,8 +48,6 @@ public static function suite() $suite = new TestSuite('PHP CodeSniffer Standards'); - $isInstalled = !is_file(__DIR__.'/../../autoload.php'); - // Optionally allow for ignoring the tests for one or more standards. $ignoreTestsForStandards = getenv('PHPCS_IGNORE_TESTS'); if ($ignoreTestsForStandards === false) {