Skip to content

Commit

Permalink
CS/QA: remove unused variables (#446)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
klausi authored and jrfnl committed Jun 5, 2024
1 parent 275a07d commit ad2451f
Show file tree
Hide file tree
Showing 18 changed files with 26 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Standards/Generic/Sniffs/Files/InlineHTMLSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 6 additions & 11 deletions src/Standards/Squiz/Sniffs/PHP/CommentedOutCodeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Core/File/GetMemberPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions tests/Core/Ruleset/ExplainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ public function testExplainWillExplainEachStandardSeparately()

$this->expectOutputString($expected);

$runner = new Runner();
$exitCode = $runner->runPHPCS();
$runner = new Runner();
$runner->runPHPCS();

}//end testExplainWillExplainEachStandardSeparately()

Expand Down
2 changes: 0 additions & 2 deletions tests/Core/Ruleset/RuleInclusionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions tests/Core/Ruleset/SetSniffPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand Down
2 changes: 0 additions & 2 deletions tests/Core/Tokenizer/PHP/BackfillFnTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Core/Tokenizer/PHP/NamespacedNameSingleTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
2 changes: 1 addition & 1 deletion tests/Core/Tokenizer/PHP/StableCommentWhitespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
2 changes: 0 additions & 2 deletions tests/Standards/AllSniffs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ad2451f

Please sign in to comment.