Skip to content

Commit

Permalink
Fix dumbed down arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 21, 2022
1 parent db2de6f commit e18f27b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Analyser/MutatingScope.php
Expand Up @@ -68,6 +68,7 @@
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Accessory\OversizedArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ClosureType;
Expand Down Expand Up @@ -4357,6 +4358,9 @@ private static function generalizeType(Type $a, Type $b): Type
if ($generalArraysA->isList()->yes() && $generalArraysB->isList()->yes()) {
$resultType = AccessoryArrayListType::intersectWith($resultType);
}
if ($generalArraysA->isOversizedArray()->yes() && $generalArraysB->isOversizedArray()->yes()) {
$resultType = TypeCombinator::intersect($resultType, new OversizedArrayType());
}
$resultTypes[] = $resultType;
}
} elseif (count($generalArrays['b']) > 0) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/TypeCombinator.php
Expand Up @@ -21,6 +21,7 @@
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateUnionType;
use function array_key_exists;
use function array_key_first;
use function array_map;
use function array_merge;
use function array_slice;
Expand Down Expand Up @@ -548,6 +549,10 @@ private static function processArrayAccessoryTypes(array $arrayTypes): array
$arrayTypeCount = count($arrayTypes);
foreach ($accessoryTypes as $accessoryType) {
if (count($accessoryType) !== $arrayTypeCount) {
$firstKey = array_key_first($accessoryType);
if ($accessoryType[$firstKey] instanceof OversizedArrayType) {
$commonAccessoryTypes[] = $accessoryType[$firstKey];
}
continue;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Expand Up @@ -1098,6 +1098,12 @@ public static function getAdditionalConfigFiles(): array
];
}

public function testBug8004(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-8004.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
1 change: 0 additions & 1 deletion tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -1069,7 +1069,6 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7987.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7963-three.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8017.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8004.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/global-namespace.php');

if (PHP_VERSION_ID >= 80000) {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/bug-8004.php
Expand Up @@ -73,7 +73,7 @@ public function getErrorsOnInvalidQuestions(array $importQuiz, int $key): array
}
}

assertType("list<non-empty-array<literal-string&non-falsy-string, 'empty_answer'|'empty_question'|'invalid_answer_1_too_long'|'invalid_answer_1_type'|'invalid_answer_2_too_long'|'invalid_answer_2_type'|'invalid_answer_3_too_long'|'invalid_answer_3_type'|'invalid_answer_4_too_long'|'invalid_answer_4_type'|'invalid_question_too_long'|'invalid_right_answer'|int>>", $errors);
assertType("list<non-empty-array<literal-string&non-falsy-string, 'empty_answer'|'empty_question'|'invalid_answer_1_too_long'|'invalid_answer_1_type'|'invalid_answer_2_too_long'|'invalid_answer_2_type'|'invalid_answer_3_too_long'|'invalid_answer_3_type'|'invalid_answer_4_too_long'|'invalid_answer_4_type'|'invalid_question_too_long'|'invalid_right_answer'|int>&oversized-array>&oversized-array", $errors);

return $errors;
}
Expand Down

0 comments on commit e18f27b

Please sign in to comment.