Skip to content
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
30 changes: 27 additions & 3 deletions src/Rules/Methods/CallMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use PHPStan\Internal\SprintfHelper;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Type\Constant\ConstantStringType;
use function array_map;
use function array_merge;

/**
Expand All @@ -31,12 +34,33 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Identifier) {
return [];
if ($node->name instanceof Node\Identifier) {
$methodNames = [$node->name->name];
} else {
Comment on lines +37 to +39
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see there are a lot of rules which early exit on :

if (!$node->name instanceof Node\Identifier) {
			return [];
}

maybe these are easy to enhance with a similar check I did here.
(just supporting the getConstantStrings() case)

$methodNames = array_map(
static fn (ConstantStringType $constantString): string => $constantString->getValue(),
$scope->getType($node->name)->getConstantStrings(),
);
if ($methodNames === []) {
return [];
}
}

$methodName = $node->name->name;
foreach ($methodNames as $methodName) {
$errors = $this->processMethod($methodName, $node, $scope);
if ($errors !== []) {
return $errors;
}
}

return [];
}

/**
* @return list<IdentifierRuleError>
*/
private function processMethod(string $methodName, MethodCall $node, Scope $scope): array
{
[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodName, $node->var);
if ($methodReflection === null) {
return $errors;
Expand Down
31 changes: 28 additions & 3 deletions src/Rules/Methods/CallStaticMethodsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use PHPStan\Internal\SprintfHelper;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Type\Constant\ConstantStringType;
use function array_map;
use function array_merge;
use function sprintf;

Expand All @@ -32,11 +35,33 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Identifier) {
return [];
if ($node->name instanceof Node\Identifier) {
$methodNames = [$node->name->name];
} else {
$methodNames = array_map(
static fn (ConstantStringType $constantString): string => $constantString->getValue(),
$scope->getType($node->name)->getConstantStrings(),
);
if ($methodNames === []) {
return [];
}
}
$methodName = $node->name->name;

foreach ($methodNames as $methodName) {
$errors = $this->processMethod($methodName, $node, $scope);
if ($errors !== []) {
return $errors;
}
}

return [];
}

/**
* @return list<IdentifierRuleError>
*/
private function processMethod(string $methodName, StaticCall $node, Scope $scope): array
{
[$errors, $method] = $this->methodCallCheck->check($scope, $methodName, $node->class);
if ($method === null) {
return $errors;
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3412,4 +3412,26 @@ public function testBug1953(): void
]);
}

public function testBug2920(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-2920.php'], [
[
'Call to an undefined method Bug2920MethodCall\HelloWorld::a().',
13,
],
[
'Parameter #1 $s of method Bug2920MethodCall\HelloWorld::b() expects string, int given.',
14,
],
[
'Parameter #1 $s of method Bug2920MethodCall\HelloWorld::b() expects string, int given.',
17,
],
]);
}

}
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,4 +852,26 @@ public function testBug10872(): void
$this->analyse([__DIR__ . '/data/bug-10872.php'], []);
}

public function testBug2920(): void
{
$this->checkThisOnly = false;
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;

$this->analyse([__DIR__ . '/data/bug-2920b.php'], [
[
'Call to an undefined static method Bug2920bStaticCall\HelloWorld::a().',
13,
],
[
'Parameter #1 $s of static method Bug2920bStaticCall\HelloWorld::b() expects string, int given.',
14,
],
[
'Parameter #1 $s of static method Bug2920bStaticCall\HelloWorld::b() expects string, int given.',
17,
],
]);
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-2920.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Bug2920MethodCall;


class HelloWorld
{
public function sayHello(): void
{
$a = 'a';
$b = 'b';

$this->$a();
$this->$b(1);
$this->$b("1");

$this->b(1);
$this->b("1");
}

public function b(string $s): void
{
}

}

26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-2920b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Bug2920bStaticCall;


class HelloWorld
{
public function sayHello(): void
{
$a = 'a';
$b = 'b';

self::$a();
self::$b(1);
self::$b("1");

self::b(1);
self::b("1");
}

static public function b(string $s): void
{
}

}