1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/Loop/ForeachTest.php

941 lines
30 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\Loop;
use Psalm\Tests\Traits;
class ForeachTest extends \Psalm\Tests\TestCase
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @return array
*/
public function providerFileCheckerValidCodeParse()
{
return [
'switchVariableWithContinue' => [
'<?php
foreach (["a", "b", "c"] as $letter) {
switch ($letter) {
case "b":
$foo = 1;
break;
case "c":
$foo = 2;
break;
default:
continue 2;
}
2017-06-29 16:22:49 +02:00
$moo = $foo;
2017-05-27 02:05:57 +02:00
}',
],
'switchVariableWithContinueAndIfs' => [
'<?php
foreach (["a", "b", "c"] as $letter) {
switch ($letter) {
case "a":
if (rand(0, 10) === 1) {
continue 2;
}
$foo = 1;
break;
case "b":
if (rand(0, 10) === 1) {
continue 2;
}
$foo = 2;
break;
default:
continue 2;
}
2017-06-29 16:22:49 +02:00
$moo = $foo;
2017-05-27 02:05:57 +02:00
}',
],
'switchVariableWithFallthrough' => [
'<?php
foreach (["a", "b", "c"] as $letter) {
switch ($letter) {
case "a":
case "b":
$foo = 2;
break;
2017-06-29 16:22:49 +02:00
default:
$foo = 3;
break;
}
2017-06-29 16:22:49 +02:00
$moo = $foo;
2017-05-27 02:05:57 +02:00
}',
'assertions' => [
'$moo' => 'int',
],
],
'switchVariableWithFallthroughStatement' => [
'<?php
foreach (["a", "b", "c"] as $letter) {
switch ($letter) {
case "a":
$bar = 1;
2017-06-29 16:22:49 +02:00
case "b":
$foo = 2;
break;
2017-06-29 16:22:49 +02:00
default:
$foo = 3;
break;
}
2017-06-29 16:22:49 +02:00
$moo = $foo;
2017-05-27 02:05:57 +02:00
}',
'assertions' => [
'$moo' => 'int',
],
],
'secondLoopWithNotNullCheck' => [
'<?php
/** @return void **/
function takesInt(int $i) {}
2017-06-29 16:22:49 +02:00
$a = null;
2017-06-29 16:22:49 +02:00
foreach ([1, 2, 3] as $i) {
if ($a !== null) takesInt($a);
$a = $i;
2017-05-27 02:05:57 +02:00
}',
],
'secondLoopWithIntCheck' => [
'<?php
/** @return void **/
function takesInt(int $i) {}
2017-06-29 16:22:49 +02:00
$a = null;
2017-06-29 16:22:49 +02:00
foreach ([1, 2, 3] as $i) {
if (is_int($a)) takesInt($a);
$a = $i;
2017-05-27 02:05:57 +02:00
}',
],
'secondLoopWithIntCheckAndConditionalSet' => [
'<?php
/** @return void **/
function takesInt(int $i) {}
2017-06-29 16:22:49 +02:00
$a = null;
2017-06-29 16:22:49 +02:00
foreach ([1, 2, 3] as $i) {
if (is_int($a)) takesInt($a);
2017-06-29 16:22:49 +02:00
if (rand(0, 1)) {
$a = $i;
}
2017-05-27 02:05:57 +02:00
}',
],
'secondLoopWithIntCheckAndAssignmentsInIfAndElse' => [
'<?php
/** @return void **/
function takesInt(int $i) {}
2017-06-29 16:22:49 +02:00
$a = null;
2017-06-29 16:22:49 +02:00
foreach ([1, 2, 3] as $i) {
if (is_int($a)) {
$a = 6;
} else {
$a = $i;
}
2017-05-27 02:05:57 +02:00
}',
],
'secondLoopWithIntCheckAndLoopSet' => [
'<?php
/** @return void **/
function takesInt(int $i) {}
2017-06-29 16:22:49 +02:00
$a = null;
2017-06-29 16:22:49 +02:00
foreach ([1, 2, 3] as $i) {
if (is_int($a)) takesInt($a);
2017-06-29 16:22:49 +02:00
while (rand(0, 1)) {
$a = $i;
}
2017-05-27 02:05:57 +02:00
}',
],
'secondLoopWithReturnInElseif' => [
'<?php
class A {}
class B extends A {}
class C extends A {}
2017-06-29 16:22:49 +02:00
$b = null;
2017-06-29 16:22:49 +02:00
foreach ([new A, new A] as $a) {
if ($a instanceof B) {
2017-06-29 16:22:49 +02:00
} elseif (!$a instanceof C) {
return "goodbye";
}
2017-06-29 16:22:49 +02:00
if ($b instanceof C) {
return "hello";
}
2017-06-29 16:22:49 +02:00
$b = $a;
2017-05-27 02:05:57 +02:00
}',
],
'thirdLoopWithIntCheckAndLoopSet' => [
'<?php
/** @return void **/
function takesInt(int $i) {}
2017-06-29 16:22:49 +02:00
$a = null;
$b = null;
2017-06-29 16:22:49 +02:00
foreach ([1, 2, 3] as $i) {
if ($b !== null) {
takesInt($b);
}
2017-06-29 16:22:49 +02:00
if ($a !== null) {
takesInt($a);
$b = $a;
}
2017-06-29 16:22:49 +02:00
$a = $i;
2017-05-27 02:05:57 +02:00
}',
],
'unsetInLoop' => [
'<?php
$a = null;
2017-06-29 16:22:49 +02:00
foreach ([1, 2, 3] as $i) {
$a = $i;
unset($i);
2017-05-27 02:05:57 +02:00
}',
],
'assignInsideForeach' => [
'<?php
$b = false;
2017-06-29 16:22:49 +02:00
foreach ([1, 2, 3, 4] as $a) {
if ($a === rand(0, 10)) {
$b = true;
}
}',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$b' => 'bool',
2017-05-27 02:05:57 +02:00
],
],
'assignInsideForeachWithBreak' => [
'<?php
$b = false;
2017-06-29 16:22:49 +02:00
foreach ([1, 2, 3, 4] as $a) {
if ($a === rand(0, 10)) {
$b = true;
break;
}
}',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$b' => 'bool',
2017-05-27 02:05:57 +02:00
],
],
'nullCheckInsideForeachWithContinue' => [
'<?php
class A {
/** @return array<A|null> */
public static function loadMultiple()
{
return [new A, null];
}
2017-06-29 16:22:49 +02:00
/** @return void */
public function barBar() {
2017-06-29 16:22:49 +02:00
}
}
2017-06-29 16:22:49 +02:00
foreach (A::loadMultiple() as $a) {
if ($a === null) {
continue;
}
2017-06-29 16:22:49 +02:00
$a->barBar();
2017-05-27 02:05:57 +02:00
}',
],
'loopWithArrayKey' => [
'<?php
/**
* @param array<array<int, array<string, string>>> $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' => [
'<?php
$a = [];
$b = rand(0, 10) > 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) {}',
],
2017-12-03 00:28:18 +01:00
'bleedVarIntoOuterContextWithEmptyLoop' => [
'<?php
$tag = null;
foreach (["a", "b", "c"] as $tag) {
}',
'assignments' => [
'$tag' => 'string',
2017-12-03 00:28:18 +01:00
],
],
'bleedVarIntoOuterContextWithRedefinedAsNull' => [
'<?php
$tag = null;
foreach (["a", "b", "c"] as $tag) {
if ($tag === "a") {
$tag = null;
} else {
$tag = null;
}
}',
'assignments' => [
'$tag' => 'null',
],
],
'bleedVarIntoOuterContextWithRedefinedAsNullAndBreak' => [
'<?php
$tag = null;
foreach (["a", "b", "c"] as $tag) {
if ($tag === "a") {
$tag = null;
break;
} elseif ($tag === "b") {
$tag = null;
break;
} else {
$tag = null;
break;
}
}',
'assignments' => [
'$tag' => 'null',
],
],
'bleedVarIntoOuterContextWithBreakInElse' => [
'<?php
$tag = null;
foreach (["a", "b", "c"] as $tag) {
if ($tag === "a") {
$tag = null;
} else {
break;
}
}',
'assignments' => [
'$tag' => 'string|null',
],
],
'bleedVarIntoOuterContextWithBreakInIf' => [
'<?php
$tag = null;
foreach (["a", "b", "c"] as $tag) {
if ($tag === "a") {
break;
} else {
$tag = null;
}
}',
'assignments' => [
'$tag' => 'string|null',
],
],
'bleedVarIntoOuterContextWithBreakInElseAndIntSet' => [
'<?php
$tag = null;
foreach (["a", "b", "c"] as $tag) {
if ($tag === "a") {
$tag = 5;
} else {
break;
}
}',
'assignments' => [
'$tag' => 'string|int|null',
2017-12-03 00:28:18 +01:00
],
],
'bleedVarIntoOuterContextWithRedefineAndBreak' => [
'<?php
$tag = null;
foreach (["a", "b", "c"] as $tag) {
if ($tag === "a") {
$tag = null;
} else {
$tag = null;
break;
}
}',
'assignments' => [
'$tag' => 'null',
],
],
'nullToMixedWithNullCheckNoContinue' => [
2017-12-03 00:28:18 +01:00
'<?php
function getStrings(): array {
return ["hello", "world"];
}
2017-12-03 00:28:18 +01:00
$a = null;
foreach (getStrings() as $s) {
2017-12-03 00:28:18 +01:00
if ($a === null) {
$a = $s;
}
}',
'assignments' => [
'$a' => 'mixed',
],
'error_levels' => [
'MixedAssignment',
],
],
'nullToMixedWithNullCheckAndContinue' => [
'<?php
$a = null;
function getStrings(): array {
return ["hello", "world"];
}
$a = null;
foreach (getStrings() as $s) {
if ($a === null) {
$a = $s;
continue;
2017-12-03 00:28:18 +01:00
}
}',
'assignments' => [
'$a' => 'mixed',
],
'error_levels' => [
'MixedAssignment',
],
],
'falseToBoolExplicitBreak' => [
'<?php
$a = false;
foreach (["a", "b", "c"] as $tag) {
$a = true;
break;
}',
'assignments' => [
'$a' => 'bool',
],
],
'falseToBoolExplicitContinue' => [
'<?php
$a = false;
foreach (["a", "b", "c"] as $tag) {
$a = true;
continue;
}',
'assignments' => [
'$a' => 'bool',
],
],
'falseToBoolInBreak' => [
'<?php
$a = false;
foreach (["a", "b", "c"] as $tag) {
if ($tag === "a") {
$a = true;
break;
} else {
$a = true;
break;
}
}',
'assignments' => [
'$a' => 'bool',
],
],
'falseToBoolInContinue' => [
'<?php
$a = false;
foreach (["a", "b", "c"] as $tag) {
if ($tag === "a") {
$a = true;
continue;
}
}',
'assignments' => [
'$a' => 'bool',
],
],
'falseToBoolInBreakAndContinue' => [
'<?php
$a = false;
foreach (["a", "b", "c"] as $tag) {
if ($tag === "a") {
$a = true;
break;
}
if ($tag === "b") {
$a = true;
continue;
}
}',
'assignments' => [
'$a' => 'bool',
],
],
'falseToBoolInNestedForeach' => [
'<?php
$a = false;
foreach (["d", "e", "f"] as $l) {
foreach (["a", "b", "c"] as $tag) {
if (!$a) {
if (rand(0, 10)) {
$a = true;
break;
} else {
$a = true;
break;
}
}
}
}',
'assignments' => [
'$a' => 'bool',
],
],
'falseToBoolAfterContinueAndBreak' => [
'<?php
$a = false;
foreach ([1, 2, 3] as $i) {
if ($i > 1) {
$a = true;
continue;
}
break;
}',
'assignments' => [
'$a' => 'bool',
],
],
2017-12-03 00:28:18 +01:00
'variableDefinedInForeachAndIf' => [
'<?php
foreach ([1,2,3,4] as $i) {
if ($i === 1) {
$a = true;
} else {
$a = false;
}
echo $a;
}',
],
'noRedundantConditionAfterIsNumeric' => [
'<?php
$ids = [];
foreach (explode(",", "hello,5,20") as $i) {
if (!is_numeric($i)) {
continue;
}
$ids[] = $i;
}',
],
2017-12-05 22:54:24 +01:00
'mixedArrayAccessNoPossiblyUndefinedVar' => [
'<?php
2018-01-11 21:50:45 +01:00
function foo(array $arr): void {
2017-12-05 22:54:24 +01:00
$r = [];
foreach ($arr as $key => $value) {
if ($value["foo"]) {}
$r[] = $key;
}
}',
'assignments' => [],
'error_levels' => [
'MixedAssignment', 'MixedArrayAccess',
],
],
'foreachLoopWithOKManipulation' => [
'<?php
$list = [1, 2, 3];
foreach ($list as $i) {
$i = 5;
}',
],
'foreachLoopDuplicateList' => [
'<?php
$list = [1, 2, 3];
foreach ($list as $i) {
foreach ($list as $j) {}
}',
],
'arrayKeyJustSetInLoop' => [
'<?php
$a = null;
$arr = [];
foreach ([1, 2, 3] as $_) {
if (rand(0, 1)) {
$arr["a"]["c"] = "foo";
$a = $arr["a"]["c"];
} else {
$arr["b"]["c"] = "bar";
$a = $arr["b"]["c"];
}
}',
],
'updateExistingValueAfterLoopContinue' => [
'<?php
$i = false;
$b = (bool) rand(0, 1);
foreach ([$b] as $n) {
$i = $n;
if ($i) {
continue;
}
}
if ($i) {}',
],
'possiblyUndefinedVariableInForeach' => [
'<?php
foreach ([1, 2, 3, 4] as $b) {
$car = "Volvo";
}
echo $car;',
],
'possiblyUndefinedVariableInForeachDueToBreakAfter' => [
'<?php
foreach ([1, 2, 3, 4] as $b) {
$car = "Volvo";
if (rand(0, 1)) {
break;
}
}
echo $car;',
],
'iteratorAggregateIteration' => [
'<?php
class C implements IteratorAggregate
{
public function getIterator(): Iterator
{
return new ArrayIterator([]);
}
}
function loopT(Traversable $coll): void
{
foreach ($coll as $item) {}
}
function loopI(IteratorAggregate $coll): void
{
foreach ($coll as $item) {}
}
loopT(new C);
loopI(new C);',
'assignments' => [],
'error_levels' => [
'MixedAssignment', 'UndefinedThisPropertyAssignment',
],
],
'intersectionIterator' => [
'<?php
/**
* @param \Traversable<int>&\Countable $object
*/
function doSomethingUseful($object) : void {
echo count($object);
foreach ($object as $foo) {}
}'
],
'arrayIteratorIteration' => [
'<?php
class Item {
/**
* @var string
*/
public $prop = "var";
}
class Collection implements IteratorAggregate {
/**
* @var Item[]
*/
private $items = [];
public function add(Item $item): void
{
$this->items[] = $item;
}
/**
* @return \ArrayIterator<mixed, Item>
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->items);
}
}
$collection = new Collection();
$collection->add(new Item());
foreach ($collection as $item) {
echo $item->prop;
}'
],
'foreachIntersectionTraversable' => [
'<?php
/** @var Countable&Traversable<int> */
$c = null;
foreach ($c as $i) {}',
],
'iterateOverNonEmptyConstant' => [
'<?php
class A {
const ARR = [0, 1, 2];
public function test() : int
{
foreach (self::ARR as $val) {
$max = $val;
}
return $max;
}
}'
],
];
2017-03-13 23:07:36 +01:00
}
/**
* @return array
2017-03-13 23:07:36 +01:00
*/
public function providerFileCheckerInvalidCodeParse()
2017-03-13 23:07:36 +01:00
{
return [
'switchVariableWithContinueOnce' => [
'<?php
foreach (["a", "b", "c"] as $letter) {
switch ($letter) {
case "b":
$foo = 1;
break;
case "c":
$foo = 2;
break;
default:
continue;
}
$moo = $foo;
}',
'error_message' => 'PossiblyUndefinedGlobalVariable',
],
'possiblyUndefinedArrayInForeach' => [
'<?php
foreach ([1, 2, 3, 4] as $b) {
$array[] = "hello";
}
2017-06-29 16:22:49 +02:00
echo $array;',
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:3 - Possibly undefined ' .
'global variable $array, first seen on line 3',
],
'possibleUndefinedVariableInForeachAndIfWithBreak' => [
'<?php
foreach ([1,2,3,4] as $i) {
if ($i === 1) {
$a = true;
break;
}
}
2017-06-29 16:22:49 +02:00
echo $a;',
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:9 - Possibly undefined ' .
'global variable $a, first seen on line 4',
],
2017-12-03 00:28:18 +01:00
'possibleUndefinedVariableInForeachAndIf' => [
'<?php
foreach ([1,2,3,4] as $i) {
if ($i === 1) {
$a = true;
}
echo $a;
}',
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:7 - Possibly undefined ' .
'global variable $a, first seen on line 4',
2017-12-03 00:28:18 +01:00
],
'implicitFourthLoopWithBadReturnType' => [
'<?php
function test(): int {
$x = 0;
$y = 1;
$z = 2;
2017-12-03 00:28:18 +01:00
foreach ([0, 1, 2] as $i) {
$x = $y;
$y = $z;
$z = "hello";
}
return $x;
}',
'error_message' => 'InvalidReturnStatement',
],
'possiblyNullCheckInsideForeachWithNoLeaveStatement' => [
'<?php
class A {
/** @return array<A|null> */
public static function loadMultiple()
{
return [new A, null];
}
2017-06-29 16:22:49 +02:00
/** @return void */
public function barBar() {
2017-06-29 16:22:49 +02:00
}
}
2017-06-29 16:22:49 +02:00
foreach (A::loadMultiple() as $a) {
if ($a === null) {
// do nothing
}
2017-06-29 16:22:49 +02:00
$a->barBar();
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'PossiblyNullReference',
],
2017-12-03 00:28:18 +01:00
'redundantConditionInForeachIf' => [
'<?php
$a = false;
foreach (["a", "b", "c"] as $tag) {
if (!$a) {
$a = true;
break;
}
}',
'error_message' => 'RedundantCondition',
],
'redundantConditionInForeachWithIfElse' => [
'<?php
$a = false;
foreach (["a", "b", "c"] as $tag) {
if (!$a) {
if (rand(0, 1)) {
$a = true;
break;
} else {
$a = true;
break;
}
}
}',
'error_message' => 'RedundantCondition',
],
'foreachLoopInvalidation' => [
'<?php
$list = [1, 2, 3];
foreach ($list as $i) {
$list = [4, 5, 6];
}',
'error_message' => 'LoopInvalidation',
],
'possiblyUndefinedVariableInForeachDueToBreakBefore' => [
'<?php
foreach ([1, 2, 3, 4] as $b) {
if (rand(0, 1)) {
break;
}
$car = "Volvo";
}
echo $car;',
'error_message' => 'PossiblyUndefinedGlobalVariable',
],
'continueOutsideLoop' => [
'<?php
continue;',
'error_message' => 'ContinueOutsideLoop',
],
'invalidIterator' => [
'<?php
foreach (5 as $a) {
}',
'error_message' => 'InvalidIterator',
],
'rawObjectIteration' => [
'<?php
class A {
/** @var ?string */
public $foo;
}
class B extends A {}
function bar(A $a): void {}
$arr = [];
if (rand(0, 10) > 5) {
$arr[] = new A;
} else {
$arr = new B;
}
foreach ($arr as $a) {
bar($a);
}',
'error_message' => 'RawObjectIteration',
],
];
2017-03-13 23:07:36 +01:00
}
}