Skip to content

Commit 67fc8f3

Browse files
committed
Detect calling static method on a trait
1 parent ecef44f commit 67fc8f3

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/Rules/Methods/CallStaticMethodsRule.php

+9
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ public function processNode(Node $node, Scope $scope): array
138138
}
139139

140140
$classReflection = $this->reflectionProvider->getClass($className);
141+
if ($classReflection->isTrait()) {
142+
return [
143+
RuleErrorBuilder::message(sprintf(
144+
'Call to static method %s() on trait %s.',
145+
$methodName,
146+
$className
147+
))->build(),
148+
];
149+
}
141150
}
142151

143152
$className = $classReflection->getName();

tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ public function testCallStaticMethods(): void
216216
'Call to an undefined static method CallStaticMethods\Foo::nonexistentMethod().',
217217
303,
218218
],
219+
[
220+
'Call to static method doFoo() on trait CallStaticMethods\TraitWithStaticMethod.',
221+
323,
222+
],
219223
]);
220224
}
221225

tests/PHPStan/Rules/Methods/data/call-static-methods.php

+20
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,23 @@ public function doFoo(
304304
}
305305

306306
}
307+
308+
trait TraitWithStaticMethod
309+
{
310+
311+
public static function doFoo(): void
312+
{
313+
314+
}
315+
316+
}
317+
318+
class MethodCallingTraitWithStaticMethod
319+
{
320+
321+
public function doFoo(): void
322+
{
323+
TraitWithStaticMethod::doFoo();
324+
}
325+
326+
}

0 commit comments

Comments
 (0)