Skip to content

Commit c2e1a47

Browse files
zonuexeclaude
andcommitted
Allow self:: calls to instance methods of a closure's bound $this
A closure bound with Closure::bind($closure, $object, Foo::class) may call Foo's instance methods through self::/static::, also at the top level and in functions: PHP forwards the bound $this. StaticMethodCallCheck only accepted that inside a non-static method of the class. Inside a closure-bind scope with a defined $this, decide by whether $this is the method's class or a subclass of it instead. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 92a6bb2 commit c2e1a47

4 files changed

Lines changed: 86 additions & 29 deletions

File tree

‎src/Rules/Methods/StaticMethodCallCheck.php‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use PHPStan\TrinaryLogic;
2828
use PHPStan\Type\ErrorType;
2929
use PHPStan\Type\Generic\GenericClassStringType;
30+
use PHPStan\Type\ObjectType;
3031
use PHPStan\Type\StaticType;
3132
use PHPStan\Type\StringType;
3233
use PHPStan\Type\Type;
@@ -281,24 +282,28 @@ public function check(
281282
|| $function->isStatic()
282283
|| $scopeIsInMethodClassOrSubClass->no()
283284
) {
284-
// per php-src docs, this method can be called statically, even if declared non-static
285-
if (strtolower($method->getName()) === 'loadhtml' && $method->getDeclaringClass()->getName() === DOMDocument::class) {
286-
return [[], null];
287-
}
285+
// a closure bound with a $this of the method's class calls its instance
286+
// methods through self::/static:: as well
287+
if (!self::isCalledOnBoundThis($scope, $classType)) {
288+
// per php-src docs, this method can be called statically, even if declared non-static
289+
if (strtolower($method->getName()) === 'loadhtml' && $method->getDeclaringClass()->getName() === DOMDocument::class) {
290+
return [[], null];
291+
}
288292

289-
return [
290-
array_merge($errors, [
291-
RuleErrorBuilder::message(sprintf(
292-
'Static call to instance method %s::%s().',
293-
$method->getDeclaringClass()->getDisplayName(),
294-
$method->getName(),
295-
))
296-
->line($astName->getStartLine())
297-
->identifier('method.staticCall')
298-
->build(),
299-
]),
300-
$method,
301-
];
293+
return [
294+
array_merge($errors, [
295+
RuleErrorBuilder::message(sprintf(
296+
'Static call to instance method %s::%s().',
297+
$method->getDeclaringClass()->getDisplayName(),
298+
$method->getName(),
299+
))
300+
->line($astName->getStartLine())
301+
->identifier('method.staticCall')
302+
->build(),
303+
]),
304+
$method,
305+
];
306+
}
302307
}
303308
}
304309

@@ -359,4 +364,26 @@ public function check(
359364
return [$errors, $method];
360365
}
361366

367+
/**
368+
* Whether the scope is a closure bound with a `$this` that is certainly an instance of
369+
* the classes of the static call. An undefined `$this` reads as an error type and a
370+
* possibly defined one as mixed, neither of which is certainly such an instance.
371+
*/
372+
private static function isCalledOnBoundThis(Scope $scope, Type $classType): bool
373+
{
374+
$objectClassNames = $classType->getObjectClassNames();
375+
if ($objectClassNames === [] || !$scope->isInClosureBind()) {
376+
return false;
377+
}
378+
379+
$boundThisType = $scope->getType(new Expr\Variable('this'));
380+
foreach ($objectClassNames as $objectClassName) {
381+
if (!(new ObjectType($objectClassName))->isSuperTypeOf($boundThisType)->yes()) {
382+
return false;
383+
}
384+
}
385+
386+
return true;
387+
}
388+
362389
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,35 +1169,43 @@ public function testClosureBindScope(): void
11691169
$this->analyse([__DIR__ . '/data/closure-bind-scope.php'], [
11701170
[
11711171
'Call to private static method psm() of class ClosureBindScopeMethods\\Foo.',
1172-
48,
1172+
53,
11731173
],
11741174
[
11751175
'Calling parent::s() but ClosureBindScopeMethods\\NoParent does not extend any class.',
1176-
51,
1176+
56,
11771177
],
11781178
[
11791179
'Call to an undefined static method ClosureBindScopeMethods\\Foo::nope().',
1180-
54,
1180+
59,
11811181
],
11821182
[
11831183
'Calling self::sm() outside of class scope.',
1184-
57,
1184+
62,
11851185
],
11861186
[
11871187
'Calling parent::sm() outside of class scope.',
1188-
57,
1188+
62,
11891189
],
11901190
[
11911191
'Calling static::sm() outside of class scope.',
1192-
57,
1192+
62,
11931193
],
11941194
[
11951195
'Call to an undefined static method ClosureBindScopeMethods\\Foo::own().',
1196-
71,
1196+
76,
11971197
],
11981198
[
11991199
'Calling parent::s() but ClosureBindScopeMethods\\NoParent does not extend any class.',
1200-
75,
1200+
80,
1201+
],
1202+
[
1203+
'Static call to instance method ClosureBindScopeMethods\\Foo::im().',
1204+
89,
1205+
],
1206+
[
1207+
'Static call to instance method ClosureBindScopeMethods\\Foo::im().',
1208+
91,
12011209
],
12021210
]);
12031211
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,27 @@ public function testClosureBindScope(): void
141141
$this->analyse([__DIR__ . '/data/closure-bind-scope.php'], [
142142
[
143143
'Call to private static method psm() of class ClosureBindScopeMethods\\Foo.',
144-
48,
144+
53,
145145
],
146146
[
147147
'Calling parent::s() but ClosureBindScopeMethods\\NoParent does not extend any class.',
148-
51,
148+
56,
149149
],
150150
[
151151
'Call to an undefined static method ClosureBindScopeMethods\\Foo::nope().',
152-
54,
152+
59,
153153
],
154154
[
155155
'Calling self::sm() outside of class scope.',
156-
57,
156+
62,
157+
],
158+
[
159+
'Static call to instance method ClosureBindScopeMethods\\Foo::im().',
160+
89,
161+
],
162+
[
163+
'Static call to instance method ClosureBindScopeMethods\\Foo::im().',
164+
91,
157165
],
158166
]);
159167
}

‎tests/PHPStan/Rules/Methods/data/closure-bind-scope.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ private static function psm(): string
1919
return 'Foo';
2020
}
2121

22+
public function im(): int
23+
{
24+
return 1;
25+
}
26+
2227
}
2328

2429
final class Bar extends Foo
@@ -76,3 +81,12 @@ public function run(): void
7681
}
7782

7883
}
84+
85+
function (): void {
86+
// a closure bound with a $this of the method's class calls its instance methods through self::
87+
Closure::bind(fn () => [self::im(), static::im(), self::im(...)], new Foo(), Foo::class);
88+
Closure::bind(fn () => self::im(), new Bar(), Foo::class);
89+
Closure::bind(static fn () => [self::im(), self::im(...)], null, Foo::class);
90+
// ...but not one of a class the bound $this only may be an instance of
91+
Closure::bind(fn () => [Bar::im(), Bar::im(...)], new Foo(), Foo::class);
92+
};

0 commit comments

Comments
 (0)