Skip to content

Rule for checking a dynamic call on static methods #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Functions `in_array` and `array_search` must be called with third parameter `$strict` set to `true` to search values with matching types only.
* Variables assigned in `while` loop condition and `for` loop initial assignment cannot be used after the loop.
* Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results.
* Statically declared methods are called statically.

Additional rules are coming in subsequent releases!

Expand Down
5 changes: 5 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ services:
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\StrictCalls\StrictFunctionCallsRule
tags:
Expand Down
67 changes: 67 additions & 0 deletions src/Rules/StrictCalls/DynamicCallOnStaticMethodsRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\StrictCalls;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;

class DynamicCallOnStaticMethodsRule implements \PHPStan\Rules\Rule
{

/**
* @var \PHPStan\Rules\RuleLevelHelper
*/
private $ruleLevelHelper;

public function __construct(RuleLevelHelper $ruleLevelHelper)
{
$this->ruleLevelHelper = $ruleLevelHelper;
}

public function getNodeType(): string
{
return MethodCall::class;
}

/**
* @param \PhpParser\Node\Expr\MethodCall $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
if (!is_string($node->name)) {
return [];
}

$name = $node->name;
$type = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->var,
''
)->getType();

if ($type instanceof ErrorType || !$type->canCallMethods() || !$type->hasMethod($name)) {
return [];
}

$methodReflection = $type->getMethod($name, $scope);
if (!$scope->canCallMethod($methodReflection)) {
return [];
}

if ($methodReflection->isStatic()) {
return [sprintf(
'Dynamic call to static method %s::%s().',
$methodReflection->getDeclaringClass()->getDisplayName(),
$methodReflection->getName()
)];
}

return [];
}

}
61 changes: 61 additions & 0 deletions tests/Rules/StrictCalls/DynamicCallOnStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\StrictCalls;

use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;

class DynamicCallOnStaticMethodsRuleTest extends \PHPStan\Testing\RuleTestCase
{

/**
* @var bool
*/
private $checkThisOnly;

protected function getRule(): Rule
{
$broker = $this->createBroker();
$ruleLevelHelper = new RuleLevelHelper($broker, true, $this->checkThisOnly, true);
return new DynamicCallOnStaticMethodsRule($ruleLevelHelper);
}

public function testRule()
{
$this->checkThisOnly = false;
$this->analyse([__DIR__ . '/data/dynamic-calls-on-static-methods.php'], [
[
'Dynamic call to static method StrictCalls\ClassWithStaticMethod::foo().',
14,
],
[
'Dynamic call to static method StrictCalls\ClassWithStaticMethod::foo().',
20,
],
[
'Dynamic call to static method StrictCalls\ClassUsingTrait::foo().',
32,
],
[
'Dynamic call to static method StrictCalls\ClassUsingTrait::foo().',
43,
],
]);
}

public function testRuleOnThisOnly()
{
$this->checkThisOnly = true;
$this->analyse([__DIR__ . '/data/dynamic-calls-on-static-methods.php'], [
[
'Dynamic call to static method StrictCalls\ClassWithStaticMethod::foo().',
14,
],
[
'Dynamic call to static method StrictCalls\ClassUsingTrait::foo().',
32,
],
]);
}

}
44 changes: 44 additions & 0 deletions tests/Rules/StrictCalls/data/dynamic-calls-on-static-methods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace StrictCalls;

class ClassWithStaticMethod
{
public static function foo()
{

}

public function bar()
{
$this->foo();
}
}

function () {
$classWithStaticMethod = new ClassWithStaticMethod();
$classWithStaticMethod->foo();
};

trait TraitWithStaticMethod
{
public static function foo()
{

}

public function bar()
{
$this->foo();
}
}

class ClassUsingTrait
{
use TraitWithStaticMethod;
}

function () {
$classUsingTrait = new ClassUsingTrait();
$classUsingTrait->foo();
};