Skip to content

Commit 566b44b

Browse files
committed
Fix Closure::bind()
1 parent 8b2e3f0 commit 566b44b

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,15 +2056,18 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
20562056
$directClassNames = TypeUtils::getDirectClassNames($argValueType);
20572057
if (count($directClassNames) === 1) {
20582058
$scopeClass = $directClassNames[0];
2059+
$thisType = new ObjectType($scopeClass);
20592060
} elseif (
20602061
$argValue instanceof Expr\ClassConstFetch
20612062
&& $argValue->name instanceof Node\Identifier
20622063
&& strtolower($argValue->name->name) === 'class'
20632064
&& $argValue->class instanceof Name
20642065
) {
20652066
$scopeClass = $scope->resolveName($argValue->class);
2067+
$thisType = new ObjectType($scopeClass);
20662068
} elseif ($argValueType instanceof ConstantStringType) {
20672069
$scopeClass = $argValueType->getValue();
2070+
$thisType = new ObjectType($scopeClass);
20682071
}
20692072
}
20702073
$closureBindScope = $scope->enterClosureBind($thisType, $scopeClass);

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,10 @@ public function testClosureBind(): void
822822
'Call to an undefined method CallClosureBind\Foo::nonexistentMethod().',
823823
19,
824824
],
825+
[
826+
'Call to an undefined method CallClosureBind\Bar::barMethod().',
827+
23,
828+
],
825829
[
826830
'Call to an undefined method CallClosureBind\Foo::nonexistentMethod().',
827831
28,
@@ -850,6 +854,10 @@ public function testArrowFunctionClosureBind(): void
850854
'Call to an undefined method CallArrowFunctionBind\Foo::nonexistentMethod().',
851855
27,
852856
],
857+
[
858+
'Call to an undefined method CallArrowFunctionBind\Bar::barMethod().',
859+
29,
860+
],
853861
[
854862
'Call to an undefined method CallArrowFunctionBind\Foo::nonexistentMethod().',
855863
31,

tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,11 @@ public function testBug4527(): void
484484
$this->analyse([__DIR__ . '/data/bug-4527.php'], []);
485485
}
486486

487+
public function testBug4808(): void
488+
{
489+
$this->checkThisOnly = false;
490+
$this->checkUnionTypes = true;
491+
$this->analyse([__DIR__ . '/data/bug-4808.php'], []);
492+
}
493+
487494
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Bug4808;
4+
5+
class A
6+
{
7+
/** @var bool */
8+
private $x = true;
9+
10+
public function preventUnusedVariable(): bool
11+
{
12+
return $this->x;
13+
}
14+
}
15+
16+
class B extends A
17+
{
18+
public function getPrivateProp(): bool
19+
{
20+
return \Closure::bind(function () {
21+
return $this->x;
22+
}, $this, A::class)();
23+
}
24+
}

0 commit comments

Comments
 (0)