Skip to content

Commit

Permalink
Fix 4949, support closure bind with class-string
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Oct 13, 2021
1 parent 2b3ee60 commit 97b71e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -105,6 +105,7 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\IntegerType;
Expand Down Expand Up @@ -2111,17 +2112,15 @@ static function () use ($scope, $expr): MutatingScope {
if (count($directClassNames) === 1) {
$scopeClass = $directClassNames[0];
$thisType = new ObjectType($scopeClass);
} elseif (
$argValue instanceof Expr\ClassConstFetch
&& $argValue->name instanceof Node\Identifier
&& strtolower($argValue->name->name) === 'class'
&& $argValue->class instanceof Name
) {
$scopeClass = $scope->resolveName($argValue->class);
$thisType = new ObjectType($scopeClass);
} elseif ($argValueType instanceof ConstantStringType) {
$scopeClass = $argValueType->getValue();
$thisType = new ObjectType($scopeClass);
} elseif (
$argValueType instanceof GenericClassStringType
&& $argValueType->getGenericType() instanceof ObjectType
) {
$scopeClass = $argValueType->getGenericType()->getClassName();
$thisType = $argValueType->getGenericType();
}
}
$closureBindScope = $scope->enterClosureBind($thisType, $scopeClass);
Expand Down
6 changes: 5 additions & 1 deletion tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Expand Up @@ -863,7 +863,11 @@ public function testClosureBind(): void
],
[
'Call to an undefined method CallClosureBind\Foo::nonexistentMethod().',
39,
38,
],
[
'Call to an undefined method CallClosureBind\Foo::nonexistentMethod().',
44,
],
]);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/data/closure-bind.php
Expand Up @@ -33,6 +33,11 @@ public function fooMethod(): Foo
$foo->nonexistentMethod();
}, null, new Foo());

\Closure::bind(function (Foo $foo) {
$foo->privateMethod();
$foo->nonexistentMethod();
}, null, get_class(new Foo()));

\Closure::bind(function () {
// $this is Foo
$this->privateMethod();
Expand Down

0 comments on commit 97b71e4

Please sign in to comment.