Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ public function equals(Type $type): bool

public function isCallable(): TrinaryLogic
{
$typeAndMethods = $this->findTypeAndMethodNames();
$hasNonExistentMethod = false;
$typeAndMethods = $this->doFindTypeAndMethodNames($hasNonExistentMethod);
if ($typeAndMethods === []) {
return TrinaryLogic::createNo();
}
Expand All @@ -504,7 +505,13 @@ public function isCallable(): TrinaryLogic
$typeAndMethods,
);

return TrinaryLogic::createYes()->and(...$results);
$result = TrinaryLogic::createYes()->and(...$results);

if ($hasNonExistentMethod) {
$result = $result->and(TrinaryLogic::createMaybe());
}

return $result;
}

public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
Expand Down Expand Up @@ -537,6 +544,12 @@ public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope)

/** @return ConstantArrayTypeAndMethod[] */
public function findTypeAndMethodNames(): array
{
return $this->doFindTypeAndMethodNames();
}

/** @return ConstantArrayTypeAndMethod[] */
private function doFindTypeAndMethodNames(bool &$hasNonExistentMethod = false): array
{
if (count($this->keyTypes) !== 2) {
return [];
Expand Down Expand Up @@ -578,6 +591,7 @@ public function findTypeAndMethodNames(): array
foreach ($methods->getConstantStrings() as $methodName) {
$has = $type->hasMethod($methodName->getValue());
if ($has->no()) {
$hasNonExistentMethod = true;
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1214,4 +1214,10 @@ public function testBug13799(): void
]);
}

public function testBug12063(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-12063.php'], []);
}

}
38 changes: 38 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-12063.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Bug12063;

use BadFunctionCallException;

final class View
{
public function existingMethod(): void
{
}
}

final class TwigExtension
{
private View $viewFunctions;

public function __construct(View $viewFunctions)
{
$this->viewFunctions = $viewFunctions;
}

public function iterateFunctions(): void
{
$functionMappings = [
'i_exist' => 'existingMethod',
'i_dont_exist' => 'nonExistingMethod'
];

$functions = [];
foreach ($functionMappings as $nameFrom => $nameTo) {
$callable = [$this->viewFunctions, $nameTo];
if (!is_callable($callable)) {
throw new BadFunctionCallException("Function $nameTo does not exist in view functions");
}
}
}
}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ public function testPipeOperator(): void
]);
}

public function testBug4608(): void
{
$this->analyse([__DIR__ . '/data/bug-4608-callables.php'], [
[
"Trying to invoke array{class@anonymous/tests/PHPStan/Rules/Functions/data/bug-4608-callables.php:5, 'abc'|'not_abc'} but it might not be a callable.",
11,
],
]);
}

public function testMaybeNotCallable(): void
{
$errors = [];
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2817,4 +2817,15 @@ public function testBug14312b(): void
$this->analyse([__DIR__ . '/data/bug-14312b.php'], []);
}

public function testBug4608(): void
{
$paramName = PHP_VERSION_ID >= 80000 ? 'callback' : 'function';
$this->analyse([__DIR__ . '/data/bug-4608-call-user-func.php'], [
[
sprintf("Parameter #1 \$%s of function call_user_func expects callable(): mixed, array{class@anonymous/tests/PHPStan/Rules/Functions/data/bug-4608-call-user-func.php:5, 'abc'|'not_abc'} given.", $paramName),
11,
],
]);
}

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-4608-call-user-func.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Bug4608CallUserFunc;

$c = new class {
public function abc(): void {}
};

$s = rand(0, 1) ? 'abc' : 'not_abc';

call_user_func([$c, $s]);
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-4608-callables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Bug4608Callables;

$c = new class {
public function abc(): void {}
};

$s = rand(0, 1) ? 'abc' : 'not_abc';

[$c, $s]();
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3962,4 +3962,17 @@ public function testBug11463(): void
]);
}

public function testBug4608(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-4608.php'], [
[
'Call to an undefined method class@anonymous/tests/PHPStan/Rules/Methods/data/bug-4608.php:5::not_abc().',
11,
],
]);
}

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-4608.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Bug4608;

$c = new class {
public function abc(): void {}
};

$s = rand(0, 1) ? 'abc' : 'not_abc';

$c->{$s}();
Loading