Skip to content

Commit 172a023

Browse files
committed
UnusedPrivateElementsSniff - fixed handling always-used property annotations
1 parent d93b9d4 commit 172a023

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ private function getProperties(PHP_CodeSniffer_File $phpcsFile, array $tokens, a
179179
$findPropertiesStartTokenPointer = $propertyTokenPointer + 1;
180180
$phpDocTags = $this->getPhpDocTags($phpcsFile, $tokens, $visibilityModifiedTokenPointer);
181181
foreach ($phpDocTags as $tag) {
182-
if (in_array(substr($tag, 0, 4), $this->alwaysUsedPropertiesAnnotations, true)) {
182+
preg_match('#([@a-zA-Z\\\]+)#', $tag, $matches);
183+
if (in_array($matches[1], $this->alwaysUsedPropertiesAnnotations, true)) {
183184
continue 2;
184185
}
185186
}

tests/Sniffs/Classes/UnusedPrivateElementsSniffTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public function testCheckFile()
1111
'alwaysUsedPropertiesAnnotations' => [
1212
'@get',
1313
'@set',
14+
'@ORM\Column',
1415
],
1516
'alwaysUsedPropertiesSuffixes' => [
1617
'Value',
@@ -42,22 +43,23 @@ public function testCheckFile()
4243
UnusedPrivateElementsSniff::CODE_WRITE_ONLY_PROPERTY,
4344
'Class ClassWithSomeUnusedProperties contains write-only property: $writeOnlyProperty'
4445
);
46+
$this->assertNoSniffError($resultFile, 33);
4547
$this->assertSniffError(
4648
$resultFile,
47-
45,
49+
48,
4850
UnusedPrivateElementsSniff::CODE_UNUSED_METHOD,
4951
'Class ClassWithSomeUnusedProperties contains unused private method: unusedPrivateMethod'
5052
);
51-
$this->assertNoSniffError($resultFile, 50);
52-
$this->assertNoSniffError($resultFile, 55);
53-
$this->assertNoSniffError($resultFile, 60);
53+
$this->assertNoSniffError($resultFile, 51);
54+
$this->assertNoSniffError($resultFile, 58);
55+
$this->assertNoSniffError($resultFile, 63);
5456
$this->assertSniffError(
5557
$resultFile,
56-
65,
58+
68,
5759
UnusedPrivateElementsSniff::CODE_UNUSED_METHOD,
5860
'Class ClassWithSomeUnusedProperties contains unused private method: unusedStaticPrivateMethod'
5961
);
60-
$this->assertNoSniffError($resultFile, 70);
62+
$this->assertNoSniffError($resultFile, 73);
6163
}
6264

6365
}

tests/Sniffs/Classes/data/ClassWithSomeUnusedProperties.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class ClassWithSomeUnusedProperties extends \Consistence\Object
2929

3030
private $writeOnlyProperty;
3131

32+
/** @ORM\Column(name="foo") */
33+
private $doctrineProperty;
34+
3235
public function foo()
3336
{
3437
$this->usedProperty->foo();

0 commit comments

Comments
 (0)