Skip to content

Commit 98fa7a3

Browse files
committed
UnusedPrivateElementsSniff: Fixed false positive with constant in string
1 parent c436d62 commit 98fa7a3

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $classPointer):
225225
unset($reportedProperties[$propertyInString]);
226226
}
227227
}
228+
if (preg_match_all('~(?:self|static)::(.+?\b)~', $token['content'], $matches, PREG_PATTERN_ORDER)) {
229+
foreach ($matches[1] as $constantInString) {
230+
if (!isset($reportedConstants[$constantInString])) {
231+
continue;
232+
}
233+
234+
unset($reportedConstants[$constantInString]);
235+
}
236+
}
228237
if (preg_match_all('~(?<=\{)\$this->(.+?\b)(?=\()~', $token['content'], $matches, PREG_PATTERN_ORDER)) {
229238
foreach ($matches[1] as $methodInString) {
230239
$normalizedMethodInString = $this->getNormalizedMethodName($methodInString);

tests/Sniffs/Classes/UnusedPrivateElementsSniffTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public function testErrors(): void
115115
UnusedPrivateElementsSniff::CODE_UNUSED_PROPERTY,
116116
'Class ClassWithSomeUnusedProperties contains unused property $propertyWithPrefixUnsetAnnotation.'
117117
);
118+
119+
self::assertNoSniffError($resultFile, 182);
118120
}
119121

120122
public function testOnlyPublicElements(): void

tests/Sniffs/Classes/data/classWithSomeUnusedElements.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,11 @@ public function createNewInstancesWithParentheses()
179179
*/
180180
private $propertyWithPrefixUnsetAnnotation;
181181

182+
private const USED_CONSTANT_IN_STRING = false;
183+
184+
public function usedConstantInString()
185+
{
186+
echo "{$this->whatever(self::USED_CONSTANT_IN_STRING)}";
187+
}
188+
182189
}

0 commit comments

Comments
 (0)