Skip to content

Commit f39a5e6

Browse files
committed
UnusedPrivateElementsSniff - configurable always-used properties
1 parent 9dd9738 commit f39a5e6

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Although PHP_CodeSniffer is not suitable for static analysis because it is limit
3333

3434
This is very useful during refactoring to clean up dead code and injected dependencies.
3535

36+
This sniff supports `alwaysUsedPropertiesAnnotations` setting to mark certain properties as always used, for example the ones with `@ORM\Column` annotations. Also, `alwaysUsedPropertiesSuffixes` can be set to mark properties with name ending with a certain string to be always marked as used.
37+
3638
### SlevomatCodingStandard.Arrays.TrailingArrayComma 🔧
3739

3840
Commas after last element in an array make adding a new element easier and result in a cleaner versioning diff.

SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ class UnusedPrivateElementsSniff implements \PHP_CodeSniffer_Sniff
1515

1616
const CODE_UNUSED_METHOD = 'unusedMethod';
1717

18+
/** @var string[] */
19+
public $alwaysUsedPropertiesAnnotations = [];
20+
21+
/** @var string[] */
22+
public $alwaysUsedPropertiesSuffixes = [];
23+
1824
/**
1925
* @return integer[]
2026
*/
@@ -173,19 +179,18 @@ private function getProperties(PHP_CodeSniffer_File $phpcsFile, array $tokens, a
173179
$findPropertiesStartTokenPointer = $propertyTokenPointer + 1;
174180
$phpDocTags = $this->getPhpDocTags($phpcsFile, $tokens, $visibilityModifiedTokenPointer);
175181
foreach ($phpDocTags as $tag) {
176-
if (in_array(substr($tag, 0, 4), ['@get', '@set'], true)) {
182+
if (in_array(substr($tag, 0, 4), $this->alwaysUsedPropertiesAnnotations, true)) {
177183
continue 2;
178184
}
179185
}
180186

181187
$propertyToken = $tokens[$propertyTokenPointer];
182188
$name = substr($propertyToken['content'], 1);
183189

184-
if (StringHelper::endsWith($name, 'Value')) {
185-
continue;
186-
}
187-
if (StringHelper::endsWith($name, 'Timestamp')) {
188-
continue;
190+
foreach ($this->alwaysUsedPropertiesSuffixes as $prefix) {
191+
if (StringHelper::endsWith($name, $prefix)) {
192+
continue 2;
193+
}
189194
}
190195

191196
$reportedProperties[$name] = $propertyTokenPointer;

tests/Sniffs/Classes/UnusedPrivateElementsSniffTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ class UnusedPrivateElementsSniffTest extends \SlevomatCodingStandard\Sniffs\Test
77

88
public function testCheckFile()
99
{
10-
$resultFile = $this->checkFile(__DIR__ . '/data/ClassWithSomeUnusedProperties.php');
10+
$resultFile = $this->checkFile(__DIR__ . '/data/ClassWithSomeUnusedProperties.php', [
11+
'alwaysUsedPropertiesAnnotations' => [
12+
'@get',
13+
'@set',
14+
],
15+
'alwaysUsedPropertiesSuffixes' => [
16+
'Value',
17+
'Timestamp',
18+
],
19+
]);
1120
$this->assertNoSniffError($resultFile, 6);
1221
$this->assertNoSniffError($resultFile, 8);
1322
$this->assertNoSniffError($resultFile, 10);

0 commit comments

Comments
 (0)