Skip to content

Commit

Permalink
UnusedPrivateElementsSniff - fixed handling always-used property anno…
Browse files Browse the repository at this point in the history
…tations
  • Loading branch information
ondrejmirtes committed Dec 22, 2015
1 parent d93b9d4 commit 172a023
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ private function getProperties(PHP_CodeSniffer_File $phpcsFile, array $tokens, a
$findPropertiesStartTokenPointer = $propertyTokenPointer + 1;
$phpDocTags = $this->getPhpDocTags($phpcsFile, $tokens, $visibilityModifiedTokenPointer);
foreach ($phpDocTags as $tag) {
if (in_array(substr($tag, 0, 4), $this->alwaysUsedPropertiesAnnotations, true)) {
preg_match('#([@a-zA-Z\\\]+)#', $tag, $matches);
if (in_array($matches[1], $this->alwaysUsedPropertiesAnnotations, true)) {
continue 2;
}
}
Expand Down
14 changes: 8 additions & 6 deletions tests/Sniffs/Classes/UnusedPrivateElementsSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function testCheckFile()
'alwaysUsedPropertiesAnnotations' => [
'@get',
'@set',
'@ORM\Column',
],
'alwaysUsedPropertiesSuffixes' => [
'Value',
Expand Down Expand Up @@ -42,22 +43,23 @@ public function testCheckFile()
UnusedPrivateElementsSniff::CODE_WRITE_ONLY_PROPERTY,
'Class ClassWithSomeUnusedProperties contains write-only property: $writeOnlyProperty'
);
$this->assertNoSniffError($resultFile, 33);
$this->assertSniffError(
$resultFile,
45,
48,
UnusedPrivateElementsSniff::CODE_UNUSED_METHOD,
'Class ClassWithSomeUnusedProperties contains unused private method: unusedPrivateMethod'
);
$this->assertNoSniffError($resultFile, 50);
$this->assertNoSniffError($resultFile, 55);
$this->assertNoSniffError($resultFile, 60);
$this->assertNoSniffError($resultFile, 51);
$this->assertNoSniffError($resultFile, 58);
$this->assertNoSniffError($resultFile, 63);
$this->assertSniffError(
$resultFile,
65,
68,
UnusedPrivateElementsSniff::CODE_UNUSED_METHOD,
'Class ClassWithSomeUnusedProperties contains unused private method: unusedStaticPrivateMethod'
);
$this->assertNoSniffError($resultFile, 70);
$this->assertNoSniffError($resultFile, 73);
}

}
3 changes: 3 additions & 0 deletions tests/Sniffs/Classes/data/ClassWithSomeUnusedProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class ClassWithSomeUnusedProperties extends \Consistence\Object

private $writeOnlyProperty;

/** @ORM\Column(name="foo") */
private $doctrineProperty;

public function foo()
{
$this->usedProperty->foo();
Expand Down

0 comments on commit 172a023

Please sign in to comment.