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
40 changes: 40 additions & 0 deletions src/Analyser/StmtHandler/ForeachHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\List_;
Expand All @@ -17,6 +19,7 @@
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\NodeFinder;
use PHPStan\Analyser\ConditionalExpressionHolder;
use PHPStan\Analyser\ConvergedLoopHeads;
use PHPStan\Analyser\ExpressionContext;
Expand Down Expand Up @@ -776,6 +779,38 @@ private function addIterateeMemberConditionalHolders(MutatingScope $scope, Expr
return $scope;
}

/**
* Whether the body writes an offset whose key mentions the loop's key or
* value variable, as `$a[$value] = ...` or `$a[$x][$key][] = ...`.
*/
private static function writesOffsetWithLoopVariable(Foreach_ $stmt): bool
{
$names = [];
foreach ([$stmt->keyVar, $stmt->valueVar] as $var) {
if (!$var instanceof Variable || !is_string($var->name)) {
continue;
}
$names[$var->name] = true;
}

return (new NodeFinder())->findFirst($stmt->stmts, static function (Node $node) use ($names): bool {
if (!$node instanceof Assign && !$node instanceof AssignRef && !$node instanceof AssignOp) {
return false;
}
for ($target = $node->var; $target instanceof ArrayDimFetch; $target = $target->var) {
if ($target->dim === null) {
continue;
}
$mentions = (new NodeFinder())->findFirst([$target->dim], static fn (Node $dimNode): bool => $dimNode instanceof Variable && is_string($dimNode->name) && isset($names[$dimNode->name]));
if ($mentions !== null) {
return true;
}
}

return false;
}) !== null;
}

