Skip to content
Closed
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
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ parameters:
reportMagicProperties: false
ignoreErrors: []
internalErrorsCountLimit: 50
constantArrayTypeBuilderArrayCountLimit: 256
cache:
nodesByStringCountMax: 256
resolvedPhpDocBlockCacheCountMax: 2048
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ parametersSchema:
)
)
internalErrorsCountLimit: int()
constantArrayTypeBuilderArrayCountLimit: int()
cache: structure([
nodesByStringCountMax: int(),
resolvedPhpDocBlockCacheCountMax: int(),
Expand Down
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
20 changes: 20 additions & 0 deletions src/DependencyInjection/ConstantArrayTypeLimitAccessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

final class ConstantArrayTypeLimitAccessor
{

private static int $limit = 256;

public static function getLimit(): int
{
return self::$limit;
}

public static function setLimit(int $limit): void
{
self::$limit = $limit;
}

}
1 change: 1 addition & 0 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public static function postInitializeContainer(Container $container): void
$container->getService('typeSpecifier');

BleedingEdgeToggle::setBleedingEdge($container->getParameter('featureToggles')['bleedingEdge']);
ConstantArrayTypeLimitAccessor::setLimit($container->getParameter('constantArrayTypeBuilderArrayCountLimit'));
}

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
17 changes: 13 additions & 4 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Constant;

use PHPStan\DependencyInjection\ConstantArrayTypeLimitAccessor;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
Expand Down Expand Up @@ -30,9 +31,17 @@
final class ConstantArrayTypeBuilder
{

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

public static function getArrayCountLimit(): int
{
return ConstantArrayTypeLimitAccessor::getLimit();
}

private bool $degradeToGeneralArray = false;

private bool $degradeClosures = false;
Expand Down Expand Up @@ -70,7 +79,7 @@ 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);
}

Expand Down Expand Up @@ -147,7 +156,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 +229,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 +253,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
Loading