Skip to content

Commit

Permalink
DynamicCallOnStaticMethodsRule - do not report for methods declared o…
Browse files Browse the repository at this point in the history
…n PHPStanTestCase and TypeInferenceTestCase
  • Loading branch information
ondrejmirtes committed Mar 29, 2023
1 parent ce25f06 commit b21c03d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Testing\TypeInferenceTestCase;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use function in_array;
use function sprintf;

class DynamicCallOnStaticMethodsRule implements Rule
Expand Down Expand Up @@ -53,6 +56,14 @@ static function (Type $type) use ($name): bool {

$methodReflection = $type->getMethod($name, $scope);
if ($methodReflection->isStatic()) {
$prototype = $methodReflection->getPrototype();
if (in_array($prototype->getDeclaringClass()->getName(), [
TypeInferenceTestCase::class,
PHPStanTestCase::class,
], true)) {
return [];
}

return [sprintf(
'Dynamic call to static method %s::%s().',
$methodReflection->getDeclaringClass()->getDisplayName(),
Expand Down
13 changes: 13 additions & 0 deletions tests/Rules/StrictCalls/data/dynamic-calls-on-static-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ function () {
$classUsingTrait->foo();
$classUsingTrait->bar();
};

class FooTest extends \PHPStan\Testing\TypeInferenceTestCase
{

public function doFoo(): void
{
self::gatherAssertTypes(__FILE__);
$this->gatherAssertTypes(__FILE__);
self::getContainer();
$this->getContainer();
}

}

0 comments on commit b21c03d

Please sign in to comment.