Skip to content

Commit d140424

Browse files
authored
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
1 parent aca6b8b commit d140424

20 files changed

+27
-43
lines changed

src/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ public static function setConfigData($key, $value, $temp=false)
15701570
// standards paths are added to the autoloader.
15711571
if ($key === 'installed_paths') {
15721572
$installedStandards = Standards::getInstalledStandardDetails();
1573-
foreach ($installedStandards as $name => $details) {
1573+
foreach ($installedStandards as $details) {
15741574
Autoload::addSearchPath($details['path'], $details['namespace']);
15751575
}
15761576
}

src/Filters/Filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ protected function shouldProcessFile($path)
179179
// complete extension list and make sure one is allowed.
180180
$extensions = [];
181181
array_shift($fileParts);
182-
foreach ($fileParts as $part) {
182+
while (empty($fileParts) === false) {
183183
$extensions[implode('.', $fileParts)] = 1;
184184
array_shift($fileParts);
185185
}

src/Runner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,11 @@ public function init()
333333

334334
// Create this class so it is autoloaded and sets up a bunch
335335
// of PHP_CodeSniffer-specific token type constants.
336-
$tokens = new Tokens();
336+
new Tokens();
337337

338338
// Allow autoloading of custom files inside installed standards.
339339
$installedStandards = Standards::getInstalledStandardDetails();
340-
foreach ($installedStandards as $name => $details) {
340+
foreach ($installedStandards as $details) {
341341
Autoload::addSearchPath($details['path'], $details['namespace']);
342342
}
343343

src/Standards/Generic/Sniffs/Files/InlineHTMLSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function process(File $phpcsFile, $stackPtr)
5454
{
5555
// Allow a byte-order mark.
5656
$tokens = $phpcsFile->getTokens();
57-
foreach ($this->bomDefinitions as $bomName => $expectedBomHex) {
57+
foreach ($this->bomDefinitions as $expectedBomHex) {
5858
$bomByteLength = (strlen($expectedBomHex) / 2);
5959
$htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength));
6060
if ($htmlBomHex === $expectedBomHex && strlen($tokens[0]['content']) === $bomByteLength) {

src/Standards/Generic/Sniffs/PHP/CharacterBeforePHPOpeningTagSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function process(File $phpcsFile, $stackPtr)
5656
if ($stackPtr > 0) {
5757
// Allow a byte-order mark.
5858
$tokens = $phpcsFile->getTokens();
59-
foreach ($this->bomDefinitions as $bomName => $expectedBomHex) {
59+
foreach ($this->bomDefinitions as $expectedBomHex) {
6060
$bomByteLength = (strlen($expectedBomHex) / 2);
6161
$htmlBomHex = bin2hex(substr($tokens[0]['content'], 0, $bomByteLength));
6262
if ($htmlBomHex === $expectedBomHex) {

src/Standards/Generic/Tests/Debug/JSHintUnitTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ final class JSHintUnitTest extends AbstractSniffUnitTest
2828
*/
2929
protected function shouldSkipTest()
3030
{
31-
$rhinoPath = Config::getExecutablePath('rhino');
3231
$jshintPath = Config::getExecutablePath('jshint');
3332
if ($jshintPath === null) {
3433
return true;

src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
8686
// Check return type (can be multiple, separated by '|').
8787
$typeNames = explode('|', $returnType);
8888
$suggestedNames = [];
89-
foreach ($typeNames as $i => $typeName) {
89+
foreach ($typeNames as $typeName) {
9090
$suggestedName = Common::suggestType($typeName);
9191
if (in_array($suggestedName, $suggestedNames, true) === false) {
9292
$suggestedNames[] = $suggestedName;

src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
135135
// Check var type (can be multiple, separated by '|').
136136
$typeNames = explode('|', $varType);
137137
$suggestedNames = [];
138-
foreach ($typeNames as $i => $typeName) {
138+
foreach ($typeNames as $typeName) {
139139
$suggestedName = Common::suggestType($typeName);
140140
if (in_array($suggestedName, $suggestedNames, true) === false) {
141141
$suggestedNames[] = $suggestedName;

src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ public function processBracket($phpcsFile, $openBracket, $tokens, $type='functio
225225
}//end if
226226

227227
// Each line between the brackets should contain a single parameter.
228-
$lastComma = null;
229228
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
230229
// Skip brackets, like arrays, as they can contain commas.
231230
if (isset($tokens[$i]['bracket_opener']) === true) {

src/Standards/Squiz/Sniffs/PHP/CommentedOutCodeSniff.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,23 +238,18 @@ public function process(File $phpcsFile, $stackPtr)
238238
];
239239
$emptyTokens += Tokens::$phpcsCommentTokens;
240240

241-
$numComment = 0;
242-
$numPossible = 0;
243241
$numCode = 0;
244242
$numNonWhitespace = 0;
245243

246244
for ($i = 0; $i < $numTokens; $i++) {
247-
if (isset($emptyTokens[$stringTokens[$i]['code']]) === true) {
248-
// Looks like comment.
249-
$numComment++;
250-
} else if (isset(Tokens::$comparisonTokens[$stringTokens[$i]['code']]) === true
251-
|| isset(Tokens::$arithmeticTokens[$stringTokens[$i]['code']]) === true
252-
|| $stringTokens[$i]['code'] === T_GOTO_LABEL
253-
) {
245+
// Do not count comments.
246+
if (isset($emptyTokens[$stringTokens[$i]['code']]) === false
254247
// Commented out HTML/XML and other docs contain a lot of these
255248
// characters, so it is best to not use them directly.
256-
$numPossible++;
257-
} else {
249+
&& isset(Tokens::$comparisonTokens[$stringTokens[$i]['code']]) === false
250+
&& isset(Tokens::$arithmeticTokens[$stringTokens[$i]['code']]) === false
251+
&& $stringTokens[$i]['code'] !== T_GOTO_LABEL
252+
) {
258253
// Looks like code.
259254
$numCode++;
260255
}

src/Tokenizers/JS.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ public function processAdditional()
11881188
}
11891189
}
11901190
} else if ($this->tokens[$i]['code'] === T_CLOSE_OBJECT) {
1191-
$opener = array_pop($classStack);
1191+
array_pop($classStack);
11921192
} else if ($this->tokens[$i]['code'] === T_COLON) {
11931193
// If it is a scope opener, it belongs to a
11941194
// DEFAULT or CASE statement.

tests/Core/File/GetMemberPropertiesTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ public function testNotClassPropertyException($identifier)
11461146
$this->expectRunTimeException('$stackPtr is not a class member var');
11471147

11481148
$variable = $this->getTargetToken($identifier, T_VARIABLE);
1149-
$result = self::$phpcsFile->getMemberProperties($variable);
1149+
self::$phpcsFile->getMemberProperties($variable);
11501150

11511151
}//end testNotClassPropertyException()
11521152

@@ -1182,8 +1182,8 @@ public function testNotAVariableException()
11821182
{
11831183
$this->expectRunTimeException('$stackPtr must be of type T_VARIABLE');
11841184

1185-
$next = $this->getTargetToken('/* testNotAVariable */', T_RETURN);
1186-
$result = self::$phpcsFile->getMemberProperties($next);
1185+
$next = $this->getTargetToken('/* testNotAVariable */', T_RETURN);
1186+
self::$phpcsFile->getMemberProperties($next);
11871187

11881188
}//end testNotAVariableException()
11891189

tests/Core/Ruleset/ExplainTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ public function testExplainWillExplainEachStandardSeparately()
249249

250250
$this->expectOutputString($expected);
251251

252-
$runner = new Runner();
253-
$exitCode = $runner->runPHPCS();
252+
$runner = new Runner();
253+
$runner->runPHPCS();
254254

255255
}//end testExplainWillExplainEachStandardSeparately()
256256

tests/Core/Ruleset/RuleInclusionTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,6 @@ public function testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFail
439439
{
440440
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
441441

442-
$sniffObject = self::$ruleset->sniffs[$sniffClass];
443-
444442
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
445443
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
446444
$this->assertFalse($hasProperty, $errorMsg);

tests/Core/Ruleset/SetSniffPropertyTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function testSetPropertyThrowsErrorOnInvalidProperty()
148148
// Set up the ruleset.
149149
$standard = __DIR__.'/SetPropertyThrowsErrorOnInvalidPropertyTest.xml';
150150
$config = new ConfigDouble(["--standard=$standard"]);
151-
$ruleset = new Ruleset($config);
151+
new Ruleset($config);
152152

153153
}//end testSetPropertyThrowsErrorOnInvalidProperty()
154154

@@ -175,7 +175,7 @@ public function testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute()
175175
// Set up the ruleset.
176176
$standard = __DIR__.'/SetPropertyNotAllowedViaAttributeTest.xml';
177177
$config = new ConfigDouble(["--standard=$standard"]);
178-
$ruleset = new Ruleset($config);
178+
new Ruleset($config);
179179

180180
}//end testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute()
181181

@@ -193,7 +193,7 @@ public function testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStand
193193
// Set up the ruleset.
194194
$standard = __DIR__.'/SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandardTest.xml';
195195
$config = new ConfigDouble(["--standard=$standard"]);
196-
$ruleset = new Ruleset($config);
196+
new Ruleset($config);
197197

198198
}//end testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandard()
199199

@@ -211,7 +211,7 @@ public function testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCateg
211211
// Set up the ruleset.
212212
$standard = __DIR__.'/SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategoryTest.xml';
213213
$config = new ConfigDouble(["--standard=$standard"]);
214-
$ruleset = new Ruleset($config);
214+
new Ruleset($config);
215215

216216
}//end testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategory()
217217

@@ -383,16 +383,13 @@ public function testDirectCallWithOldArrayFormatThrowsDeprecationNotice()
383383
}
384384

385385
$name = 'AllowedAsDeclared';
386-
$sniffCode = "Fixtures.SetProperty.{$name}";
387386
$sniffClass = 'Fixtures\Sniffs\SetProperty\\'.$name.'Sniff';
388387

389388
// Set up the ruleset.
390389
$standard = __DIR__."/SetProperty{$name}Test.xml";
391390
$config = new ConfigDouble(["--standard=$standard"]);
392391
$ruleset = new Ruleset($config);
393392

394-
$propertyName = 'arbitrarystring';
395-
396393
$ruleset->setSniffProperty(
397394
$sniffClass,
398395
'arbitrarystring',

tests/Core/Tokenizer/PHP/BackfillFnTokenTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,6 @@ public function testTernary()
658658
*/
659659
public function testTernaryWithTypes()
660660
{
661-
$tokens = $this->phpcsFile->getTokens();
662-
663661
$token = $this->getTargetToken('/* testTernaryWithTypes */', T_FN);
664662
$this->backfillHelper($token);
665663
$this->scopePositionTestHelper($token, 15, 27);

tests/Core/Tokenizer/PHP/StableCommentWhitespaceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testCommentTokenization($testMarker, $expectedTokens)
3838
$tokens = $this->phpcsFile->getTokens();
3939
$comment = $this->getTargetToken($testMarker, Tokens::$commentTokens);
4040

41-
foreach ($expectedTokens as $key => $tokenInfo) {
41+
foreach ($expectedTokens as $tokenInfo) {
4242
$this->assertSame(
4343
constant($tokenInfo['type']),
4444
$tokens[$comment]['code'],

tests/Core/Tokenizer/PHP/StableCommentWhitespaceWinTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testCommentTokenization($testMarker, $expectedTokens)
3535
$tokens = $this->phpcsFile->getTokens();
3636
$comment = $this->getTargetToken($testMarker, Tokens::$commentTokens);
3737

38-
foreach ($expectedTokens as $key => $tokenInfo) {
38+
foreach ($expectedTokens as $tokenInfo) {
3939
$this->assertSame(
4040
constant($tokenInfo['type']),
4141
$tokens[$comment]['code'],

tests/Core/Tokenizer/PHP/UndoNamespacedNameSingleTokenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testIdentifierTokenization($testMarker, $expectedTokens)
4242
$tokens = $this->phpcsFile->getTokens();
4343
$identifier = $this->getTargetToken($testMarker, constant($expectedTokens[0]['type']));
4444

45-
foreach ($expectedTokens as $key => $tokenInfo) {
45+
foreach ($expectedTokens as $tokenInfo) {
4646
$this->assertSame(
4747
constant($tokenInfo['type']),
4848
$tokens[$identifier]['code'],

tests/Standards/AllSniffs.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public static function suite()
4848

4949
$suite = new TestSuite('PHP CodeSniffer Standards');
5050

51-
$isInstalled = !is_file(__DIR__.'/../../autoload.php');
52-
5351
// Optionally allow for ignoring the tests for one or more standards.
5452
$ignoreTestsForStandards = getenv('PHPCS_IGNORE_TESTS');
5553
if ($ignoreTestsForStandards === false) {

0 commit comments

Comments
 (0)