Skip to content

Commit

Permalink
Fix detecting unused property from class used in trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 5, 2020
1 parent f368080 commit 11ef998
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,17 +585,14 @@ private function processStmtNode(
if (!$scope->isInClass()) {
throw new \PHPStan\ShouldNotHappenException();
}
if ($scope->isInTrait()) {
return;
}
if ($scope->getClassReflection()->getName() !== $classReflection->getName()) {
return;
}
if ($node instanceof Node\Stmt\Property) {
if ($node instanceof Node\Stmt\Property && !$scope->isInTrait()) {
$properties[] = $node;
return;
}
if ($node instanceof Node\Stmt\ClassMethod) {
if ($node instanceof Node\Stmt\ClassMethod && !$scope->isInTrait()) {
$methods[] = $node;
return;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/private-property-trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@ public function setFoo($prop1)
$this->prop1 = $prop1;
}

public function getProp3()
{
return $this->prop3;
}

}

class ClassUsingTrait
{

use FooTrait;

private $prop3;

public function __construct(string $prop3)
{
$this->prop3 = $prop3;
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/unused-private-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ private function doBar()
public function doBaz()
{
$this->doFoo();
$this->doLorem();
}

}
Expand All @@ -147,4 +148,9 @@ class UsingFooTrait

use FooTrait;

private function doLorem()
{

}

}

0 comments on commit 11ef998

Please sign in to comment.