Skip to content
Closed
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
38 changes: 31 additions & 7 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,17 +458,41 @@ public function equals(Type $type): bool

public function isCallable(): TrinaryLogic
{
$typeAndMethods = $this->findTypeAndMethodNames();
if ($typeAndMethods === []) {
$callableArray = $this->getClassOrObjectAndMethods();
if ($callableArray === []) {
return TrinaryLogic::createNo();
}

$results = array_map(
static fn (ConstantArrayTypeAndMethod $typeAndMethod): TrinaryLogic => $typeAndMethod->getCertainty(),
$typeAndMethods,
);
[$classOrObject, $methods] = $callableArray;
if (count($methods->getConstantStrings()) === 0) {
return TrinaryLogic::createMaybe();
}

$type = $classOrObject->getObjectTypeOrClassStringObjectType();
if (!$type->isObject()->yes()) {
return TrinaryLogic::createMaybe();
}

$hasMethodTrinary = [];
$phpVersion = PhpVersionStaticAccessor::getInstance();
foreach ($methods->getConstantStrings() as $method) {
$has = $type->hasMethod($method->getValue());

if ($has->yes()) {
if (!$phpVersion->supportsCallableInstanceMethods()) {
$methodReflection = $type->getMethod($method->getValue(), new OutOfClassScope());
if ($classOrObject->isString()->yes() && !$methodReflection->isStatic()) {
$has = TrinaryLogic::createNo();
}
} elseif ($this->isOptionalKey(0) || $this->isOptionalKey(1)) {
$has = $has->and(TrinaryLogic::createMaybe());
}
}

$hasMethodTrinary[] = $has;
}

return TrinaryLogic::createYes()->and(...$results);
return TrinaryLogic::extremeIdentity(...$hasMethodTrinary);
}

public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,4 +1102,11 @@ public function testAlwaysTruePregMatch(): void
$this->analyse([__DIR__ . '/data/always-true-preg-match.php'], []);
}

public function testBug12063(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$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); // lint >= 7.4

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