Skip to content

Commit c436d62

Browse files
committed
UnusedPrivateElementsSniff: Fixed "alwaysUsedPropertiesAnnotations" support of prefixes
1 parent d43b9a6 commit c436d62

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ private function getProperties(\PHP_CodeSniffer\Files\File $phpcsFile, array $to
379379
break 2;
380380
}
381381

382-
if (substr($alwaysUsedPropertyAnnotationName, -1) !== '\\' || strpos($annotationName, $alwaysUsedPropertyAnnotationName) !== 0) {
382+
if (substr($alwaysUsedPropertyAnnotationName, -1) === '\\' && strpos($annotationName, $alwaysUsedPropertyAnnotationName) === 0) {
383383
$alwaysUsedProperty = true;
384384
break 2;
385385
}

tests/Sniffs/Classes/UnusedPrivateElementsSniffTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public function testErrors(): void
108108

109109
self::assertNoSniffError($resultFile, 161);
110110
self::assertNoSniffError($resultFile, 169);
111+
112+
self::assertSniffError(
113+
$resultFile,
114+
180,
115+
UnusedPrivateElementsSniff::CODE_UNUSED_PROPERTY,
116+
'Class ClassWithSomeUnusedProperties contains unused property $propertyWithPrefixUnsetAnnotation.'
117+
);
111118
}
112119

113120
public function testOnlyPublicElements(): void

tests/Sniffs/Classes/data/classWithSomeUnusedElements.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,9 @@ public function createNewInstancesWithParentheses()
174174
new $this->propertyUsedToCreateNewInstanceWithParentheses();
175175
}
176176

177+
/**
178+
* @AssertX\Whatever()
179+
*/
180+
private $propertyWithPrefixUnsetAnnotation;
181+
177182
}

0 commit comments

Comments
 (0)