Skip to content

Commit 5958c29

Browse files
committed
Do not report unused private methods/properties in traits
1 parent cdbed86 commit 5958c29

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ private function processStmtNode(
585585
if (!$scope->isInClass()) {
586586
throw new \PHPStan\ShouldNotHappenException();
587587
}
588+
if ($scope->isInTrait()) {
589+
return;
590+
}
588591
if ($scope->getClassReflection()->getName() !== $classReflection->getName()) {
589592
return;
590593
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,30 @@ public function doBar(string $s)
121121
}
122122

123123
}
124+
125+
trait FooTrait
126+
{
127+
128+
private function doFoo()
129+
{
130+
131+
}
132+
133+
private function doBar()
134+
{
135+
136+
}
137+
138+
public function doBaz()
139+
{
140+
$this->doFoo();
141+
}
142+
143+
}
144+
145+
class UsingFooTrait
146+
{
147+
148+
use FooTrait;
149+
150+
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,29 @@ class TextExtension
154154
private $used;
155155

156156
}
157+
158+
trait FooTrait
159+
{
160+
161+
private $prop1;
162+
163+
private $prop2;
164+
165+
public function doFoo()
166+
{
167+
echo $this->prop1;
168+
}
169+
170+
public function setFoo($prop1)
171+
{
172+
$this->prop1 = $prop1;
173+
}
174+
175+
}
176+
177+
class ClassUsingTrait
178+
{
179+
180+
use FooTrait;
181+
182+
}

0 commit comments

Comments
 (0)