/**
* @return array{bodyScope: MutatingScope, endScope: MutatingScope|null, totalKeys: int}|null
*/
Expand Down Expand Up @@ -819,6 +854,11 @@ private function tryProcessUnrolledConstantArrayForeach(
if ($totalKeys === 0 || $totalKeys > self::FOREACH_UNROLL_LIMIT) {
return null;
}
// in an enclosing loop's passes, unrolling is worth its cost only to keep
// the offsets the body writes with the key or the value required
if (!$context->isTopLevel() && !self::writesOffsetWithLoopVariable($stmt)) {
return null;
}
$foreachUnrollFactor = $context->getForeachUnrollFactor();
if ($foreachUnrollFactor > 1 && $foreachUnrollFactor * $totalKeys > self::FOREACH_UNROLL_NESTED_LIMIT) {
return null;
Expand Down
35 changes: 32 additions & 3 deletions src/Type/Combinator/ArrayWidening.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use function count;

/**
* Widening of array shapes, what TypeCombinator::widen() does to the arrays of
* a union and to the values merged into a general array: while the types hold
* more than ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT constant array values,
* the next step runs - the general arrays merge first, then the shapes fold,
* more than ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT constant array values
* or more than UNION_SHAPES_LIMIT shapes, the next step runs - the general arrays merge first, then the shapes fold,
* keeping their keys, and then degrade to oversized general arrays. The steps
* see the members of the unions of arrays among the types.
*
Expand All @@ -23,6 +24,14 @@
final class ArrayWidening
{

/**
* The most shapes a union keeps apart. Exact unions keep shapes that do not
* merge into one, so a list appended to in N branches would join N shapes of
* up to N keys - quadratic in the branches, while each stays far below
* ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT values.
*/
public const UNION_SHAPES_LIMIT = 16;

/** @var list<WideningStep>|null */
private static ?array $steps = null;

Expand Down Expand Up @@ -69,7 +78,27 @@ public static function widen(array $types): array
/** @param list<Type> $types */
public static function isWithinLimit(array $types): bool
{
return TypeCombinator::countConstantArrayValueTypes($types) <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT;
return self::countShapes($types) <= self::UNION_SHAPES_LIMIT
&& TypeCombinator::countConstantArrayValueTypes($types) <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT;
}

/**
* A list with optional keys counts once per length it can have: appending
* to it gives a shape of each length, so folding the lists of a union into
* one with optional keys does not make it smaller to compute with.
*
* @param list<Type> $types
*/
private static function countShapes(array $types): int
{
$count = 0;
foreach ($types as $type) {
foreach ($type->getConstantArrays() as $shape) {
$count += $shape->isList()->yes() ? count($shape->getOptionalKeys()) + 1 : 1;
}
}

return $count;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Turbo\TurboExtensionEnabler;
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\Combinator\AbsorbedArrayUnionRule;
use PHPStan\Type\Combinator\ArrayWidening;
use PHPStan\Type\Combinator\AtOnceUnionRemoveRule;
use PHPStan\Type\Combinator\BooleanUnionRule;
use PHPStan\Type\Combinator\DisjointFiniteUnionRemoveRule;
Expand Down Expand Up @@ -37,7 +38,6 @@
use PHPStan\Type\Combinator\UnionRemoveRule;
use PHPStan\Type\Combinator\UnionRule;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Generic\TemplateBenevolentUnionType;
use PHPStan\Type\Generic\TemplateMixedType;
use PHPStan\Type\Generic\TemplateType;
Expand Down Expand Up @@ -204,7 +204,8 @@ public static function containsNull(Type $type): bool

/**
* The type, generalized where it holds more constant array values than
* ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT: shapes with the same keys
* ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT or more shapes than
* ArrayWidening::UNION_SHAPES_LIMIT: shapes with the same keys
* merged, shapes sharing a required key folded into one, and what is still
* too big degraded to an oversized array.
*
Expand Down Expand Up @@ -235,7 +236,7 @@ public static function widen(Type $type): Type

$bleedingEdge = BleedingEdgeToggle::isBleedingEdge();
if (
self::countConstantArrayValueTypes($arrayTypes) <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
ArrayWidening::isWithinLimit($arrayTypes)
&& (!$bleedingEdge || count(GeneralArrayUnionRule::getGeneralArrays($arrayTypes)) <= GeneralArrayUnionRule::GENERAL_ARRAYS_LIMIT)
) {
return $type;
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-13509.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function alert(): ?array
return null;
}

assertType("list{0: array{message: 'Foo', details: 'bar', duration: int<1, max>|null, severity: 100}|array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'Offline', duration: int<1, max>|null, severity: 99}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 1?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'Offline', duration: int<1, max>|null, severity: 99}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 2?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 3?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 4?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 5?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 6?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'On Break', duration: int<1, max>|null, severity: 24}, 7?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}}", $alerts);
assertType("array{array{message: 'Idle', duration: int<1, max>|null, severity: 23}}|list{0: array{message: 'Foo', details: 'bar', duration: int<1, max>|null, severity: 100}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'Offline', duration: int<1, max>|null, severity: 99}|array{message: 'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 1?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'Offline', duration: int<1, max>|null, severity: 99}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 2?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}|array{message: 'Running W/O Operator', duration: int<1, max>|null, severity: 75}, 3?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'No Queue', duration: int<1, max>|null, severity: 60}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 4?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'Not Scheduled', duration: null, severity: 25}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 5?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'On Break'|'On Lunch', duration: int<1, max>|null, severity: 24}, 6?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}|array{message: 'On Break', duration: int<1, max>|null, severity: 24}, 7?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}}|list{0: array{message: 'On Break', duration: int<1, max>|null, severity: 24}, 1?: array{message: 'Idle', duration: int<1, max>|null, severity: 23}}", $alerts);

usort($alerts, fn ($a, $b) => $b['severity'] <=> $a['severity']);

Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-15293.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ class Foo

public function doFoo(string $key): void
{
assertType('array{}|(non-empty-array<literal-string&lowercase-string&non-falsy-string, list{0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89}&oversized-array>&oversized-array)', self::MAP[$key]);
assertType('non-empty-array<literal-string&lowercase-string&non-falsy-string, list{0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89}&oversized-array>&oversized-array', self::MAP_WITHOUT_EMPTY[$key]);
assertType('array{}|(non-empty-array<literal-string&lowercase-string&non-falsy-string, (list{0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29}&oversized-array)|(list{30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59, 30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59, 30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59}&oversized-array)|(list{60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89}&oversized-array)>&oversized-array)', self::MAP[$key]);
assertType('non-empty-array<literal-string&lowercase-string&non-falsy-string, (list{0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29, 0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29}&oversized-array)|(list{30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59, 30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59, 30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59}&oversized-array)|(list{60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89, 60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89}&oversized-array)>&oversized-array', self::MAP_WITHOUT_EMPTY[$key]);
}

}
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/bug-6936-limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ public function testLimits():void
$arr[] = 'g';
}

assertType("array{'e', 2|'f'|'g', 3|'g'}|array{'f', 2|'g', 3}|array{1|'g', 2, 3}|list{0: 'a', 1: 2|'b'|'c'|'d'|'e'|'f'|'g', 2: 3|'c'|'d'|'e'|'f'|'g', 3?: 'd'|'e'|'f'|'g', 4?: 'e'|'f'|'g', 5?: 'f'|'g', 6?: 'g'}|list{0: 'b', 1: 2|'c'|'d'|'e'|'f'|'g', 2: 3|'d'|'e'|'f'|'g', 3?: 'e'|'f'|'g', 4?: 'f'|'g', 5?: 'g'}|list{0: 'c', 1: 2|'d'|'e'|'f'|'g', 2: 3|'e'|'f'|'g', 3?: 'f'|'g', 4?: 'g'}|list{0: 'd', 1: 2|'e'|'f'|'g', 2: 3|'f'|'g', 3?: 'g'}", $arr + $arr2);
assertType("array{1|'g', 2, 3}|list{0: 'a'|'b'|'c'|'d'|'e'|'f', 1: 2|'b'|'c'|'d'|'e'|'f'|'g', 2: 3|'c'|'d'|'e'|'f'|'g', 3?: 'd'|'e'|'f'|'g', 4?: 'e'|'f'|'g', 5?: 'f'|'g', 6?: 'g'}", $arr + $arr2);
if (rand(0,1)) {
$arr[] = 'h';
}

assertType("array{'f', 2|'g'|'h', 3|'h'}|array{'g', 2|'h', 3}|array{1|'h', 2, 3}|list{0: 'a', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h', 2: 3|'c'|'d'|'e'|'f'|'g'|'h', 3?: 'd'|'e'|'f'|'g'|'h', 4?: 'e'|'f'|'g'|'h', 5?: 'f'|'g'|'h', 6?: 'g'|'h', 7?: 'h'}|list{0: 'b', 1: 2|'c'|'d'|'e'|'f'|'g'|'h', 2: 3|'d'|'e'|'f'|'g'|'h', 3?: 'e'|'f'|'g'|'h', 4?: 'f'|'g'|'h', 5?: 'g'|'h', 6?: 'h'}|list{0: 'c', 1: 2|'d'|'e'|'f'|'g'|'h', 2: 3|'e'|'f'|'g'|'h', 3?: 'f'|'g'|'h', 4?: 'g'|'h', 5?: 'h'}|list{0: 'd', 1: 2|'e'|'f'|'g'|'h', 2: 3|'f'|'g'|'h', 3?: 'g'|'h', 4?: 'h'}|list{0: 'e', 1: 2|'f'|'g'|'h', 2: 3|'g'|'h', 3?: 'h'}", $arr + $arr2);
assertType("array{'g', 2|'h', 3}|array{1|'h', 2, 3}|list{0: 'a'|'b'|'c'|'d'|'e'|'f', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h', 2: 3|'c'|'d'|'e'|'f'|'g'|'h', 3?: 'd'|'e'|'f'|'g'|'h', 4?: 'e'|'f'|'g'|'h', 5?: 'f'|'g'|'h', 6?: 'g'|'h', 7?: 'h'}", $arr + $arr2);
if (rand(0,1)) {
$arr[] = 'i';
}

assertType("array{'g', 2|'h'|'i', 3|'i'}|array{'h', 2|'i', 3}|array{1|'i', 2, 3}|list{0: 'a', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'c'|'d'|'e'|'f'|'g'|'h'|'i', 3?: 'd'|'e'|'f'|'g'|'h'|'i', 4?: 'e'|'f'|'g'|'h'|'i', 5?: 'f'|'g'|'h'|'i', 6?: 'g'|'h'|'i', 7?: 'h'|'i', 8?: 'i'}|list{0: 'b', 1: 2|'c'|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'d'|'e'|'f'|'g'|'h'|'i', 3?: 'e'|'f'|'g'|'h'|'i', 4?: 'f'|'g'|'h'|'i', 5?: 'g'|'h'|'i', 6?: 'h'|'i', 7?: 'i'}|list{0: 'c', 1: 2|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'e'|'f'|'g'|'h'|'i', 3?: 'f'|'g'|'h'|'i', 4?: 'g'|'h'|'i', 5?: 'h'|'i', 6?: 'i'}|list{0: 'd', 1: 2|'e'|'f'|'g'|'h'|'i', 2: 3|'f'|'g'|'h'|'i', 3?: 'g'|'h'|'i', 4?: 'h'|'i', 5?: 'i'}|list{0: 'e', 1: 2|'f'|'g'|'h'|'i', 2: 3|'g'|'h'|'i', 3?: 'h'|'i', 4?: 'i'}|list{0: 'f', 1: 2|'g'|'h'|'i', 2: 3|'h'|'i', 3?: 'i'}", $arr + $arr2);
assertType("array{'g', 2|'h'|'i', 3|'i'}|array{'h', 2|'i', 3}|array{1|'i', 2, 3}|list{0: 'a'|'b'|'c'|'d'|'e'|'f', 1: 2|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i', 2: 3|'c'|'d'|'e'|'f'|'g'|'h'|'i', 3?: 'd'|'e'|'f'|'g'|'h'|'i', 4?: 'e'|'f'|'g'|'h'|'i', 5?: 'f'|'g'|'h'|'i', 6?: 'g'|'h'|'i', 7?: 'h'|'i', 8?: 'i'}", $arr + $arr2);
}
}
29 changes: 29 additions & 0 deletions tests/PHPStan/Analyser/nsrt/loop-in-unrolled-foreach-in-loop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace LoopInUnrolledForeachInLoop;

use function PHPStan\Testing\assertType;

/**
* @param array<int, array{analysis?: list<string>, declarations?: list<string>}> $dependentsById
*/
function collect(array $dependentsById): void
{
$ids = [];
$paths = [];
foreach ($dependentsById as $dependents) {
$dependentIds = [];
foreach (['analysis', 'declarations'] as $kind) {
$pathIds = [];
foreach ($dependents[$kind] ?? [] as $dependentFile) {
if (!isset($ids[$dependentFile])) {
$ids[$dependentFile] = count($paths);
$paths[] = $dependentFile;
}
$pathIds[] = $ids[$dependentFile];
}
$dependentIds[$kind] = $pathIds;
}
assertType('array{analysis: list<int<0, max>>, declarations: list<int<0, max>>}', $dependentIds);
}
}
Loading
Loading