Skip to content

Commit

Permalink
Fix Closure::bind()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 5, 2021
1 parent 8b2e3f0 commit 566b44b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2056,15 +2056,18 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
$directClassNames = TypeUtils::getDirectClassNames($argValueType);
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);
}
}
$closureBindScope = $scope->enterClosureBind($thisType, $scopeClass);
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,10 @@ public function testClosureBind(): void
'Call to an undefined method CallClosureBind\Foo::nonexistentMethod().',
19,
],
[
'Call to an undefined method CallClosureBind\Bar::barMethod().',
23,
],
[
'Call to an undefined method CallClosureBind\Foo::nonexistentMethod().',
28,
Expand Down Expand Up @@ -850,6 +854,10 @@ public function testArrowFunctionClosureBind(): void
'Call to an undefined method CallArrowFunctionBind\Foo::nonexistentMethod().',
27,
],
[
'Call to an undefined method CallArrowFunctionBind\Bar::barMethod().',
29,
],
[
'Call to an undefined method CallArrowFunctionBind\Foo::nonexistentMethod().',
31,
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,11 @@ public function testBug4527(): void
$this->analyse([__DIR__ . '/data/bug-4527.php'], []);
}

public function testBug4808(): void
{
$this->checkThisOnly = false;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-4808.php'], []);
}

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

namespace Bug4808;

class A
{
/** @var bool */
private $x = true;

public function preventUnusedVariable(): bool
{
return $this->x;
}
}

class B extends A
{
public function getPrivateProp(): bool
{
return \Closure::bind(function () {
return $this->x;
}, $this, A::class)();
}
}

0 comments on commit 566b44b

Please sign in to comment.