Skip to content

Commit ad2451f

Browse files
klausijrfnl
authored andcommitted
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 275a07d commit ad2451f

18 files changed

+26
-38
lines changed

src/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ public function setConfigData($key, $value, $temp=false)
15501550
// standards paths are added to the autoloader.
15511551
if ($key === 'installed_paths') {
15521552
$installedStandards = Standards::getInstalledStandardDetails();
1553-
foreach ($installedStandards as $name => $details) {
1553+
foreach ($installedStandards as $details) {
15541554
Autoload::addSearchPath($details['path'], $details['namespace']);
15551555
}
15561556
}

src/Filters/Filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ protected function shouldProcessFile($path)
200200
// complete extension list and make sure one is allowed.
201201
$extensions = [];
202202
array_shift($fileParts);
203-
foreach ($fileParts as $part) {
203+
while (empty($fileParts) === false) {
204204
$extensions[] = implode('.', $fileParts);
205205
array_shift($fileParts);
206206
}

src/Runner.php

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

320320
// Create this class so it is autoloaded and sets up a bunch
321321
// of PHP_CodeSniffer-specific token type constants.
322-
$tokens = new Tokens();
322+
new Tokens();
323323

324324
// Allow autoloading of custom files inside installed standards.
325325
$installedStandards = Standards::getInstalledStandardDetails();
326-
foreach ($installedStandards as $name => $details) {
326+
foreach ($installedStandards as $details) {
327327
Autoload::addSearchPath($details['path'], $details['namespace']);
328328
}
329329

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/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
@@ -136,7 +136,7 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
136136
// Check var type (can be multiple, separated by '|').
137137
$typeNames = explode('|', $varType);
138138
$suggestedNames = [];
139-
foreach ($typeNames as $i => $typeName) {
139+
foreach ($typeNames as $typeName) {
140140
$suggestedName = Common::suggestType($typeName);
141141
if (in_array($suggestedName, $suggestedNames, true) === false) {
142142
$suggestedNames[] = $suggestedName;

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

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

217217
// Each line between the brackets should contain a single parameter.
218-
$lastComma = null;
219218
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
220219
// Skip brackets, like arrays, as they can contain commas.
221220
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
@@ -224,23 +224,18 @@ public function process(File $phpcsFile, $stackPtr)
224224
];
225225
$emptyTokens += Tokens::$phpcsCommentTokens;
226226

227-
$numComment = 0;
228-
$numPossible = 0;
229227
$numCode = 0;
230228
$numNonWhitespace = 0;
231229

232230
for ($i = 0; $i < $numTokens; $i++) {
233-
if (isset($emptyTokens[$stringTokens[$i]['code']]) === true) {
234-
// Looks like comment.
235-
$numComment++;
236-
} else if (isset(Tokens::$comparisonTokens[$stringTokens[$i]['code']]) === true
237-
|| isset(Tokens::$arithmeticTokens[$stringTokens[$i]['code']]) === true
238-
|| $stringTokens[$i]['code'] === T_GOTO_LABEL
239-
) {
231+
// Do not count comments.
232+
if (isset($emptyTokens[$stringTokens[$i]['code']]) === false
240233
// Commented out HTML/XML and other docs contain a lot of these
241234
// characters, so it is best to not use them directly.
242-
$numPossible++;
243-
} else {
235+
&& isset(Tokens::$comparisonTokens[$stringTokens[$i]['code']]) === false
236+
&& isset(Tokens::$arithmeticTokens[$stringTokens[$i]['code']]) === false
237+
&& $stringTokens[$i]['code'] !== T_GOTO_LABEL
238+
) {
244239
// Looks like code.
245240
$numCode++;
246241
}

tests/Core/File/GetMemberPropertiesTest.php

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

11491149
$variable = $this->getTargetToken($identifier, T_VARIABLE);
1150-
$result = self::$phpcsFile->getMemberProperties($variable);
1150+
self::$phpcsFile->getMemberProperties($variable);
11511151

11521152
}//end testNotClassPropertyException()
11531153

@@ -1184,8 +1184,8 @@ public function testNotAVariableException()
11841184
$this->expectException('PHP_CodeSniffer\Exceptions\RuntimeException');
11851185
$this->expectExceptionMessage('$stackPtr must be of type T_VARIABLE');
11861186

1187-
$next = $this->getTargetToken('/* testNotAVariable */', T_RETURN);
1188-
$result = self::$phpcsFile->getMemberProperties($next);
1187+
$next = $this->getTargetToken('/* testNotAVariable */', T_RETURN);
1188+
self::$phpcsFile->getMemberProperties($next);
11891189

11901190
}//end testNotAVariableException()
11911191

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
@@ -435,8 +435,6 @@ public function testSettingInvalidPropertiesOnStandardsAndCategoriesSilentlyFail
435435
{
436436
$this->assertArrayHasKey($sniffClass, self::$ruleset->sniffs, 'Sniff class '.$sniffClass.' not listed in registered sniffs');
437437

438-
$sniffObject = self::$ruleset->sniffs[$sniffClass];
439-
440438
$hasProperty = (new ReflectionObject(self::$ruleset->sniffs[$sniffClass]))->hasProperty($propertyName);
441439
$errorMsg = sprintf('Property %s registered for sniff %s which does not support it', $propertyName, $sniffClass);
442440
$this->assertFalse($hasProperty, $errorMsg);

tests/Core/Ruleset/SetSniffPropertyTest.php

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

150150
}//end testSetPropertyThrowsErrorOnInvalidProperty()
151151

@@ -169,7 +169,7 @@ public function testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute()
169169
// Set up the ruleset.
170170
$standard = __DIR__.'/SetPropertyNotAllowedViaAttributeTest.xml';
171171
$config = new ConfigDouble(["--standard=$standard"]);
172-
$ruleset = new Ruleset($config);
172+
new Ruleset($config);
173173

174174
}//end testSetPropertyThrowsErrorWhenPropertyOnlyAllowedViaAttribute()
175175

@@ -187,7 +187,7 @@ public function testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStand
187187
// Set up the ruleset.
188188
$standard = __DIR__.'/SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandardTest.xml';
189189
$config = new ConfigDouble(["--standard=$standard"]);
190-
$ruleset = new Ruleset($config);
190+
new Ruleset($config);
191191

192192
}//end testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForStandard()
193193

@@ -205,7 +205,7 @@ public function testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCateg
205205
// Set up the ruleset.
206206
$standard = __DIR__.'/SetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategoryTest.xml';
207207
$config = new ConfigDouble(["--standard=$standard"]);
208-
$ruleset = new Ruleset($config);
208+
new Ruleset($config);
209209

210210
}//end testSetPropertyDoesNotThrowErrorOnInvalidPropertyWhenSetForCategory()
211211

tests/Core/Tokenizer/PHP/BackfillFnTokenTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,6 @@ public function testTernary()
714714
*/
715715
public function testTernaryWithTypes()
716716
{
717-
$tokens = $this->phpcsFile->getTokens();
718-
719717
$token = $this->getTargetToken('/* testTernaryWithTypes */', T_FN);
720718
$this->backfillHelper($token);
721719
$this->scopePositionTestHelper($token, 15, 27);

tests/Core/Tokenizer/PHP/NamespacedNameSingleTokenTest.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/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/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)