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
33 changes: 21 additions & 12 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\RecursionGuard;
use PHPStan\Type\Traits\ArrayTypeTrait;
use PHPStan\Type\Traits\NonObjectTypeTrait;
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
Expand Down Expand Up @@ -494,21 +495,29 @@ public function equals(Type $type): bool

public function isCallable(): TrinaryLogic
{
$hasNonExistentMethod = false;
$typeAndMethods = $this->doFindTypeAndMethodNames($hasNonExistentMethod);
if ($typeAndMethods === []) {
return TrinaryLogic::createNo();
}
$result = RecursionGuard::run($this, function (): TrinaryLogic {
$hasNonExistentMethod = false;
$typeAndMethods = $this->doFindTypeAndMethodNames($hasNonExistentMethod);
if ($typeAndMethods === []) {
return TrinaryLogic::createNo();
}

$results = array_map(
static fn (ConstantArrayTypeAndMethod $typeAndMethod): TrinaryLogic => $typeAndMethod->getCertainty(),
$typeAndMethods,
);
$results = array_map(
static fn (ConstantArrayTypeAndMethod $typeAndMethod): TrinaryLogic => $typeAndMethod->getCertainty(),
$typeAndMethods,
);

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

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

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

if ($result instanceof ErrorType) {
return TrinaryLogic::createNo();
}

return $result;
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ public function testInfiniteRecursionWithCallable(): void
$this->assertNoErrors($errors);
}

#[RequiresPhp('>= 8.0')]
public function testConstantArrayCallableDoesNotCauseInfiniteRecursion(): void
{
// Previously caused infinite recursion / OOM via ConstantArrayType::isCallable()
// resolving getMethod() which triggered return type resolution that called isCallable() again
$errors = $this->runAnalyse(__DIR__ . '/data/bug-constant-array-callable-recursion.php');
$this->assertCount(3, $errors);
}

public function testClassThatExtendsUnknownClassIn3rdPartyPropertyTypeShouldNotCauseAutoloading(): void
{
// no error about PHPStan\Tests\Baz not being able to be autoloaded
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace BugConstantArrayCallableRecursion;

class Period
{

/**
* @return array{'BugConstantArrayCallableRecursion\Period', 'endIteration'}
*/
public function endIteration(): callable
{
return [self::class, 'endIteration'];
}

}

function test(): void
{
$cb = ['BugConstantArrayCallableRecursion\Period', 'endIteration'];
is_callable($cb);
}
Loading