Skip to content

Commit 1a6575c

Browse files
committed
UselessVariableSniff: Improved fixer
1 parent 0a9c30f commit 1a6575c

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

SlevomatCodingStandard/Sniffs/Variables/UselessVariableSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function sprintf;
1717
use const T_AND_EQUAL;
1818
use const T_BITWISE_AND;
19+
use const T_CLOSE_CURLY_BRACKET;
1920
use const T_CONCAT_EQUAL;
2021
use const T_DIV_EQUAL;
2122
use const T_DO;
@@ -130,7 +131,7 @@ public function process(File $phpcsFile, $returnPointer): void
130131
}
131132

132133
if (
133-
!in_array($tokens[$pointerBeforePreviousVariable]['code'], [T_SEMICOLON, T_OPEN_CURLY_BRACKET], true)
134+
!in_array($tokens[$pointerBeforePreviousVariable]['code'], [T_SEMICOLON, T_OPEN_CURLY_BRACKET, T_CLOSE_CURLY_BRACKET], true)
134135
&& TokenHelper::findNextEffective($phpcsFile, $returnSemicolonPointer + 1) !== null
135136
) {
136137
$phpcsFile->addError(...$errorParameters);

tests/Sniffs/Variables/UselessVariableSniffTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/uselessVariableErrors.php');
1919

20-
self::assertSame(18, $report->getErrorCount());
20+
self::assertSame(19, $report->getErrorCount());
2121

2222
self::assertSniffError($report, 4, UselessVariableSniff::CODE_USELESS_VARIABLE, 'Useless variable $a.');
2323
self::assertSniffError($report, 9, UselessVariableSniff::CODE_USELESS_VARIABLE, 'Useless variable $b.');
@@ -36,7 +36,8 @@ public function testErrors(): void
3636
self::assertSniffError($report, 78, UselessVariableSniff::CODE_USELESS_VARIABLE, 'Useless variable $o.');
3737
self::assertSniffError($report, 84, UselessVariableSniff::CODE_USELESS_VARIABLE, 'Useless variable $p.');
3838
self::assertSniffError($report, 89, UselessVariableSniff::CODE_USELESS_VARIABLE, 'Useless variable $q.');
39-
self::assertSniffError($report, 93, UselessVariableSniff::CODE_USELESS_VARIABLE, 'Useless variable $z.');
39+
self::assertSniffError($report, 99, UselessVariableSniff::CODE_USELESS_VARIABLE, 'Useless variable $r.');
40+
self::assertSniffError($report, 103, UselessVariableSniff::CODE_USELESS_VARIABLE, 'Useless variable $z.');
4041

4142
self::assertAllFixedInFile($report);
4243
}

tests/Sniffs/Variables/data/uselessVariableErrors.fixed.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,13 @@ function assigmentAfterAssignment() {
7373
return $q;
7474
}
7575

76+
function afterIfStatement(float $seconds): string
77+
{
78+
if ($seconds < 1) {
79+
return round($seconds * 1000, 2) . 'ms';
80+
}
81+
82+
return round($seconds, 2) . 's';
83+
}
84+
7685
return null;

tests/Sniffs/Variables/data/uselessVariableErrors.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,15 @@ function assigmentAfterAssignment() {
9090
return $q;
9191
}
9292

93+
function afterIfStatement(float $seconds): string
94+
{
95+
if ($seconds < 1) {
96+
return round($seconds * 1000, 2) . 'ms';
97+
}
98+
99+
$r = round($seconds, 2) . 's';
100+
return $r;
101+
}
102+
93103
$z = null;
94104
return $z;

0 commit comments

Comments
 (0)