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
4 changes: 1 addition & 3 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,9 +1049,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) {
$builder->degradeToGeneralArray(true);
}
$builder->disableArrayDegradation();

foreach ($typeNode->items as $itemNode) {
if ($itemNode->valueType instanceof CallableTypeNode) {
Expand Down
29 changes: 26 additions & 3 deletions src/Type/Constant/ConstantArrayTypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ final class ConstantArrayTypeBuilder

private bool $degradeToGeneralArray = false;

private bool $disableArrayDegradation = false;

private ?bool $degradeClosures = null;

private bool $oversized = false;
Expand Down Expand Up @@ -84,7 +86,11 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
}

if (!$this->degradeToGeneralArray) {
if ($valueType instanceof ClosureType && $this->degradeClosures !== false) {
if (
$valueType instanceof ClosureType
&& $this->degradeClosures !== false
&& !$this->disableArrayDegradation
) {
$numClosures = 1;
foreach ($this->valueTypes as $innerType) {
if (!($innerType instanceof ClosureType)) {
Expand Down Expand Up @@ -147,7 +153,10 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
$this->optionalKeys[] = count($this->keyTypes) - 1;
}

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

if (count($this->keyTypes) > self::ARRAY_COUNT_LIMIT) {
if (
!$this->disableArrayDegradation
&& count($this->keyTypes) > self::ARRAY_COUNT_LIMIT
) {
$this->degradeToGeneralArray = true;
$this->oversized = true;
}
Expand Down Expand Up @@ -296,6 +308,10 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt

public function degradeToGeneralArray(bool $oversized = false): void
{
if ($this->disableArrayDegradation) {
throw new ShouldNotHappenException();
}

$this->degradeToGeneralArray = true;
$this->oversized = $this->oversized || $oversized;
}
Expand All @@ -305,6 +321,13 @@ public function disableClosureDegradation(): void
$this->degradeClosures = false;
}

public function disableArrayDegradation(): void
{
$this->degradeToGeneralArray = false;
$this->oversized = false;
$this->disableArrayDegradation = true;
}

public function getArray(): Type
{
$keyTypesCount = count($this->keyTypes);
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,12 @@ public function testBug13945Two(): void
$this->assertNoErrors($errors);
}

public function testBigPhpdocArrayShape(): void
{
$errors = $this->runAnalyse(__DIR__ . '/nsrt/bug-14012b.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return list<Error>
Expand Down
293 changes: 293 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14012b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
<?php declare(strict_types=1);

namespace Bug14012b;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use function PHPStan\Testing\assertType;

final class ExpectationMethodResolver
{
/**
* @var array{
* hasMethod: bool,
* hasProperty: bool,
* isArray: bool,
* isBool: bool,
* isCallable: bool,
* isCountable: bool,
* isFalse: bool,
* isFloat: bool,
* isInstanceOf: bool,
* isInt: bool,
* isIterable: bool,
* isList: bool,
* isMap: bool,
* isNaturalInt: bool,
* isNegativeInt: bool,
* isNonEmptyString: bool,
* isNull: bool,
* isNumeric: bool,
* isObject: bool,
* isPositiveInt: bool,
* isResource: bool,
* isSameAs: bool,
* isScalar: bool,
* isString: bool,
* isTrue: bool,
* jhasMethod: bool,
* jhasProperty: bool,
* jisArray: bool,
* jisBool: bool,
* jisCallable: bool,
* jisCountable: bool,
* jisFalse: bool,
* jisFloat: bool,
* jisInstanceOf: bool,
* jisInt: bool,
* jisIterable: bool,
* jisList: bool,
* jisMap: bool,
* jisNaturalInt: bool,
* jisNegativeInt: bool,
* jisNonEmptyString: bool,
* jisNull: bool,
* jisNumeric: bool,
* jisObject: bool,
* jisPositiveInt: bool,
* jisResource: bool,
* jisSameAs: bool,
* jisScalar: bool,
* jisString: bool,
* jisTrue: bool,
* ihasMethod: bool,
* ihasProperty: bool,
* iisArray: bool,
* iisBool: bool,
* iisCallable: bool,
* iisCountable: bool,
* iisFalse: bool,
* iisFloat: bool,
* iisInstanceOf: bool,
* iisInt: bool,
* iisIterable: bool,
* iisList: bool,
* iisMap: bool,
* iisNaturalInt: bool,
* iisNegativeInt: bool,
* iisNonEmptyString: bool,
* iisNull: bool,
* iisNumeric: bool,
* iisObject: bool,
* iisPositiveInt: bool,
* iisResource: bool,
* iisSameAs: bool,
* iisScalar: bool,
* iisString: bool,
* iisTrue: bool,
* hhasMethod: bool,
* hhasProperty: bool,
* hisArray: bool,
* hisBool: bool,
* hisCallable: bool,
* hisCountable: bool,
* hisFalse: bool,
* hisFloat: bool,
* hisInstanceOf: bool,
* hisInt: bool,
* hisIterable: bool,
* hisList: bool,
* hisMap: bool,
* hisNaturalInt: bool,
* hisNegativeInt: bool,
* hisNonEmptyString: bool,
* hisNull: bool,
* hisNumeric: bool,
* hisObject: bool,
* hisPositiveInt: bool,
* hisResource: bool,
* hisSameAs: bool,
* hisScalar: bool,
* hisString: bool,
* hisTrue: bool,
* ghasMethod: bool,
* ghasProperty: bool,
* gisArray: bool,
* gisBool: bool,
* gisCallable: bool,
* gisCountable: bool,
* gisFalse: bool,
* gisFloat: bool,
* gisInstanceOf: bool,
* gisInt: bool,
* gisIterable: bool,
* gisList: bool,
* gisMap: bool,
* gisNaturalInt: bool,
* gisNegativeInt: bool,
* gisNonEmptyString: bool,
* gisNull: bool,
* gisNumeric: bool,
* gisObject: bool,
* gisPositiveInt: bool,
* gisResource: bool,
* gisSameAs: bool,
* gisScalar: bool,
* gisString: bool,
* gisTrue: bool,
* fhasMethod: bool,
* fhasProperty: bool,
* fisArray: bool,
* fisBool: bool,
* fisCallable: bool,
* fisCountable: bool,
* fisFalse: bool,
* fisFloat: bool,
* fisInstanceOf: bool,
* fisInt: bool,
* fisIterable: bool,
* fisList: bool,
* fisMap: bool,
* fisNaturalInt: bool,
* fisNegativeInt: bool,
* fisNonEmptyString: bool,
* fisNull: bool,
* fisNumeric: bool,
* fisObject: bool,
* fisPositiveInt: bool,
* fisResource: bool,
* fisSameAs: bool,
* fisScalar: bool,
* fisString: bool,
* fisTrue: bool,
* ehasMethod: bool,
* ehasProperty: bool,
* eisArray: bool,
* eisBool: bool,
* eisCallable: bool,
* eisCountable: bool,
* eisFalse: bool,
* eisFloat: bool,
* eisInstanceOf: bool,
* eisInt: bool,
* eisIterable: bool,
* eisList: bool,
* eisMap: bool,
* eisNaturalInt: bool,
* eisNegativeInt: bool,
* eisNonEmptyString: bool,
* eisNull: bool,
* eisNumeric: bool,
* eisObject: bool,
* eisPositiveInt: bool,
* eisResource: bool,
* eisSameAs: bool,
* eisScalar: bool,
* eisString: bool,
* eisTrue: bool,
* dhasMethod: bool,
* dhasProperty: bool,
* disArray: bool,
* disBool: bool,
* disCallable: bool,
* disCountable: bool,
* disFalse: bool,
* disFloat: bool,
* disInstanceOf: bool,
* disInt: bool,
* disIterable: bool,
* disList: bool,
* disMap: bool,
* disNaturalInt: bool,
* disNegativeInt: bool,
* disNonEmptyString: bool,
* disNull: bool,
* disNumeric: bool,
* disObject: bool,
* disPositiveInt: bool,
* disResource: bool,
* disSameAs: bool,
* disScalar: bool,
* disString: bool,
* disTrue: bool,
* chasMethod: bool,
* chasProperty: bool,
* cisArray: bool,
* cisBool: bool,
* cisCallable: bool,
* cisCountable: bool,
* cisFalse: bool,
* cisFloat: bool,
* cisInstanceOf: bool,
* cisInt: bool,
* cisIterable: bool,
* cisList: bool,
* cisMap: bool,
* cisNaturalInt: bool,
* cisNegativeInt: bool,
* cisNonEmptyString: bool,
* cisNull: bool,
* cisNumeric: bool,
* cisObject: bool,
* cisPositiveInt: bool,
* cisResource: bool,
* cisSameAs: bool,
* cisScalar: bool,
* cisString: bool,
* cisTrue: bool,
* bhasMethod: bool,
* bhasProperty: bool,
* bisArray: bool,
* bisBool: bool,
* bisCallable: bool,
* bisCountable: bool,
* bisFalse: bool,
* bisFloat: bool,
* bisInstanceOf: bool,
* bisInt: bool,
* bisIterable: bool,
* bisList: bool,
* bisMap: bool,
* bisNaturalInt: bool,
* bisNegativeInt: bool,
* bisNonEmptyString: bool,
* bisNull: bool,
* bisNumeric: bool,
* bisObject: bool,
* bisPositiveInt: bool,
* bisResource: bool,
* bisSameAs: bool,
* bisScalar: bool,
* bisString: bool,
* bisTrue: bool,
* ahasMethod: bool,
* ahasProperty: bool,
* aisArray: bool,
* aisBool: bool,
* aisCallable: bool,
* aisCountable: bool,
* aisFalse: bool,
* aisFloat: bool,
* aisInstanceOf: bool,
* aisInt: bool,
* aisIterable: bool,
* aisList: bool,
* aisMap: bool,
* aisNaturalInt: bool,
* aisNegativeInt: bool,
* aisNonEmptyString: bool,
* aisNull: bool,
* aisNumeric: bool,
* aisObject: bool,
* aisPositiveInt: bool,
* aisResource: bool,
* aisSameAs: bool,
* aisScalar: bool,
* aisString: bool,
* aisTrue: bool,
* }
*/
public static array $resolvers;
}

assertType("array{hasMethod: bool, hasProperty: bool, isArray: bool, isBool: bool, isCallable: bool, isCountable: bool, isFalse: bool, isFloat: bool, isInstanceOf: bool, isInt: bool, isIterable: bool, isList: bool, isMap: bool, isNaturalInt: bool, isNegativeInt: bool, isNonEmptyString: bool, isNull: bool, isNumeric: bool, isObject: bool, isPositiveInt: bool, isResource: bool, isSameAs: bool, isScalar: bool, isString: bool, isTrue: bool, jhasMethod: bool, jhasProperty: bool, jisArray: bool, jisBool: bool, jisCallable: bool, jisCountable: bool, jisFalse: bool, jisFloat: bool, jisInstanceOf: bool, jisInt: bool, jisIterable: bool, jisList: bool, jisMap: bool, jisNaturalInt: bool, jisNegativeInt: bool, jisNonEmptyString: bool, jisNull: bool, jisNumeric: bool, jisObject: bool, jisPositiveInt: bool, jisResource: bool, jisSameAs: bool, jisScalar: bool, jisString: bool, jisTrue: bool, ihasMethod: bool, ihasProperty: bool, iisArray: bool, iisBool: bool, iisCallable: bool, iisCountable: bool, iisFalse: bool, iisFloat: bool, iisInstanceOf: bool, iisInt: bool, iisIterable: bool, iisList: bool, iisMap: bool, iisNaturalInt: bool, iisNegativeInt: bool, iisNonEmptyString: bool, iisNull: bool, iisNumeric: bool, iisObject: bool, iisPositiveInt: bool, iisResource: bool, iisSameAs: bool, iisScalar: bool, iisString: bool, iisTrue: bool, hhasMethod: bool, hhasProperty: bool, hisArray: bool, hisBool: bool, hisCallable: bool, hisCountable: bool, hisFalse: bool, hisFloat: bool, hisInstanceOf: bool, hisInt: bool, hisIterable: bool, hisList: bool, hisMap: bool, hisNaturalInt: bool, hisNegativeInt: bool, hisNonEmptyString: bool, hisNull: bool, hisNumeric: bool, hisObject: bool, hisPositiveInt: bool, hisResource: bool, hisSameAs: bool, hisScalar: bool, hisString: bool, hisTrue: bool, ghasMethod: bool, ghasProperty: bool, gisArray: bool, gisBool: bool, gisCallable: bool, gisCountable: bool, gisFalse: bool, gisFloat: bool, gisInstanceOf: bool, gisInt: bool, gisIterable: bool, gisList: bool, gisMap: bool, gisNaturalInt: bool, gisNegativeInt: bool, gisNonEmptyString: bool, gisNull: bool, gisNumeric: bool, gisObject: bool, gisPositiveInt: bool, gisResource: bool, gisSameAs: bool, gisScalar: bool, gisString: bool, gisTrue: bool, fhasMethod: bool, fhasProperty: bool, fisArray: bool, fisBool: bool, fisCallable: bool, fisCountable: bool, fisFalse: bool, fisFloat: bool, fisInstanceOf: bool, fisInt: bool, fisIterable: bool, fisList: bool, fisMap: bool, fisNaturalInt: bool, fisNegativeInt: bool, fisNonEmptyString: bool, fisNull: bool, fisNumeric: bool, fisObject: bool, fisPositiveInt: bool, fisResource: bool, fisSameAs: bool, fisScalar: bool, fisString: bool, fisTrue: bool, ehasMethod: bool, ehasProperty: bool, eisArray: bool, eisBool: bool, eisCallable: bool, eisCountable: bool, eisFalse: bool, eisFloat: bool, eisInstanceOf: bool, eisInt: bool, eisIterable: bool, eisList: bool, eisMap: bool, eisNaturalInt: bool, eisNegativeInt: bool, eisNonEmptyString: bool, eisNull: bool, eisNumeric: bool, eisObject: bool, eisPositiveInt: bool, eisResource: bool, eisSameAs: bool, eisScalar: bool, eisString: bool, eisTrue: bool, dhasMethod: bool, dhasProperty: bool, disArray: bool, disBool: bool, disCallable: bool, disCountable: bool, disFalse: bool, disFloat: bool, disInstanceOf: bool, disInt: bool, disIterable: bool, disList: bool, disMap: bool, disNaturalInt: bool, disNegativeInt: bool, disNonEmptyString: bool, disNull: bool, disNumeric: bool, disObject: bool, disPositiveInt: bool, disResource: bool, disSameAs: bool, disScalar: bool, disString: bool, disTrue: bool, chasMethod: bool, chasProperty: bool, cisArray: bool, cisBool: bool, cisCallable: bool, cisCountable: bool, cisFalse: bool, cisFloat: bool, cisInstanceOf: bool, cisInt: bool, cisIterable: bool, cisList: bool, cisMap: bool, cisNaturalInt: bool, cisNegativeInt: bool, cisNonEmptyString: bool, cisNull: bool, cisNumeric: bool, cisObject: bool, cisPositiveInt: bool, cisResource: bool, cisSameAs: bool, cisScalar: bool, cisString: bool, cisTrue: bool, bhasMethod: bool, bhasProperty: bool, bisArray: bool, bisBool: bool, bisCallable: bool, bisCountable: bool, bisFalse: bool, bisFloat: bool, bisInstanceOf: bool, bisInt: bool, bisIterable: bool, bisList: bool, bisMap: bool, bisNaturalInt: bool, bisNegativeInt: bool, bisNonEmptyString: bool, bisNull: bool, bisNumeric: bool, bisObject: bool, bisPositiveInt: bool, bisResource: bool, bisSameAs: bool, bisScalar: bool, bisString: bool, bisTrue: bool, ahasMethod: bool, ahasProperty: bool, aisArray: bool, aisBool: bool, aisCallable: bool, aisCountable: bool, aisFalse: bool, aisFloat: bool, aisInstanceOf: bool, aisInt: bool, aisIterable: bool, aisList: bool, aisMap: bool, aisNaturalInt: bool, aisNegativeInt: bool, aisNonEmptyString: bool, aisNull: bool, aisNumeric: bool, aisObject: bool, aisPositiveInt: bool, aisResource: bool, aisSameAs: bool, aisScalar: bool, aisString: bool, aisTrue: bool}", ExpectationMethodResolver::$resolvers);
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-8775.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class Test
public static $fixtures;

function doFoo():void {
assertType('(int|string)', Test::$fixtures['257']);
assertType('int', Test::$fixtures['257']);
}
}

Loading
Loading