Skip to content

Commit de75ab4

Browse files
committed
Use scope by method name
1 parent c5e320a commit de75ab4

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/Rules/Methods/CallMethodsRule.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
namespace PHPStan\Rules\Methods;
44

55
use PhpParser\Node;
6+
use PhpParser\Node\Expr\BinaryOp\Identical;
67
use PhpParser\Node\Expr\MethodCall;
8+
use PhpParser\Node\Scalar\String_;
79
use PHPStan\Analyser\Scope;
810
use PHPStan\Internal\SprintfHelper;
911
use PHPStan\Reflection\ParametersAcceptorSelector;
1012
use PHPStan\Rules\FunctionCallParametersCheck;
1113
use PHPStan\Rules\IdentifierRuleError;
1214
use PHPStan\Rules\Rule;
1315
use PHPStan\Type\Constant\ConstantStringType;
16+
use function array_column;
1417
use function array_map;
1518
use function array_merge;
1619

@@ -36,14 +39,22 @@ public function processNode(Node $node, Scope $scope): array
3639
{
3740
$errors = [];
3841
if ($node->name instanceof Node\Identifier) {
39-
$methodNames = [$node->name->name];
42+
$methodNameScopes = [$node->name->name => $scope];
4043
} else {
41-
$callType = $scope->getType($node->name);
42-
$methodNames = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $callType->getConstantStrings());
44+
$nameType = $scope->getType($node->name);
45+
$methodNames = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $nameType->getConstantStrings());
46+
$methodNameScopes = array_column(array_map(
47+
static fn (ConstantStringType $constantString) => [
48+
$name = $constantString->getValue(),
49+
$scope->filterByTruthyValue(new Identical($node->name, new String_($name))),
50+
],
51+
$nameType->getConstantStrings(),
52+
), 1, 0);
53+
4354
}
4455

45-
foreach ($methodNames as $methodName) {
46-
$errors = array_merge($errors, $this->processSingleMethodCall($scope, $node, $methodName));
56+
foreach ($methodNameScopes as $methodName => $methodScope) {
57+
$errors = array_merge($errors, $this->processSingleMethodCall($methodScope, $node, $methodName));
4758
}
4859

4960
return $errors;

0 commit comments

Comments
 (0)