Skip to content

Commit

Permalink
more accessories
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 16, 2022
1 parent ed7c153 commit 500ae8a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
19 changes: 11 additions & 8 deletions src/Parser/OversizedConstantArrayVisitor.php
Expand Up @@ -12,6 +12,7 @@
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\GetTypeHelper;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
Expand Down Expand Up @@ -83,6 +84,7 @@ public function leaveNode(Node $node): ?Node
*/
private function inspectArrayItems(Array_ $node): void
{
$isList = true;
$valueType = [];

$itemKeyTypes = [];
Expand All @@ -96,6 +98,7 @@ private function inspectArrayItems(Array_ $node): void
if ($key !== null) {
$keyValue = $this->getValueFromExpr($key);
$itemKeyTypes[gettype($keyValue)] = true; // de-duplicate values
$isList = false;
}

$value = $item->value;
Expand All @@ -106,16 +109,16 @@ private function inspectArrayItems(Array_ $node): void
throw new ShouldNotHappenException();
}

$types = [];
$types[] = new ArrayType($arrayTypes[0], $arrayTypes[1]);
if (count($value->items) > 0) {
$valueType[] = TypeCombinator::intersect(
new ArrayType($arrayTypes[0], $arrayTypes[1]),
new NonEmptyArrayType(),
);

continue;
$types[] = new NonEmptyArrayType();
}
if ($arrayTypes[2] === true) {
$types[] = new AccessoryArrayListType();
}

$valueType[] = new ArrayType($arrayTypes[0], $arrayTypes[1]);
$valueType[] = TypeCombinator::intersect(...$types);
continue;
}

Expand All @@ -141,7 +144,7 @@ private function inspectArrayItems(Array_ $node): void
$valueType[] = $type;
}

$node->setAttribute(self::ARRAY_TYPES, [$keyType, TypeCombinator::union(...$valueType)]);
$node->setAttribute(self::ARRAY_TYPES, [$keyType, TypeCombinator::union(...$valueType), $isList]);
}

/**
Expand Down
13 changes: 8 additions & 5 deletions src/Reflection/InitializerExprTypeResolver.php
Expand Up @@ -446,11 +446,14 @@ public function getArrayType(Expr\Array_ $expr, callable $getTypeCallback): Type
// degrade oversized constant arrays
$oversizedArrayTypes = $expr->getAttribute(OversizedConstantArrayVisitor::ARRAY_TYPES);
if (is_array($oversizedArrayTypes)) {
return TypeCombinator::intersect(
new ArrayType($oversizedArrayTypes[0], $oversizedArrayTypes[1]),
new NonEmptyArrayType(),
new OversizedArrayType()
);
$types = [];
$types[] = new ArrayType($oversizedArrayTypes[0], $oversizedArrayTypes[1]);
$types[] = new NonEmptyArrayType();
$types[] = new OversizedArrayType();
if ($oversizedArrayTypes[2] === true) {
$types[] = new AccessoryArrayListType();
}
return TypeCombinator::intersect(...$types);
}

$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
Expand Down

0 comments on commit 500ae8a

Please sign in to comment.