Skip to content

Commit a81d289

Browse files
zonuexeclaude
andcommitted
Cover Closure::bind() scopes around nested classes, arguments and closure-this
- A class or function declared inside a bound closure has its own self/parent/static (rule data and a walk-trace fixture). - The bind scope applies to the closure body only: the arguments of the call keep resolving against the enclosing class (regression test for phpstan/phpstan#6319, reproducer from the issue's playground sample). - A closure passed to a @param-closure-this parameter is analysed as scoped to that class, so self:: names it even outside any class. - More parser-visitor snippets with self/parent/static names inside Closure::bind() calls, which the visitor leaves alone. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c2e1a47 commit a81d289

9 files changed

Lines changed: 190 additions & 0 deletions

File tree

‎Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ lint:
2424
--exclude tests/PHPStan/Rules/Arrays/data/offset-access-without-dim-for-reading.php \
2525
--exclude tests/PHPStan/Rules/Classes/data/bug-11891.php \
2626
--exclude tests/PHPStan/Rules/Classes/data/bug-13768.php \
27+
--exclude tests/PHPStan/Rules/Classes/data/closure-bind-scope-nested-class.php \
2728
--exclude tests/PHPStan/Rules/Classes/data/duplicate-declarations.php \
2829
--exclude tests/PHPStan/Rules/Classes/data/duplicate-enum-cases.php \
2930
--exclude tests/PHPStan/Rules/Classes/data/enum-sanity.php \
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ClosureBindScopeParamClosureThis;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
public const A = 'Foo';
11+
12+
}
13+
14+
/**
15+
* @param-closure-this Foo $callback
16+
*/
17+
function withFooThis(\Closure $callback): void
18+
{
19+
}
20+
21+
// The closure is assumed to be scoped to its @param-closure-this class, as
22+
// Closure::call() and Closure::bind($closure, $object, $object) would scope it,
23+
// so self:: names that class even outside any class.
24+
withFooThis(function (): void {
25+
assertType('ClosureBindScopeParamClosureThis\Foo', $this);
26+
assertType("'Foo'", self::A);
27+
assertType('ClosureBindScopeParamClosureThis\Foo', new self());
28+
});

‎tests/PHPStan/Rules/Classes/ClassConstantRuleTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,16 @@ public function testClosureBindScope(): void
667667
]);
668668
}
669669

670+
public function testClosureBindScopeNestedClass(): void
671+
{
672+
$this->analyse([__DIR__ . '/data/closure-bind-scope-nested-class.php'], [
673+
[
674+
'Using self outside of class scope.',
675+
48,
676+
],
677+
]);
678+
}
679+
670680
public function testClosureBindScopeAmbiguous(): void
671681
{
672682
$this->analyse([__DIR__ . '/data/closure-bind-scope-ambiguous.php'], [

‎tests/PHPStan/Rules/Classes/InstantiationRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,11 @@ public function testClosureBindScope(): void
840840
]);
841841
}
842842

843+
public function testClosureBindScopeNestedClass(): void
844+
{
845+
$this->analyse([__DIR__ . '/data/closure-bind-scope-nested-class.php'], []);
846+
}
847+
843848
public function testClosureBindScopeAmbiguous(): void
844849
{
845850
$this->analyse([__DIR__ . '/data/closure-bind-scope-ambiguous.php'], [
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ClosureBindScopeNestedClass;
4+
5+
use Closure;
6+
7+
class Foo
8+
{
9+
10+
protected const A = 'Foo';
11+
12+
}
13+
14+
// a class declared inside a bound closure has its own self/parent/static
15+
$o = Closure::bind(static function () {
16+
return new class {
17+
18+
public const Z = 'z';
19+
20+
public function f(): string
21+
{
22+
return self::Z;
23+
}
24+
25+
public static function g(): string
26+
{
27+
return static::Z;
28+
}
29+
30+
public function h(): self
31+
{
32+
return new self();
33+
}
34+
35+
public function i(): static
36+
{
37+
return new static();
38+
}
39+
40+
};
41+
}, null, Foo::class)();
42+
43+
// and so does a function declared inside one
44+
Closure::bind(static function (): void {
45+
if (!function_exists('ClosureBindScopeNestedClass\\inner')) {
46+
function inner(): string
47+
{
48+
return self::A;
49+
}
50+
}
51+
}, null, Foo::class);
52+
53+
class Container
54+
{
55+
56+
public function run(): void
57+
{
58+
Closure::bind(static function () {
59+
return new class extends Foo {
60+
61+
public function k(): string
62+
{
63+
return self::A . parent::A;
64+
}
65+
66+
public function l(): parent
67+
{
68+
return new parent();
69+
}
70+
71+
};
72+
}, null, Container::class)();
73+
}
74+
75+
}

‎tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,12 @@ public function testClosureBindScope(): void
12101210
]);
12111211
}
12121212

