Skip to content

Commit 11ef998

Browse files
committed
Fix detecting unused property from class used in trait
1 parent f368080 commit 11ef998

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/Analyser/NodeScopeResolver.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -585,17 +585,14 @@ private function processStmtNode(
585585
if (!$scope->isInClass()) {
586586
throw new \PHPStan\ShouldNotHappenException();
587587
}
588-
if ($scope->isInTrait()) {
589-
return;
590-
}
591588
if ($scope->getClassReflection()->getName() !== $classReflection->getName()) {
592589
return;
593590
}
594-
if ($node instanceof Node\Stmt\Property) {
591+
if ($node instanceof Node\Stmt\Property && !$scope->isInTrait()) {
595592
$properties[] = $node;
596593
return;
597594
}
598-
if ($node instanceof Node\Stmt\ClassMethod) {
595+
if ($node instanceof Node\Stmt\ClassMethod && !$scope->isInTrait()) {
599596
$methods[] = $node;
600597
return;
601598
}

tests/PHPStan/Rules/DeadCode/data/private-property-trait.php

+12
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,23 @@ public function setFoo($prop1)
1919
$this->prop1 = $prop1;
2020
}
2121

22+
public function getProp3()
23+
{
24+
return $this->prop3;
25+
}
26+
2227
}
2328

2429
class ClassUsingTrait
2530
{
2631

2732
use FooTrait;
2833

34+
private $prop3;
35+
36+
public function __construct(string $prop3)
37+
{
38+
$this->prop3 = $prop3;
39+
}
40+
2941
}

tests/PHPStan/Rules/DeadCode/data/unused-private-method.php

+6
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ private function doBar()
138138
public function doBaz()
139139
{
140140
$this->doFoo();
141+
$this->doLorem();
141142
}
142143

143144
}
@@ -147,4 +148,9 @@ class UsingFooTrait
147148

148149
use FooTrait;
149150

151+
private function doLorem()
152+
{
153+
154+
}
155+
150156
}

0 commit comments

Comments
 (0)