Skip to content

Commit 9e5035e

Browse files
committed
SlevomatCodingStandard.Variables.UnusedVariable: Fixed missing report when option "ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach" is enabled
1 parent 1f2e6db commit 9e5035e

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

SlevomatCodingStandard/Sniffs/Variables/UnusedVariableSniff.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,21 @@ private function isUsedAsKeyOrValueInArray(File $phpcsFile, int $variablePointer
511511

512512
private function isValueInForeachAndErrorIsIgnored(File $phpcsFile, int $variablePointer): bool
513513
{
514-
if (!$this->ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach) {
514+
$tokens = $phpcsFile->getTokens();
515+
516+
$parenthesisOwnerPointer = $this->findNestedParenthesisWithOwner($phpcsFile, $variablePointer);
517+
$isInForeach = $parenthesisOwnerPointer !== null && $tokens[$parenthesisOwnerPointer]['code'] === T_FOREACH;
518+
519+
if (!$isInForeach) {
515520
return false;
516521
}
517522

518-
$tokens = $phpcsFile->getTokens();
523+
$pointerAfterVariable = TokenHelper::findNextEffective($phpcsFile, $variablePointer + 1);
524+
if ($pointerAfterVariable !== null && $tokens[$pointerAfterVariable]['code'] === T_DOUBLE_ARROW) {
525+
return false;
526+
}
519527

520-
$parenthesisOwnerPointer = $this->findNestedParenthesisWithOwner($phpcsFile, $variablePointer);
521-
return $parenthesisOwnerPointer !== null && $tokens[$parenthesisOwnerPointer]['code'] === T_FOREACH;
528+
return $this->ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach;
522529
}
523530

524531
private function isStaticOrGlobalVariable(File $phpcsFile, int $functionPointer, string $variableName): bool

tests/Sniffs/Variables/UnusedVariableSniffTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ public function testErrors(): void
4646
self::assertSniffError($report, 128, UnusedVariableSniff::CODE_UNUSED_VARIABLE, 'Unused variable $a.');
4747
}
4848

49-
public function testNoErrorsWithIgnoredUnusedValuesWhenOnlyKeysAreUsedInForeach(): void
49+
public function testErrorsWithIgnoredUnusedValuesWhenOnlyKeysAreUsedInForeach(): void
5050
{
51-
$report = self::checkFile(__DIR__ . '/data/unusedVariableNoErrorsWithIgnoredUnusedValuesWhenOnlyKeysAreUsedInForeach.php', [
51+
$report = self::checkFile(__DIR__ . '/data/unusedVariableErrorsWithIgnoredUnusedValuesWhenOnlyKeysAreUsedInForeach.php', [
5252
'ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach' => true,
5353
]);
54-
self::assertNoSniffErrorInFile($report);
54+
55+
self::assertSame(1, $report->getErrorCount());
56+
57+
self::assertSniffError($report, 18, UnusedVariableSniff::CODE_UNUSED_VARIABLE, 'Unused variable $someKey.');
5558
}
5659

5760
}

tests/Sniffs/Variables/data/unusedVariableNoErrorsWithIgnoredUnusedValuesWhenOnlyKeysAreUsedInForeach.php renamed to tests/Sniffs/Variables/data/unusedVariableErrorsWithIgnoredUnusedValuesWhenOnlyKeysAreUsedInForeach.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414
foreach (range(0, 5) as $otherKey => $otherValue) {
1515
echo $otherKey;
1616
}
17+
18+
foreach (range(0, 5) as $someKey => $someValue) {
19+
// Key and value are not used
20+
}

0 commit comments

Comments
 (0)