1213+
public function testBug6319(): void
1214+
{
1215+
$this->checkThisOnly = false;
1216+
$this->analyse([__DIR__ . '/data/bug-6319.php'], []);
1217+
}
1218+
12131219
#[RequiresPhp('>= 8.1.0')]
12141220
public function testClosureBindScopeAmbiguous(): void
12151221
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug6319;
4+
5+
class A {}
6+
7+
class HelloWorld
8+
{
9+
public static function isLower(): bool
10+
{
11+
return true;
12+
}
13+
14+
public function sayHello(): string
15+
{
16+
return \Closure::bind(self::isLower() ? fn () => 'LC' : fn () => 'not-LC', null, A::class)();
17+
}
18+
}
19+
20+
var_dump(
21+
(new HelloWorld())->sayHello()
22+
);

‎turbo-ext/tests/parser-visitors.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,26 @@ function f() {
135135
$y instanceof Qux;
136136
TypeTraverser::map($t, $cb);
137137
\PHPStan\Type\TypeTraverser::other($t, $cb);',
138+
139+
// the closure argument of Closure::bind() calls around self/parent/static
140+
// names, which the visitor leaves alone (the bound class comes from the scope)
141+
'<?php
142+
self::A; Closure::bind(function () { return [self::A, static::b(), new parent(), Other::C]; }, null, Foo::class);
143+
Closure::bind(fn () => SELF::$p, $obj); \Closure::bind(static function () { return parent::X; }, null, self::class);
144+
Closure::bind(function () { return Closure::bind(fn () => static::B, null, Inner::class)() + self::C; }, null, Outer::class);
145+
Closure::bind($callable, null, Foo::class); Closure::bind(...); static::D;',
146+
// ... with the arguments found by name as well as by position
147+
'<?php
148+
Closure::bind(closure: fn () => self::A, newScope: Foo::class, newThis: null);
149+
Closure::bind(newScope: Bar::class, closure: function () { return static::B; });
150+
Closure::bind(function () { return parent::C; }, newScope: Baz::class); Closure::bind(fn () => self::D, $obj, newScope: Qux::class);
151+
Closure::bind(closure: fn () => self::E); Closure::bind(Closure: fn () => self::F, newscope: X::class, other: 1);',
152+
// ... duplicated, unpacked and nested arguments
153+
'<?php
154+
Closure::bind(fn () => 1, $o, Foo::class, newScope: Bar::class); Closure::bind($c, $o, closure: $d, newThis: $p);
155+
Closure::bind(...$a, $o); Closure::bind(...$a); Closure::bind($c, ...$rest); Closure::bind(newThis: $o, closure: $c);
156+
Closure::bind(fn () => 1, $o, Closure::bind(fn () => self::class, $p, X::class)());
157+
Closure::bind(Closure::bind(fn () => 1, $o, X::class), $p, Y::class);',
138158
];
139159

140160
// plus real source files, so the ports meet whatever the repository contains
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace WalkTraceClosureBindScopeNestedClass;
4+
5+
// a class or function declared inside a closure bound with Closure::bind()
6+
// has its own self/parent/static: entering it resets the bound class
7+
class Foo
8+
{
9+
protected const A = 'Foo';
10+
}
11+
12+
\Closure::bind(static fn () => new class extends Foo {
13+
public const Z = 'z';
14+
public function f(): array { return [self::Z, static::Z, self::A, parent::A, new self(), new static(), new parent()]; }
15+
}, null, Foo::class)();
16+
17+
\Closure::bind(static function (): void {
18+
function walkTraceClosureBindInner(): void
19+
{
20+
echo self::A;
21+
}
22+
(static fn () => [self::A, new self()])();
23+
}, null, Foo::class)();

0 commit comments

Comments
 (0)