Skip to content

Commit 95f7a4d

Browse files
authored
ConstantArrayType: prevent unnecessary work
1 parent 085d75e commit 95f7a4d

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,15 @@ public function findTypeAndMethodNames(): array
564564
$has->yes()
565565
&& !$phpVersion->supportsCallableInstanceMethods()
566566
) {
567-
$methodReflection = $type->getMethod($methodName->getValue(), new OutOfClassScope());
568-
if ($classOrObject->isString()->yes() && !$methodReflection->isStatic()) {
569-
continue;
567+
$isString = $classOrObject->isString();
568+
if ($isString->yes()) {
569+
$methodReflection = $type->getMethod($methodName->getValue(), new OutOfClassScope());
570+
571+
if (!$methodReflection->isStatic()) {
572+
continue;
573+
}
574+
} elseif ($isString->maybe()) {
575+
$has = $has->and(TrinaryLogic::createMaybe());
570576
}
571577
}
572578

tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,17 @@ public function testPipeOperator(): void
353353
]);
354354
}
355355

356+
public function testMaybeNotCallable(): void
357+
{
358+
$errors = [];
359+
if (PHP_VERSION_ID >= 80000) {
360+
$errors[] = [
361+
"Trying to invoke array{'MaybeNotCallable\\\Bar'|\$this(MaybeNotCallable\Bar), 'doFoo'} but it might not be a callable.",
362+
15,
363+
];
364+
}
365+
366+
$this->analyse([__DIR__ . '/data/maybe-not-callable.php'], $errors);
367+
}
368+
356369
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace MaybeNotCallable;
4+
5+
class Bar
6+
{
7+
private function doFoo()
8+
{
9+
echo 'yes';
10+
}
11+
12+
public function doBar()
13+
{
14+
$cb = [rand(0,1) ? 'MaybeNotCallable\Bar' : $this, 'doFoo'];
15+
$cb();
16+
}
17+
}

0 commit comments

Comments
 (0)