Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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
8 changes: 4 additions & 4 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ private function specifyTypesForCountFuncCall(

if (
$sizeType instanceof ConstantIntegerType
&& $sizeType->getValue() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
&& $sizeType->getValue() < ConstantArrayTypeBuilder::getArrayCountLimit()
&& $isList->yes()
&& $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, $sizeType->getValue() - 1))->yes()
) {
Expand All @@ -1168,7 +1168,7 @@ private function specifyTypesForCountFuncCall(
if (
$sizeType instanceof IntegerRangeType
&& $sizeType->getMin() !== null
&& $sizeType->getMin() < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
&& $sizeType->getMin() < ConstantArrayTypeBuilder::getArrayCountLimit()
&& $isList->yes()
&& $arrayType->getKeyType()->isSuperTypeOf(IntegerRangeType::fromInterval(0, ($sizeType->getMax() ?? $sizeType->getMin()) - 1))->yes()
) {
Expand All @@ -1179,7 +1179,7 @@ private function specifyTypesForCountFuncCall(
$builderData[] = [$offsetType, $arrayType->getOffsetValueType($offsetType), false];
}
if ($sizeType->getMax() !== null) {
if ($sizeType->getMax() - $sizeType->getMin() > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
if ($sizeType->getMax() - $sizeType->getMin() > ConstantArrayTypeBuilder::getArrayCountLimit()) {
$resultTypes[] = $arrayType;
continue;
}
Expand All @@ -1201,7 +1201,7 @@ private function specifyTypesForCountFuncCall(
continue;
}

if (count($builderData) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
if (count($builderData) > ConstantArrayTypeBuilder::getArrayCountLimit()) {
$resultTypes[] = $arrayType;
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\ObjectType;
use function array_diff_key;
use function array_intersect;
Expand Down Expand Up @@ -199,6 +200,7 @@ public static function postInitializeContainer(Container $container): void
$container->getService('typeSpecifier');

BleedingEdgeToggle::setBleedingEdge($container->getParameter('featureToggles')['bleedingEdge']);
ConstantArrayTypeBuilder::setArrayCountLimit(256);

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.

Should it be the opposite ?
Letting 512 as default, and manually calling setArrayCountLimit(256) for test which need it ?

Maybe it's because I'm not familiar with this code but setting 256 in postInitializeContainer won't also set it for user of phpstan (and not only for tests) ?

}

public function getCurrentWorkingDirectory(): string
Expand Down
2 changes: 1 addition & 1 deletion src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ function (CallableTypeParameterNode $parameterNode) use ($nameScope, &$isVariadi
private function resolveArrayShapeNode(ArrayShapeNode $typeNode, NameScope $nameScope): Type
{
$builder = ConstantArrayTypeBuilder::createEmpty();
if (count($typeNode->items) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
if (count($typeNode->items) > ConstantArrayTypeBuilder::getArrayCountLimit()) {
$builder->degradeToGeneralArray(true);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ public function resolveConcatType(Type $left, Type $right): Type
*/
public function getArrayType(Expr\Array_ $expr, callable $getTypeCallback): Type
{
if (count($expr->items) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
if (count($expr->items) > ConstantArrayTypeBuilder::getArrayCountLimit()) {
return $this->oversizedArrayBuilder->build($expr, $getTypeCallback);
}

Expand Down Expand Up @@ -1464,7 +1464,7 @@ public function getPlusTypeFromTypes(Expr $left, Expr $right, Type $leftType, Ty
$leftCount = count($leftConstantArrays);
$rightCount = count($rightConstantArrays);
if ($leftCount > 0 && $rightCount > 0
&& ($leftCount + $rightCount < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT)) {
&& ($leftCount + $rightCount < ConstantArrayTypeBuilder::getArrayCountLimit())) {
$resultTypes = [];
foreach ($rightConstantArrays as $rightConstantArray) {
foreach ($leftConstantArrays as $leftConstantArray) {
Expand Down
26 changes: 21 additions & 5 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
final class ConstantArrayTypeBuilder
{

public const ARRAY_COUNT_LIMIT = 256;
/**
* @internal Use getArrayCountLimit() instead
*/
public const ARRAY_COUNT_LIMIT = 512;
private const CLOSURES_COUNT_LIMIT = 16;

private bool $degradeToGeneralArray = false;
Expand All @@ -39,6 +42,8 @@ final class ConstantArrayTypeBuilder

private bool $oversized = false;

private static int $arrayCountLimit = self::ARRAY_COUNT_LIMIT;

/**
* @param list<Type> $keyTypes
* @param array<int, Type> $valueTypes
Expand Down Expand Up @@ -70,13 +75,24 @@ public static function createFromConstantArray(ConstantArrayType $startArrayType
$startArrayType->isList(),
);

if (count($startArrayType->getKeyTypes()) > self::ARRAY_COUNT_LIMIT) {
if (count($startArrayType->getKeyTypes()) > self::getArrayCountLimit()) {
$builder->degradeToGeneralArray(true);
}

return $builder;
}

/** @internal */
public static function setArrayCountLimit(int $limit): void
{
self::$arrayCountLimit = $limit;
}

public static function getArrayCountLimit(): int
{
return self::$arrayCountLimit;
}

public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $optional = false): void
{
if ($offsetType !== null) {
Expand Down Expand Up @@ -147,7 +163,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
$this->optionalKeys[] = count($this->keyTypes) - 1;
}

if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
if (count($this->keyTypes) > self::getArrayCountLimit()) {
$this->degradeToGeneralArray = true;
$this->oversized = true;
}
Expand Down Expand Up @@ -220,7 +236,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
$this->optionalKeys[] = count($this->keyTypes) - 1;
}

if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
if (count($this->keyTypes) > self::getArrayCountLimit()) {
$this->degradeToGeneralArray = true;
$this->oversized = true;
}
Expand All @@ -244,7 +260,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
}
}
}
if (count($scalarTypes) > 0 && count($scalarTypes) < self::ARRAY_COUNT_LIMIT) {
if (count($scalarTypes) > 0 && count($scalarTypes) < self::getArrayCountLimit()) {
$match = true;
$valueTypes = $this->valueTypes;
foreach ($scalarTypes as $scalarType) {
Expand Down
2 changes: 1 addition & 1 deletion src/Type/ConstantTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function getTypeFromValue($value): Type
return new ConstantStringType($value);
} elseif (is_array($value)) {
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
if (count($value) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
if (count($value) > ConstantArrayTypeBuilder::getArrayCountLimit()) {
$arrayBuilder->degradeToGeneralArray(true);
}
foreach ($value as $k => $v) {
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Php/ArrayMapFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
if (count($constantArrays) > 0) {
$arrayTypes = [];
$totalCount = TypeCombinator::countConstantArrayValueTypes($constantArrays) * TypeCombinator::countConstantArrayValueTypes([$valueType]);
if ($totalCount < ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
if ($totalCount < ConstantArrayTypeBuilder::getArrayCountLimit()) {
foreach ($constantArrays as $constantArray) {
$returnedArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
$valueTypes = $constantArray->getValueTypes();
Expand Down
4 changes: 2 additions & 2 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ private static function optimizeConstantArrays(array $types): array
{
$constantArrayValuesCount = self::countConstantArrayValueTypes($types);

if ($constantArrayValuesCount <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
if ($constantArrayValuesCount <= ConstantArrayTypeBuilder::getArrayCountLimit()) {
return $types;
}

Expand Down Expand Up @@ -991,7 +991,7 @@ private static function optimizeConstantArrays(array $types): array
$keyType = self::union(...$keyTypes);
$valueType = self::union(...$valueTypes);

if ($valueType instanceof UnionType && count($valueType->getTypes()) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
if ($valueType instanceof UnionType && count($valueType->getTypes()) > ConstantArrayTypeBuilder::getArrayCountLimit()) {
$valueType = $valueType->generalize(GeneralizePrecision::lessSpecific());
}

Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPUnit\Framework\Attributes\CoversNothing;
Expand Down Expand Up @@ -681,8 +682,11 @@ public function testBug1447(): void
$this->assertNoErrors($errors);
}

/** @runInSeparateProcess */
public function testBug5081(): void
{
ConstantArrayTypeBuilder::setArrayCountLimit(ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT);

$errors = $this->runAnalyse(__DIR__ . '/data/bug-5081.php');
$this->assertNoErrors($errors);
}
Expand Down
24 changes: 23 additions & 1 deletion tests/PHPStan/Analyser/data/bug-5081.php

Large diffs are not rendered by default.

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

namespace PHPStan\Rules\Methods;

use PHPStan\Rules\FunctionReturnTypeCheck;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;

/**
* @extends RuleTestCase<ReturnTypeRule>
*/
class ReturnTypeRuleBug8636Test extends RuleTestCase
{

protected function getRule(): Rule
{
return new ReturnTypeRule(new FunctionReturnTypeCheck(new RuleLevelHelper(self::createReflectionProvider(), true, false, true, false, false, false, true)));
}

/** @runInSeparateProcess */
public function testBug8636(): void
{
ConstantArrayTypeBuilder::setArrayCountLimit(ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT);

$this->analyse([__DIR__ . '/data/bug-8636.php'], []);
}

}
Loading
Loading