Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 17 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,13 @@ public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope)

/** @return ConstantArrayTypeAndMethod[] */
public function findTypeAndMethodNames(): array
{
$hasNonExistentMethod = false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to instantiate this variable since we don't use it ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Made the $hasNonExistentMethod parameter optional with a default value of false in doFindTypeAndMethodNames(), so findTypeAndMethodNames() no longer needs to instantiate a variable it never uses. All tests and static analysis pass.

return $this->doFindTypeAndMethodNames($hasNonExistentMethod);
}

/** @return ConstantArrayTypeAndMethod[] */
private function doFindTypeAndMethodNames(bool &$hasNonExistentMethod): array
{
if (count($this->keyTypes) !== 2) {
return [];
Expand Down Expand Up @@ -578,6 +592,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