Skip to content

Commit

Permalink
Detect calling static method on a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 16, 2020
1 parent ecef44f commit 67fc8f3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Rules/Methods/CallStaticMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ public function processNode(Node $node, Scope $scope): array
}

$classReflection = $this->reflectionProvider->getClass($className);
if ($classReflection->isTrait()) {
return [
RuleErrorBuilder::message(sprintf(
'Call to static method %s() on trait %s.',
$methodName,
$className
))->build(),
];
}
}

$className = $classReflection->getName();
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ public function testCallStaticMethods(): void
'Call to an undefined static method CallStaticMethods\Foo::nonexistentMethod().',
303,
],
[
'Call to static method doFoo() on trait CallStaticMethods\TraitWithStaticMethod.',
323,
],
]);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Methods/data/call-static-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,23 @@ public function doFoo(
}

}

trait TraitWithStaticMethod
{

public static function doFoo(): void
{

}

}

class MethodCallingTraitWithStaticMethod
{

public function doFoo(): void
{
TraitWithStaticMethod::doFoo();
}

}

0 comments on commit 67fc8f3

Please sign in to comment.