,error_levels?:string[]}> */ public function providerValidCodeParse(): iterable { return [ 'switchVariableWithContinue' => [ ' [ ' [ ' [ '$moo' => 'int', ], ], 'switchVariableWithFallthroughStatement' => [ ' [ '$moo' => 'int', ], ], 'secondLoopWithNotNullCheck' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ '$b' => 'bool', ], ], 'assignInsideForeachWithBreak' => [ ' [ '$b' => 'bool', ], ], 'nullCheckInsideForeachWithContinue' => [ ' */ public static function loadMultiple() { return [new A, null]; } /** @return void */ public function barBar() { } } foreach (A::loadMultiple() as $a) { if ($a === null) { continue; } $a->barBar(); }', ], 'loopWithArrayKey' => [ '>> $args * @return array[] */ function get_merged_dict(array $args) { $merged = array(); foreach ($args as $group) { foreach ($group as $key => $value) { if (isset($merged[$key])) { $merged[$key] = array_merge($merged[$key], $value); } else { $merged[$key] = $value; } } } return $merged; }', ], 'loopWithIfElseNoParadox' => [ ' 5; foreach ([1, 2, 3] as $i) { if (rand(0, 5)) { $a[] = 5; continue; } if ($b) { continue; // if this is removed, no failure } else {} // if else is removed, no failure } if ($a) {}', ], 'bleedVarIntoOuterContextWithEmptyLoop' => [ ' [ '$tag' => 'string', ], ], 'bleedVarIntoOuterContextWithRedefinedAsNull' => [ ' [ '$tag' => 'null', ], ], 'bleedVarIntoOuterContextWithRedefinedAsNullAndBreak' => [ ' [ '$tag' => 'null', ], ], 'bleedVarIntoOuterContextWithBreakInElse' => [ ' [ '$tag' => 'null|string', ], ], 'bleedVarIntoOuterContextWithBreakInIf' => [ ' [ '$tag' => 'null|string', ], ], 'bleedVarIntoOuterContextWithBreakInElseAndIntSet' => [ ' [ '$tag' => 'int|null|string', ], ], 'bleedVarIntoOuterContextWithRedefineAndBreak' => [ ' [ '$tag' => 'null', ], ], 'nullToMixedWithNullCheckNoContinue' => [ ' [ '$a' => 'mixed', ], 'error_levels' => [ 'MixedAssignment', ], ], 'nullToMixedWithNullCheckAndContinue' => [ ' [ '$a' => 'mixed', ], 'error_levels' => [ 'MixedAssignment', ], ], 'falseToBoolExplicitBreak' => [ ' [ '$a' => 'bool', ], ], 'falseToBoolExplicitContinue' => [ ' [ '$a' => 'bool', ], ], 'falseToBoolInBreak' => [ ' [ '$a' => 'bool', ], ], 'falseToBoolInContinue' => [ ' [ '$a' => 'bool', ], ], 'falseToBoolInBreakAndContinue' => [ ' [ '$a' => 'bool', ], ], 'falseToBoolInNestedForeach' => [ ' [ '$a' => 'bool', ], ], 'falseToBoolAfterContinueAndBreak' => [ ' 1) { $a = true; continue; } break; }', 'assignments' => [ '$a' => 'bool', ], ], 'variableDefinedInForeachAndIf' => [ ' [ ' [ ' $value) { if ($value["foo"]) {} $r[] = $key; } }', 'assignments' => [], 'error_levels' => [ 'MixedAssignment', 'MixedArrayAccess', ], ], 'foreachLoopWithOKManipulation' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [], 'error_levels' => [ 'MixedAssignment', 'UndefinedThisPropertyAssignment', ], ], 'intersectionIterator' => [ '&\Countable $object */ function doSomethingUseful($object) : void { echo count($object); foreach ($object as $foo) {} }', ], 'rawIteratorIteration' => [ ' */ function getIterator(): Iterator { return new ArrayIterator([new Item()]); } foreach (getIterator() as $item) { echo $item->prop; }', ], 'seekableIteratorIteration' => [ ' */ function getIterator(): \SeekableIterator { return new ArrayIterator([new Item()]); } foreach (getIterator() as $item) { echo $item->prop; }', ], 'arrayIteratorIteration' => [ ' */ function getIterator(): \SeekableIterator { return new ArrayIterator([new Item()]); } foreach (getIterator() as $item) { echo $item->prop; }', ], 'templatedIteratorAggregateIteration' => [ 'items[] = $item; } /** * @return ArrayIterator */ public function getIterator(): \ArrayIterator { return new ArrayIterator($this->items); } } $collection = new Collection(); $collection->add(new Item()); foreach ($collection as $item) { echo $item->prop; }', ], 'foreachIntersectionTraversable' => [ ' */ $c = null; foreach ($c as $i) {}', ], 'iterateOverNonEmptyConstant' => [ ' [ ' [ ' [ ' [ '> */ function Foo(int $a, int $b, int ...$ints) : array { array_unshift($ints, $a, $b); return array_chunk($ints, 2); } foreach(Foo(1, 2, 3, 4, 5) as $ints) { echo $ints[0], ", ", ($ints[1] ?? "n/a"), "\n"; }', ], 'iteratorClassCurrent' => [ 'items[] = $item; } /** * @return Value[] */ public function toArray(): array { return $this->items; } public function getIterator(): ValueCollectionIterator { return new ValueCollectionIterator($this); } public function count(): int { return \count($this->items); } public function isEmpty(): bool { return empty($this->items); } public function contains(Value $item): bool { foreach ($this->items as $_item) { if ($_item === $item) { return true; } } return false; } } class ValueCollectionIterator implements \Countable, \Iterator { /** * @var Value[] */ private $items; /** * @var int */ private $position = 0; public function __construct(ValueCollection $collection) { $this->items = $collection->toArray(); } public function count(): int { return \iterator_count($this); } public function rewind(): void { $this->position = 0; } public function valid(): bool { return $this->position < \count($this->items); } public function key(): int { return $this->position; } public function current(): Value { return $this->items[$this->position]; } public function next(): void { $this->position++; } } function foo(ValueCollection $v) : void { foreach ($v as $value) {} }', ], 'possibleRawObjectIterationFromIssetSuppressed' => [ ' [ ' [ ' $arr */ function foo(Iterator $arr) : void { foreach ($arr as $a) {} }' ], 'foreachLoopInvalidation' => [ ' [ ' [ ' */ class FooIterator implements \Iterator { private ?int $key = null; public function current(): string { return "a"; } public function next(): void { $this->key = $this->key === null ? 0 : $this->key + 1; } public function key(): int { if ($this->key === null) { throw new \Exception(); } return $this->key; } public function valid(): bool { return $this->key !== null && $this->key <= 3; } public function rewind(): void { $this->key = null; $this->next(); } } foreach (new FooIterator() as $key => $value) { echo $key . " " . $value; }' ], 'loopClosure' => [ ' $currentIndexes */ function cartesianProduct(array $currentIndexes): void { while (rand(0, 1)) { array_map( function ($index) { echo $index; }, $currentIndexes ); $currentIndexes[0]++; } }' ], 'loopCanUpdateOuterWithoutBreak' => [ ' $mappings */ function foo(string $id, array $mappings) : void { if ($id === "a") { foreach ($mappings as $value) { $id = $value; } } if (is_int($id)) {} }' ], 'loopCanUpdateOuterWithBreak' => [ ' $mappings */ function foo(string $id, array $mappings) : void { if ($id === "a") { foreach ($mappings as $value) { if (rand(0, 1)) { $id = $value; break; } } } if (is_int($id)) {} }' ], 'loopCanUpdateOuterWithContinue' => [ ' $mappings */ function foo(string $id, array $mappings) : void { if ($id === "a") { foreach ($mappings as $value) { if (rand(0, 1)) { $id = $value; continue; } } } if (is_int($id)) {} }' ], 'loopVarRedefinedAtLoopStart' => [ ' $files */ function foo(array $files): void { $file = reset($files); foreach ($files as $file) { strlen($file); $file = 0; } }' ], ]; } /** * @return iterable */ public function providerInvalidCodeParse(): iterable { return [ 'switchVariableWithContinueOnce' => [ ' 'PossiblyUndefinedGlobalVariable', ], 'possiblyUndefinedArrayInForeach' => [ ' 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:25 - Possibly undefined ' . 'global variable $array, first seen on line 3', ], 'possibleUndefinedVariableInForeachAndIfWithBreak' => [ ' 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:9:26 - Possibly undefined ' . 'global variable $a, first seen on line 4', ], 'possibleUndefinedVariableInForeachAndIf' => [ ' 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:7:30 - Possibly undefined ' . 'global variable $a, first seen on line 4', ], 'implicitFourthLoopWithBadReturnType' => [ ' 'InvalidReturnStatement', ], 'possiblyNullCheckInsideForeachWithNoLeaveStatement' => [ ' */ public static function loadMultiple() { return [new A, null]; } /** @return void */ public function barBar() { } } foreach (A::loadMultiple() as $a) { if ($a === null) { // do nothing } $a->barBar(); }', 'error_message' => 'PossiblyNullReference', ], 'redundantConditionInForeachIf' => [ ' 'RedundantCondition', ], 'redundantConditionInForeachWithIfElse' => [ ' 'RedundantCondition', ], 'possiblyUndefinedVariableInForeachDueToBreakBefore' => [ ' 'PossiblyUndefinedGlobalVariable', ], 'continueOutsideLoop' => [ ' 'ContinueOutsideLoop', ], 'invalidIterator' => [ ' 'InvalidIterator', ], 'rawObjectIteration' => [ ' 'RawObjectIteration', ], 'possibleRawObjectIteration' => [ ' 5) { $arr[] = new A; } else { $arr = new B; } foreach ($arr as $a) { bar($a); }', 'error_message' => 'PossibleRawObjectIteration', ], 'possibleRawObjectIterationFromIsset' => [ ' 'PossibleRawObjectIteration', ], 'ifSpecificNonEmptyValues' => [ ' 'RedundantCondition', ], 'ifSpecificNonEmptyStringValues' => [ ' 'RedundantCondition', ], ]; } }