2017-02-21 22:33:20 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 13:12:06 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-11-10 01:54:10 +01:00
|
|
|
namespace Psalm\Tests\Loop;
|
2017-02-21 22:33:20 +01:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Tests\TestCase;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
2018-11-10 01:54:10 +01:00
|
|
|
|
2021-06-08 04:55:21 +02:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
class ForeachTest extends TestCase
|
2017-02-21 22:33:20 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2017-02-21 22:33:20 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2017-02-21 22:33:20 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'switchVariableWithContinue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-24 06:01:08 +01:00
|
|
|
foreach (["a", "b", "c"] as $letter) {
|
2017-04-25 05:45:02 +02:00
|
|
|
switch ($letter) {
|
2018-01-24 06:01:08 +01:00
|
|
|
case "b":
|
2017-04-25 05:45:02 +02:00
|
|
|
$foo = 1;
|
|
|
|
break;
|
2018-01-24 06:01:08 +01:00
|
|
|
case "c":
|
2017-04-25 05:45:02 +02:00
|
|
|
$foo = 2;
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-24 06:01:08 +01:00
|
|
|
continue 2;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$moo = $foo;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'switchVariableWithContinueAndIfs' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-24 06:01:08 +01:00
|
|
|
foreach (["a", "b", "c"] as $letter) {
|
2017-04-25 05:45:02 +02:00
|
|
|
switch ($letter) {
|
2018-01-24 06:01:08 +01:00
|
|
|
case "a":
|
2017-04-25 05:45:02 +02:00
|
|
|
if (rand(0, 10) === 1) {
|
2018-01-24 06:01:08 +01:00
|
|
|
continue 2;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
$foo = 1;
|
|
|
|
break;
|
2018-01-24 06:01:08 +01:00
|
|
|
case "b":
|
2017-04-25 05:45:02 +02:00
|
|
|
if (rand(0, 10) === 1) {
|
2018-01-24 06:01:08 +01:00
|
|
|
continue 2;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
$foo = 2;
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-24 06:01:08 +01:00
|
|
|
continue 2;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$moo = $foo;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'switchVariableWithFallthrough' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-24 06:01:08 +01:00
|
|
|
foreach (["a", "b", "c"] as $letter) {
|
2017-04-25 05:45:02 +02:00
|
|
|
switch ($letter) {
|
2018-01-24 06:01:08 +01:00
|
|
|
case "a":
|
|
|
|
case "b":
|
2017-04-25 05:45:02 +02:00
|
|
|
$foo = 2;
|
|
|
|
break;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
default:
|
|
|
|
$foo = 3;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$moo = $foo;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2018-11-10 22:26:03 +01:00
|
|
|
'assertions' => [
|
|
|
|
'$moo' => 'int',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'switchVariableWithFallthroughStatement' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-24 06:01:08 +01:00
|
|
|
foreach (["a", "b", "c"] as $letter) {
|
2017-04-25 05:45:02 +02:00
|
|
|
switch ($letter) {
|
2018-01-24 06:01:08 +01:00
|
|
|
case "a":
|
2017-04-25 05:45:02 +02:00
|
|
|
$bar = 1;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2018-01-24 06:01:08 +01:00
|
|
|
case "b":
|
2017-04-25 05:45:02 +02:00
|
|
|
$foo = 2;
|
|
|
|
break;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
default:
|
|
|
|
$foo = 3;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$moo = $foo;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2018-11-10 22:26:03 +01:00
|
|
|
'assertions' => [
|
|
|
|
'$moo' => 'int',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'secondLoopWithNotNullCheck' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void **/
|
|
|
|
function takesInt(int $i) {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
if ($a !== null) takesInt($a);
|
|
|
|
$a = $i;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'secondLoopWithIntCheck' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void **/
|
|
|
|
function takesInt(int $i) {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
if (is_int($a)) takesInt($a);
|
|
|
|
$a = $i;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'secondLoopWithIntCheckAndConditionalSet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void **/
|
|
|
|
function takesInt(int $i) {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
if (is_int($a)) takesInt($a);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = $i;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'secondLoopWithIntCheckAndAssignmentsInIfAndElse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void **/
|
|
|
|
function takesInt(int $i) {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +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
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'secondLoopWithIntCheckAndLoopSet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void **/
|
|
|
|
function takesInt(int $i) {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
if (is_int($a)) takesInt($a);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
while (rand(0, 1)) {
|
|
|
|
$a = $i;
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'secondLoopWithReturnInElseif' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([new A, new A] as $a) {
|
|
|
|
if ($a instanceof B) {
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
} elseif (!$a instanceof C) {
|
|
|
|
return "goodbye";
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($b instanceof C) {
|
|
|
|
return "hello";
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'thirdLoopWithIntCheckAndLoopSet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void **/
|
|
|
|
function takesInt(int $i) {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = null;
|
|
|
|
$b = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
if ($b !== null) {
|
|
|
|
takesInt($b);
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a !== null) {
|
|
|
|
takesInt($a);
|
|
|
|
$b = $a;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = $i;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'unsetInLoop' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
$a = $i;
|
|
|
|
unset($i);
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'assignInsideForeach' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = false;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +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
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'assignInsideForeachWithBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = false;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +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
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullCheckInsideForeachWithContinue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return array<A|null> */
|
|
|
|
public static function loadMultiple()
|
|
|
|
{
|
|
|
|
return [new A, null];
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function barBar() {
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach (A::loadMultiple() as $a) {
|
|
|
|
if ($a === null) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a->barBar();
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-11-08 14:23:34 +01:00
|
|
|
'loopWithArrayKey' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-08 14:23:34 +01:00
|
|
|
/**
|
2017-11-16 06:27:11 +01:00
|
|
|
* @param array<array<int, array<string, string>>> $args
|
2017-11-08 14:23:34 +01:00
|
|
|
* @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;
|
|
|
|
}',
|
2017-11-20 03:36:09 +01:00
|
|
|
],
|
2017-11-22 04:13:46 +01:00
|
|
|
'loopWithIfElseNoParadox' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-22 04:13:46 +01:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$tag = null;
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2018-11-10 22:10:59 +01:00
|
|
|
'$tag' => 'string',
|
2017-12-03 00:28:18 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'bleedVarIntoOuterContextWithRedefinedAsNull' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$tag = null;
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if ($tag === "a") {
|
|
|
|
$tag = null;
|
|
|
|
} else {
|
|
|
|
$tag = null;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'$tag' => 'null',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'bleedVarIntoOuterContextWithRedefinedAsNullAndBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$tag = null;
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if ($tag === "a") {
|
|
|
|
$tag = null;
|
|
|
|
break;
|
|
|
|
} elseif ($tag === "b") {
|
|
|
|
$tag = null;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$tag = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'$tag' => 'null',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'bleedVarIntoOuterContextWithBreakInElse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$tag = null;
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if ($tag === "a") {
|
|
|
|
$tag = null;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$tag' => 'null|string',
|
2017-12-03 00:28:18 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'bleedVarIntoOuterContextWithBreakInIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$tag = null;
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if ($tag === "a") {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$tag = null;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$tag' => 'null|string',
|
2017-12-03 00:28:18 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'bleedVarIntoOuterContextWithBreakInElseAndIntSet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$tag = null;
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if ($tag === "a") {
|
|
|
|
$tag = 5;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$tag' => 'int|null|string',
|
2017-12-03 00:28:18 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'bleedVarIntoOuterContextWithRedefineAndBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$tag = null;
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if ($tag === "a") {
|
|
|
|
$tag = null;
|
|
|
|
} else {
|
|
|
|
$tag = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'$tag' => 'null',
|
|
|
|
],
|
|
|
|
],
|
2018-02-08 20:02:32 +01:00
|
|
|
'nullToMixedWithNullCheckNoContinue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-02-08 20:02:32 +01:00
|
|
|
function getStrings(): array {
|
|
|
|
return ["hello", "world"];
|
|
|
|
}
|
|
|
|
|
2017-12-03 00:28:18 +01:00
|
|
|
$a = null;
|
|
|
|
|
2018-02-08 20:02:32 +01:00
|
|
|
foreach (getStrings() as $s) {
|
2017-12-03 00:28:18 +01:00
|
|
|
if ($a === null) {
|
2018-02-08 20:02:32 +01:00
|
|
|
$a = $s;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2018-02-08 20:02:32 +01:00
|
|
|
'$a' => 'mixed',
|
|
|
|
],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [
|
2018-02-08 20:02:32 +01:00
|
|
|
'MixedAssignment',
|
|
|
|
],
|
|
|
|
],
|
2021-12-12 23:00:32 +01:00
|
|
|
'noMixedAssigmentWithIfAssertion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-12 23:00:32 +01:00
|
|
|
$object = new stdClass();
|
|
|
|
$reflection = new ReflectionClass($object);
|
|
|
|
|
|
|
|
foreach ($reflection->getProperties() as $property) {
|
|
|
|
$message = $property->getValue($reflection->newInstance());
|
|
|
|
|
|
|
|
if (!is_string($message)) {
|
|
|
|
throw new RuntimeException();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'noMixedAssigmentWithAssertion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-12 23:00:32 +01:00
|
|
|
$object = new stdClass();
|
|
|
|
$reflection = new ReflectionClass($object);
|
|
|
|
|
|
|
|
foreach ($reflection->getProperties() as $property) {
|
|
|
|
$message = $property->getValue($reflection->newInstance());
|
|
|
|
assert(is_string($message));
|
|
|
|
}',
|
|
|
|
],
|
2018-02-08 20:02:32 +01:00
|
|
|
'nullToMixedWithNullCheckAndContinue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-02-08 20:02:32 +01:00
|
|
|
$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
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2019-02-18 17:39:05 +01:00
|
|
|
'$a' => 'mixed',
|
2017-12-03 00:28:18 +01:00
|
|
|
],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'MixedAssignment',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'falseToBoolExplicitBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$a = false;
|
|
|
|
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'$a' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'falseToBoolExplicitContinue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$a = false;
|
|
|
|
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
$a = true;
|
|
|
|
continue;
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'$a' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'falseToBoolInBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$a = false;
|
|
|
|
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if ($tag === "a") {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'$a' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'falseToBoolInContinue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$a = false;
|
|
|
|
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if ($tag === "a") {
|
|
|
|
$a = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'$a' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'falseToBoolInBreakAndContinue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$a = false;
|
|
|
|
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if ($tag === "a") {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($tag === "b") {
|
|
|
|
$a = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'$a' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'falseToBoolInNestedForeach' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'$a' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
2017-12-07 01:20:24 +01:00
|
|
|
'falseToBoolAfterContinueAndBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-07 01:20:24 +01:00
|
|
|
$a = false;
|
|
|
|
foreach ([1, 2, 3] as $i) {
|
2018-11-10 22:10:59 +01:00
|
|
|
if ($i > 1) {
|
2017-12-07 01:20:24 +01:00
|
|
|
$a = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [
|
2017-12-07 01:20:24 +01:00
|
|
|
'$a' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
2017-12-03 00:28:18 +01:00
|
|
|
'variableDefinedInForeachAndIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
foreach ([1,2,3,4] as $i) {
|
|
|
|
if ($i === 1) {
|
|
|
|
$a = true;
|
|
|
|
} else {
|
|
|
|
$a = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}',
|
|
|
|
],
|
2017-12-03 18:44:08 +01:00
|
|
|
'noRedundantConditionAfterIsNumeric' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 18:44:08 +01:00
|
|
|
$ids = [];
|
|
|
|
foreach (explode(",", "hello,5,20") as $i) {
|
|
|
|
if (!is_numeric($i)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids[] = $i;
|
|
|
|
}',
|
|
|
|
],
|
2017-12-05 22:54:24 +01:00
|
|
|
'mixedArrayAccessNoPossiblyUndefinedVar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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;
|
|
|
|
}
|
|
|
|
}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [
|
2017-12-05 22:54:24 +01:00
|
|
|
'MixedAssignment', 'MixedArrayAccess',
|
|
|
|
],
|
|
|
|
],
|
2017-12-17 16:58:03 +01:00
|
|
|
'foreachLoopWithOKManipulation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-17 16:58:03 +01:00
|
|
|
$list = [1, 2, 3];
|
|
|
|
foreach ($list as $i) {
|
|
|
|
$i = 5;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'foreachLoopDuplicateList' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-17 16:58:03 +01:00
|
|
|
$list = [1, 2, 3];
|
|
|
|
foreach ($list as $i) {
|
|
|
|
foreach ($list as $j) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-04-10 00:02:45 +02:00
|
|
|
'arrayKeyJustSetInLoop' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-10 00:02:45 +02:00
|
|
|
$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"];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-09-21 01:19:00 +02:00
|
|
|
'updateExistingValueAfterLoopContinue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-09-21 01:19:00 +02:00
|
|
|
$i = false;
|
|
|
|
$b = (bool) rand(0, 1);
|
|
|
|
foreach ([$b] as $n) {
|
|
|
|
$i = $n;
|
|
|
|
if ($i) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($i) {}',
|
|
|
|
],
|
2018-11-10 22:10:59 +01:00
|
|
|
'possiblyUndefinedVariableInForeach' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 22:10:59 +01:00
|
|
|
foreach ([1, 2, 3, 4] as $b) {
|
|
|
|
$car = "Volvo";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $car;',
|
|
|
|
],
|
|
|
|
'possiblyUndefinedVariableInForeachDueToBreakAfter' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 22:10:59 +01:00
|
|
|
foreach ([1, 2, 3, 4] as $b) {
|
|
|
|
$car = "Volvo";
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $car;',
|
|
|
|
],
|
|
|
|
'iteratorAggregateIteration' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingTemplateParam
|
|
|
|
*/
|
2018-11-10 22:10:59 +01:00
|
|
|
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);',
|
2022-11-05 22:34:42 +01:00
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [
|
2018-11-10 22:10:59 +01:00
|
|
|
'MixedAssignment', 'UndefinedThisPropertyAssignment',
|
|
|
|
],
|
|
|
|
],
|
2019-05-29 20:22:15 +02:00
|
|
|
'intersectionIterator' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 22:10:59 +01:00
|
|
|
/**
|
2019-01-26 22:58:49 +01:00
|
|
|
* @param \Traversable<int, int>&\Countable $object
|
2018-11-10 22:10:59 +01:00
|
|
|
*/
|
|
|
|
function doSomethingUseful($object) : void {
|
|
|
|
echo count($object);
|
|
|
|
foreach ($object as $foo) {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-11-10 22:10:59 +01:00
|
|
|
],
|
2019-01-26 22:58:49 +01:00
|
|
|
'rawIteratorIteration' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-26 22:58:49 +01:00
|
|
|
class Item {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $prop = "var";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Iterator<int, Item>
|
|
|
|
*/
|
|
|
|
function getIterator(): Iterator {
|
|
|
|
return new ArrayIterator([new Item()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (getIterator() as $item) {
|
|
|
|
echo $item->prop;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'seekableIteratorIteration' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-26 22:58:49 +01:00
|
|
|
class Item {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $prop = "var";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return SeekableIterator<int, Item>
|
|
|
|
*/
|
|
|
|
function getIterator(): \SeekableIterator {
|
|
|
|
return new ArrayIterator([new Item()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (getIterator() as $item) {
|
|
|
|
echo $item->prop;
|
|
|
|
}',
|
|
|
|
],
|
2018-11-10 22:10:59 +01:00
|
|
|
'arrayIteratorIteration' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-26 22:58:49 +01:00
|
|
|
class Item {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $prop = "var";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ArrayIterator<int, Item>
|
|
|
|
*/
|
|
|
|
function getIterator(): \SeekableIterator {
|
|
|
|
return new ArrayIterator([new Item()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (getIterator() as $item) {
|
|
|
|
echo $item->prop;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'templatedIteratorAggregateIteration' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 22:10:59 +01:00
|
|
|
class Item {
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $prop = "var";
|
|
|
|
}
|
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @implements IteratorAggregate<array-key, Item>
|
|
|
|
*/
|
2018-11-10 22:10:59 +01:00
|
|
|
class Collection implements IteratorAggregate {
|
|
|
|
/**
|
|
|
|
* @var Item[]
|
|
|
|
*/
|
|
|
|
private $items = [];
|
|
|
|
|
|
|
|
public function add(Item $item): void
|
|
|
|
{
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-29 17:29:22 +01:00
|
|
|
* @return ArrayIterator<array-key, Item>
|
2018-11-10 22:10:59 +01:00
|
|
|
*/
|
|
|
|
public function getIterator(): \ArrayIterator
|
|
|
|
{
|
2019-01-26 22:58:49 +01:00
|
|
|
return new ArrayIterator($this->items);
|
2018-11-10 22:10:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$collection = new Collection();
|
|
|
|
$collection->add(new Item());
|
|
|
|
foreach ($collection as $item) {
|
|
|
|
echo $item->prop;
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-11-10 22:10:59 +01:00
|
|
|
],
|
|
|
|
'foreachIntersectionTraversable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 22:10:59 +01:00
|
|
|
/** @var Countable&Traversable<int> */
|
|
|
|
$c = null;
|
|
|
|
foreach ($c as $i) {}',
|
|
|
|
],
|
2018-11-10 23:15:37 +01:00
|
|
|
'iterateOverNonEmptyConstant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 23:15:37 +01:00
|
|
|
class A {
|
|
|
|
const ARR = [0, 1, 2];
|
|
|
|
|
|
|
|
public function test() : int
|
|
|
|
{
|
|
|
|
foreach (self::ARR as $val) {
|
|
|
|
$max = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $max;
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-11-10 23:15:37 +01:00
|
|
|
],
|
2018-11-10 23:56:22 +01:00
|
|
|
'ifSpecificMaybeEmptyValues' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 23:56:22 +01:00
|
|
|
foreach ([0, 1, 2, 3] as $i) {
|
|
|
|
$a = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {}',
|
|
|
|
],
|
|
|
|
'ifSpecificMaybeEmptyStringValues' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 23:56:22 +01:00
|
|
|
foreach (["", "1", "2", "3"] as $i) {
|
|
|
|
$a = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {}',
|
|
|
|
],
|
2019-02-05 19:34:59 +01:00
|
|
|
'domNodeListIterator' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-05 19:34:59 +01:00
|
|
|
function foo(DOMNodeList $list) : void {
|
|
|
|
foreach ($list as $item) {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-05 19:34:59 +01:00
|
|
|
],
|
2019-02-06 20:34:04 +01:00
|
|
|
'loopOverArrayChunk' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-06 20:34:04 +01:00
|
|
|
/**
|
2019-02-06 20:49:15 +01:00
|
|
|
* @return array<int, array<array-key, int>>
|
2019-02-06 20:34:04 +01:00
|
|
|
*/
|
|
|
|
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";
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2019-02-06 20:34:04 +01:00
|
|
|
],
|
2019-05-08 15:04:52 +02:00
|
|
|
'iteratorClassCurrent' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-05-08 15:04:52 +02:00
|
|
|
class Value {}
|
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingTemplateParam
|
|
|
|
*/
|
2019-05-08 15:04:52 +02:00
|
|
|
class ValueCollection implements \Countable, \IteratorAggregate {
|
|
|
|
/**
|
|
|
|
* @var Value[]
|
|
|
|
*/
|
|
|
|
private $items = [];
|
|
|
|
|
|
|
|
private function add(Value $item): void
|
|
|
|
{
|
|
|
|
$this->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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingTemplateParam
|
|
|
|
*/
|
2019-05-08 15:04:52 +02:00
|
|
|
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) {}
|
|
|
|
}',
|
|
|
|
],
|
2019-08-20 18:06:02 +02:00
|
|
|
'possibleRawObjectIterationFromIssetSuppressed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-20 18:06:02 +02:00
|
|
|
/**
|
|
|
|
* @psalm-suppress RawObjectIteration
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
*/
|
|
|
|
function foo(array $a) : void {
|
|
|
|
if (isset($a["a"]["b"])) {
|
|
|
|
foreach ($a["a"] as $c) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-08-23 20:15:20 +02:00
|
|
|
'simpleXmlIterator' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-23 20:15:20 +02:00
|
|
|
function f(SimpleXMLElement $elt): void {
|
|
|
|
foreach ($elt as $item) {
|
|
|
|
f($item);
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-08-23 20:15:20 +02:00
|
|
|
],
|
2019-10-21 13:13:27 +02:00
|
|
|
'loopOverIteratorWithTooFewParams' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-10-21 13:13:27 +02:00
|
|
|
/**
|
|
|
|
* @param Iterator<string> $arr
|
|
|
|
*/
|
|
|
|
function foo(Iterator $arr) : void {
|
|
|
|
foreach ($arr as $a) {}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-10-21 13:13:27 +02:00
|
|
|
],
|
2020-01-24 19:55:30 +01:00
|
|
|
'foreachLoopInvalidation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-24 19:55:30 +01:00
|
|
|
$list = [1, 2, 3];
|
|
|
|
foreach ($list as $i) {
|
2020-01-24 20:08:01 +01:00
|
|
|
$list = [4, 5, 6];
|
2020-01-24 19:55:30 +01:00
|
|
|
}',
|
|
|
|
],
|
2020-04-08 22:11:11 +02:00
|
|
|
'createNestedArrayInLoop' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-08 22:11:11 +02:00
|
|
|
function foo() : void {
|
|
|
|
$arr = [];
|
|
|
|
|
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
if (!isset($arr[$i]["a"])) {
|
|
|
|
$arr[$i]["a"] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr[$i]["a"] += 5;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-04-08 22:11:11 +02:00
|
|
|
],
|
2020-04-27 06:41:34 +02:00
|
|
|
'iteratorForeach' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-27 06:41:34 +02:00
|
|
|
/**
|
|
|
|
* @implements Iterator<int, string>
|
|
|
|
*/
|
|
|
|
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;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-04-27 06:41:34 +02:00
|
|
|
],
|
2020-08-29 17:10:09 +02:00
|
|
|
'loopClosure' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-29 17:10:09 +02:00
|
|
|
/**
|
|
|
|
* @param list<0> $currentIndexes
|
|
|
|
*/
|
|
|
|
function cartesianProduct(array $currentIndexes): void {
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
array_map(
|
|
|
|
function ($index) { echo $index; },
|
|
|
|
$currentIndexes
|
|
|
|
);
|
|
|
|
|
2022-12-13 21:40:19 +01:00
|
|
|
/** @psalm-suppress PossiblyUndefinedArrayOffset */
|
2020-08-29 17:10:09 +02:00
|
|
|
$currentIndexes[0]++;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-08-29 17:10:09 +02:00
|
|
|
],
|
2020-09-30 06:04:07 +02:00
|
|
|
'loopCanUpdateOuterWithoutBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-27 23:50:48 +02:00
|
|
|
/**
|
|
|
|
* @param array<int> $mappings
|
|
|
|
*/
|
|
|
|
function foo(string $id, array $mappings) : void {
|
|
|
|
if ($id === "a") {
|
|
|
|
foreach ($mappings as $value) {
|
|
|
|
$id = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 06:04:07 +02:00
|
|
|
if (is_int($id)) {}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-09-30 06:04:07 +02:00
|
|
|
],
|
|
|
|
'loopCanUpdateOuterWithBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-30 06:04:07 +02:00
|
|
|
/**
|
|
|
|
* @param array<int> $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)) {}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-09-30 06:04:07 +02:00
|
|
|
],
|
|
|
|
'loopCanUpdateOuterWithContinue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-30 06:04:07 +02:00
|
|
|
/**
|
|
|
|
* @param array<int> $mappings
|
|
|
|
*/
|
|
|
|
function foo(string $id, array $mappings) : void {
|
|
|
|
if ($id === "a") {
|
|
|
|
foreach ($mappings as $value) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$id = $value;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 23:50:48 +02:00
|
|
|
if (is_int($id)) {}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-09-27 23:50:48 +02:00
|
|
|
],
|
2020-11-01 17:26:42 +01:00
|
|
|
'loopVarRedefinedAtLoopStart' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-01 17:26:42 +01:00
|
|
|
/**
|
|
|
|
* @param non-empty-array<string, string> $files
|
|
|
|
*/
|
|
|
|
function foo(array $files): void
|
|
|
|
{
|
|
|
|
$file = reset($files);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
strlen($file);
|
|
|
|
$file = 0;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-11-01 17:26:42 +01:00
|
|
|
],
|
2023-02-23 01:44:50 +01:00
|
|
|
'arrayIsNotEmptyInForeachLoop' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
/** @return non-empty-array */
|
|
|
|
function f(array $a): array {
|
|
|
|
foreach ($a as $_) {
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
throw new RuntimeException;
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-03-13 23:07:36 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2017-03-13 23:07:36 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
2018-01-24 06:01:08 +01:00
|
|
|
'switchVariableWithContinueOnce' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-24 06:01:08 +01:00
|
|
|
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',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'possiblyUndefinedArrayInForeach' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1, 2, 3, 4] as $b) {
|
|
|
|
$array[] = "hello";
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $array;',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:25 - Possibly undefined ' .
|
2017-12-06 06:56:00 +01:00
|
|
|
'global variable $array, first seen on line 3',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-01-18 23:16:50 +01:00
|
|
|
'possibleUndefinedVariableInForeachAndIfWithBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1,2,3,4] as $i) {
|
|
|
|
if ($i === 1) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $a;',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:9:26 - Possibly undefined ' .
|
2017-12-06 06:56:00 +01:00
|
|
|
'global variable $a, first seen on line 4',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-12-03 00:28:18 +01:00
|
|
|
'possibleUndefinedVariableInForeachAndIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
foreach ([1,2,3,4] as $i) {
|
|
|
|
if ($i === 1) {
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:7:30 - Possibly undefined ' .
|
2017-12-06 06:56:00 +01:00
|
|
|
'global variable $a, first seen on line 4',
|
2017-12-03 00:28:18 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'implicitFourthLoopWithBadReturnType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
function test(): int {
|
|
|
|
$x = 0;
|
|
|
|
$y = 1;
|
|
|
|
$z = 2;
|
2017-12-03 00:28:18 +01:00
|
|
|
foreach ([0, 1, 2] as $i) {
|
2017-04-25 05:45:02 +02:00
|
|
|
$x = $y;
|
|
|
|
$y = $z;
|
|
|
|
$z = "hello";
|
|
|
|
}
|
|
|
|
return $x;
|
|
|
|
}',
|
2017-12-07 21:50:25 +01:00
|
|
|
'error_message' => 'InvalidReturnStatement',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'possiblyNullCheckInsideForeachWithNoLeaveStatement' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return array<A|null> */
|
|
|
|
public static function loadMultiple()
|
|
|
|
{
|
|
|
|
return [new A, null];
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function barBar() {
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach (A::loadMultiple() as $a) {
|
|
|
|
if ($a === null) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a->barBar();
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
|
|
|
],
|
2017-12-03 00:28:18 +01:00
|
|
|
'redundantConditionInForeachIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$a = false;
|
|
|
|
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if (!$a) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
|
|
|
'redundantConditionInForeachWithIfElse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 00:28:18 +01:00
|
|
|
$a = false;
|
|
|
|
|
|
|
|
foreach (["a", "b", "c"] as $tag) {
|
|
|
|
if (!$a) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
2018-11-10 22:10:59 +01:00
|
|
|
'possiblyUndefinedVariableInForeachDueToBreakBefore' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 22:10:59 +01:00
|
|
|
foreach ([1, 2, 3, 4] as $b) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$car = "Volvo";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $car;',
|
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable',
|
|
|
|
],
|
|
|
|
'continueOutsideLoop' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 22:10:59 +01:00
|
|
|
continue;',
|
|
|
|
'error_message' => 'ContinueOutsideLoop',
|
|
|
|
],
|
|
|
|
'invalidIterator' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 22:10:59 +01:00
|
|
|
foreach (5 as $a) {
|
|
|
|
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidIterator',
|
|
|
|
],
|
|
|
|
'rawObjectIteration' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-20 18:06:02 +02:00
|
|
|
class A {
|
|
|
|
/** @var ?string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr = new A;
|
|
|
|
|
|
|
|
foreach ($arr as $a) {}',
|
|
|
|
'error_message' => 'RawObjectIteration',
|
|
|
|
],
|
|
|
|
'possibleRawObjectIteration' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 22:10:59 +01:00
|
|
|
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);
|
|
|
|
}',
|
2019-08-20 18:06:02 +02:00
|
|
|
'error_message' => 'PossibleRawObjectIteration',
|
|
|
|
],
|
|
|
|
'possibleRawObjectIterationFromIsset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-08-20 18:06:02 +02:00
|
|
|
function foo(array $a) : void {
|
|
|
|
if (isset($a["a"]["b"])) {
|
|
|
|
foreach ($a["a"] as $c) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossibleRawObjectIteration',
|
2018-11-10 22:10:59 +01:00
|
|
|
],
|
2018-11-10 23:56:22 +01:00
|
|
|
'ifSpecificNonEmptyValues' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 23:56:22 +01:00
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
$a = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
|
|
|
'ifSpecificNonEmptyStringValues' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 23:56:22 +01:00
|
|
|
foreach (["1", "2", "3"] as $i) {
|
|
|
|
$a = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
2023-02-23 01:44:50 +01:00
|
|
|
'arrayCanBeEmptyOutsideTheLoop' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
/** @return non-empty-array */
|
|
|
|
function f(array $a): array {
|
|
|
|
foreach ($a as $_) {
|
|
|
|
}
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-03-13 23:07:36 +01:00
|
|
|
}
|
2017-02-21 22:33:20 +01:00
|
|
|
}
|