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 12, 2021
1 parent 4f5dacf commit e0f5a41
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 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 @@ -2122,6 +2123,12 @@ static function () use ($scope, $expr): MutatingScope {
} elseif ($argValueType instanceof ConstantStringType) {
$scopeClass = $argValueType->getValue();
$thisType = new ObjectType($scopeClass);
} elseif (
$argValueType instanceof GenericClassStringType
&& $argValueType->getGenericType() instanceof ConstantStringType
) {
$scopeClass = $argValueType->getGenericType()->getValue();
$thisType = new ObjectType($scopeClass);
}
}
$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 e0f5a41

Please sign in to comment.