Skip to content
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

Fix 4949, support closure bind with class-string #706

Merged
merged 1 commit into from Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -118,6 +118,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 All @@ -132,6 +133,7 @@
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType;
use Throwable;
Expand Down Expand Up @@ -2065,17 +2067,15 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
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 TypeWithClassName
) {
$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 @@ -873,7 +873,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