Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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");
}
}
}
}
Loading