2017-02-21 22:33:20 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class LoopScopeTest extends TestCase
|
2017-02-21 22:33:20 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-02-21 22:33:20 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-02-21 22:33:20 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2017-02-21 22:33:20 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'switchVariableWithContinue' => [
|
|
|
|
'<?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' => [
|
|
|
|
'<?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' => [
|
|
|
|
'<?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
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'switchVariableWithFallthroughStatement' => [
|
|
|
|
'<?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
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'whileVar' => [
|
|
|
|
'<?php
|
|
|
|
$worked = false;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
while (rand(0,100) === 10) {
|
|
|
|
$worked = true;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$worked' => 'bool',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'doWhileVar' => [
|
|
|
|
'<?php
|
|
|
|
$worked = false;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
do {
|
|
|
|
$worked = true;
|
2017-02-21 22:33:20 +01:00
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
while (rand(0,100) === 10);',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$worked' => 'bool',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-12-05 17:05:10 +01:00
|
|
|
'doWhileUndefinedVar' => [
|
|
|
|
'<?php
|
|
|
|
do {
|
|
|
|
$result = rand(0,1);
|
|
|
|
} while (!$result);',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'doWhileVarAndBreak' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function foo(string $b) {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
do {
|
|
|
|
if (null === ($a = rand(0, 1) ? "hello" : null)) {
|
|
|
|
break;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foo($a);
|
2017-02-21 22:33:20 +01:00
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
while (rand(0,100) === 10);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-11-28 06:46:41 +01:00
|
|
|
'objectValueWithTwoTypes' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
class B {}
|
|
|
|
class A {
|
|
|
|
/** @var A|B */
|
2017-11-28 06:46:41 +01:00
|
|
|
public $parent;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function __construct() {
|
2018-01-11 21:50:45 +01:00
|
|
|
$this->parent = rand(0, 1) ? new A(): new B();
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function makeA(): A {
|
2017-04-25 05:45:02 +02:00
|
|
|
return new A();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = makeA();
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
while ($a instanceof A) {
|
2017-11-28 06:46:41 +01:00
|
|
|
$a = $a->parent;
|
2017-04-25 05:45:02 +02:00
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'B',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-11-28 06:46:41 +01:00
|
|
|
'objectValueWithInstanceofProperty' => [
|
|
|
|
'<?php
|
|
|
|
class B {}
|
|
|
|
class A {
|
|
|
|
/** @var A|B */
|
|
|
|
public $parent;
|
|
|
|
|
|
|
|
public function __construct() {
|
2018-01-11 21:50:45 +01:00
|
|
|
$this->parent = rand(0, 1) ? new A(): new B();
|
2017-11-28 06:46:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function makeA(): A {
|
2017-11-28 06:46:41 +01:00
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = makeA();
|
|
|
|
|
|
|
|
while ($a->parent instanceof A) {
|
|
|
|
$a = $a->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = $a->parent;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'A',
|
|
|
|
'$b' => 'A|B',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'objectValueNullable' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?A */
|
|
|
|
public $parent;
|
|
|
|
|
|
|
|
public function __construct() {
|
2018-01-11 21:50:45 +01:00
|
|
|
$this->parent = rand(0, 1) ? new A(): null;
|
2017-11-28 06:46:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function makeA(): A {
|
2017-11-28 06:46:41 +01:00
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = makeA();
|
|
|
|
|
|
|
|
while ($a) {
|
|
|
|
$a = $a->parent;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'null',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'objectValueWithAnd' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?A */
|
|
|
|
public $parent;
|
|
|
|
|
|
|
|
public function __construct() {
|
2018-01-11 21:50:45 +01:00
|
|
|
$this->parent = rand(0, 1) ? new A(): null;
|
2017-11-28 06:46:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function makeA(): A {
|
2017-11-28 06:46:41 +01:00
|
|
|
return new A();
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = makeA();
|
|
|
|
|
|
|
|
while ($a && rand(0, 10) > 5) {
|
|
|
|
$a = $a->parent;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'A|null',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'secondLoopWithNotNullCheck' => [
|
|
|
|
'<?php
|
|
|
|
/** @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' => [
|
|
|
|
'<?php
|
|
|
|
/** @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' => [
|
|
|
|
'<?php
|
|
|
|
/** @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' => [
|
|
|
|
'<?php
|
|
|
|
/** @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' => [
|
|
|
|
'<?php
|
|
|
|
/** @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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
/** @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
|
|
|
],
|
|
|
|
'implicitFourthLoop' => [
|
|
|
|
'<?php
|
|
|
|
function test(): int {
|
|
|
|
$x = 0;
|
|
|
|
$y = 1;
|
|
|
|
$z = 2;
|
|
|
|
for ($i = 0; $i < 3; $i++) {
|
|
|
|
$x = $y;
|
|
|
|
$y = $z;
|
|
|
|
$z = 5;
|
|
|
|
}
|
|
|
|
return $x;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'unsetInLoop' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
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
|
|
|
],
|
|
|
|
'loopWithNoParadox' => [
|
|
|
|
'<?php
|
|
|
|
$a = ["b", "c", "d"];
|
2018-11-09 16:56:27 +01:00
|
|
|
array_pop($a);
|
2017-11-20 03:36:09 +01:00
|
|
|
while ($a) {
|
2018-05-20 02:31:48 +02:00
|
|
|
$letter = array_pop($a);
|
|
|
|
if (!$a) {}
|
2017-11-20 03:36:09 +01:00
|
|
|
}',
|
2017-11-08 14:23:34 +01:00
|
|
|
],
|
2017-11-22 04:13:46 +01:00
|
|
|
'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' => [
|
2018-05-18 17:02:50 +02:00
|
|
|
'$tag' => 'string|null',
|
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' => [
|
2018-05-18 17:02:50 +02:00
|
|
|
'$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',
|
|
|
|
],
|
|
|
|
],
|
2018-02-08 20:02:32 +01:00
|
|
|
'nullToMixedWithNullCheckNoContinue' => [
|
2017-12-03 00:28:18 +01:00
|
|
|
'<?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;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'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',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'falseToBoolInContinueAndBreak' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
|
|
|
|
for ($i = 0; $i < 4; $i++) {
|
|
|
|
$j = rand(0, 10);
|
|
|
|
|
|
|
|
if ($j === 2) {
|
|
|
|
$a = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($j === 3) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assignments' => [
|
|
|
|
'$a' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
2017-12-07 01:20:24 +01:00
|
|
|
'falseToBoolAfterContinueAndBreak' => [
|
|
|
|
'<?php
|
|
|
|
$a = false;
|
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
if ($i > 0) {
|
|
|
|
$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;
|
|
|
|
}',
|
|
|
|
],
|
2017-12-03 02:02:29 +01:00
|
|
|
'noRedundantConditionInWhileAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?int */
|
|
|
|
public $bar;
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): ?A {
|
2017-12-03 02:02:29 +01:00
|
|
|
return rand(0, 1) ? new A : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ($a = foo()) {
|
|
|
|
if ($a->bar) {}
|
|
|
|
}',
|
|
|
|
],
|
2017-12-03 18:44:08 +01:00
|
|
|
'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',
|
|
|
|
],
|
|
|
|
],
|
2017-12-07 05:46:41 +01:00
|
|
|
'whileTrue' => [
|
|
|
|
'<?php
|
|
|
|
while (true) {
|
|
|
|
$a = "hello";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
$b = 5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(;;) {
|
|
|
|
$c = true;
|
|
|
|
break;
|
|
|
|
}',
|
2018-01-25 19:46:17 +01:00
|
|
|
'assertions' => [
|
2017-12-07 05:46:41 +01:00
|
|
|
'$a' => 'string',
|
|
|
|
'$b' => 'int',
|
2017-12-09 20:53:39 +01:00
|
|
|
'$c' => 'true',
|
2017-12-07 05:46:41 +01:00
|
|
|
],
|
|
|
|
],
|
2017-12-17 16:58:03 +01:00
|
|
|
'foreachLoopWithOKManipulation' => [
|
|
|
|
'<?php
|
|
|
|
$list = [1, 2, 3];
|
|
|
|
foreach ($list as $i) {
|
|
|
|
$i = 5;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'forLoopwithOKChange' => [
|
|
|
|
'<?php
|
|
|
|
$j = 5;
|
|
|
|
for ($i = $j; $i < 4; $i++) {
|
|
|
|
$j = 9;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'foreachLoopDuplicateList' => [
|
|
|
|
'<?php
|
|
|
|
$list = [1, 2, 3];
|
|
|
|
foreach ($list as $i) {
|
|
|
|
foreach ($list as $j) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-25 19:46:17 +01:00
|
|
|
'whileWithNotEmptyCheck' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var A|null */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->a = rand(0, 1) ? new A : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesA(A $a): void {}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
while ($a) {
|
|
|
|
takesA($a);
|
|
|
|
$a = $a->a;
|
|
|
|
};',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'null',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'doWhileWithNotEmptyCheck' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var A|null */
|
|
|
|
public $a;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->a = rand(0, 1) ? new A : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesA(A $a): void {}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
do {
|
|
|
|
takesA($a);
|
|
|
|
$a = $a->a;
|
|
|
|
} while ($a);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'null',
|
|
|
|
],
|
|
|
|
],
|
2018-01-29 23:18:03 +01:00
|
|
|
'doWhileWithMethodCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function getParent(): ?A {
|
|
|
|
return rand(0, 1) ? new A() : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
do {
|
|
|
|
$a = $a->getParent();
|
|
|
|
} while ($a);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'null',
|
|
|
|
],
|
|
|
|
],
|
2018-02-06 17:07:27 +01:00
|
|
|
'doWhileFirstGood' => [
|
|
|
|
'<?php
|
|
|
|
do {
|
|
|
|
$done = rand(0, 1) > 0;
|
2018-02-06 17:27:01 +01:00
|
|
|
} while (!$done);',
|
2018-02-06 17:07:27 +01:00
|
|
|
],
|
2018-02-07 00:44:53 +01:00
|
|
|
'doWhileWithIfException' => [
|
|
|
|
'<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var null|A
|
|
|
|
*/
|
|
|
|
public $parent;
|
|
|
|
|
|
|
|
public static function foo(A $a) : void
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
if ($a->parent === null) {
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = $a->parent;
|
|
|
|
} while (rand(0,1));
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'doWhileWithIfExceptionOutside' => [
|
|
|
|
'<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var null|A
|
|
|
|
*/
|
|
|
|
public $parent;
|
|
|
|
|
|
|
|
public static function foo(A $a) : void
|
|
|
|
{
|
|
|
|
if ($a->parent === null) {
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
$a = $a->parent;
|
|
|
|
} while ($a->parent && rand(0, 1));
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-02-07 21:20:47 +01:00
|
|
|
'whileInstanceOf' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var null|A */
|
|
|
|
public $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
while ($a->parent instanceof B) {
|
|
|
|
$a = $a->parent;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'whileInstanceOfAndNotEmptyCheck' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var null|A */
|
|
|
|
public $parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
$a = (new A())->parent;
|
|
|
|
|
|
|
|
$foo = rand(0, 1) ? "hello" : null;
|
|
|
|
|
|
|
|
if (!$foo) {
|
|
|
|
while ($a instanceof B && !$foo) {
|
|
|
|
$a = $a->parent;
|
|
|
|
$foo = rand(0, 1) ? "hello" : null;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-03-08 17:17:27 +01:00
|
|
|
'doWhileDefinedVar' => [
|
|
|
|
'<?php
|
|
|
|
$value = null;
|
|
|
|
do {
|
|
|
|
$count = rand(0, 1);
|
|
|
|
$value = 6;
|
|
|
|
} while ($count);',
|
|
|
|
],
|
2018-04-10 00:02:45 +02:00
|
|
|
'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"];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-04-25 03:02:07 +02:00
|
|
|
'noRedundantConditionAfterArrayAssignment' => [
|
|
|
|
'<?php
|
|
|
|
$data = ["a" => false];
|
|
|
|
while (!$data["a"]) {
|
|
|
|
if (rand() % 2 > 0) {
|
|
|
|
$data = ["a" => true];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-31 00:56:44 +02:00
|
|
|
'additionSubtractionOps' => [
|
|
|
|
'<?php
|
|
|
|
$a = 0;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a++;
|
|
|
|
} elseif ($a) {
|
|
|
|
$a--;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2018-07-02 22:44:32 +02:00
|
|
|
'invalidateBothByRefAssignments' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string &$i) : void {}
|
|
|
|
function bar(?string &$i) : void {}
|
|
|
|
|
|
|
|
$c = null;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (!$c) {
|
|
|
|
foo($c);
|
|
|
|
} else {
|
|
|
|
bar($c);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'invalidateBothByRefAssignmentsInDo' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string &$i) : void {}
|
|
|
|
function bar(?string &$i) : void {}
|
|
|
|
|
|
|
|
$c = null;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!$c) {
|
|
|
|
foo($c);
|
|
|
|
} else {
|
|
|
|
bar($c);
|
|
|
|
}
|
|
|
|
} while (rand(0, 1));',
|
|
|
|
],
|
2018-07-03 05:46:51 +02:00
|
|
|
'applyLoopConditionalAfterIf' => [
|
|
|
|
'<?php
|
|
|
|
class Obj {}
|
|
|
|
class A extends Obj {
|
|
|
|
/** @var A|null */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
class B extends Obj {}
|
|
|
|
|
|
|
|
function foo(Obj $node) : void {
|
|
|
|
while ($node instanceof A
|
|
|
|
|| $node instanceof B
|
|
|
|
) {
|
|
|
|
if (!$node instanceof B) {
|
|
|
|
$node = $node->foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-07-03 18:27:14 +02:00
|
|
|
'shouldBeFine' => [
|
|
|
|
'<?php
|
|
|
|
class Obj {}
|
|
|
|
class A extends Obj {
|
|
|
|
/** @var A|null */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
class B extends Obj {
|
|
|
|
/** @var A|null */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
class C extends Obj {
|
|
|
|
/** @var A|C|null */
|
|
|
|
public $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesA(A $a) : void {}
|
|
|
|
|
|
|
|
function foo(Obj $node) : void {
|
|
|
|
while ($node instanceof A
|
|
|
|
|| $node instanceof B
|
|
|
|
|| ($node instanceof C && $node->bar instanceof A)
|
|
|
|
) {
|
|
|
|
if (!$node instanceof C) {
|
|
|
|
$node = $node->foo;
|
|
|
|
} else {
|
|
|
|
$node = $node->bar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'doParentCall' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @return A|false */
|
|
|
|
public function getParent() {
|
|
|
|
return rand(0, 1) ? new A : false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
do {
|
|
|
|
$a = $a->getParent();
|
|
|
|
} while ($a !== false);',
|
|
|
|
],
|
2018-07-06 03:03:44 +02:00
|
|
|
'doWithContinue' => [
|
|
|
|
'<?php
|
|
|
|
do {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} while (rand(0, 1));',
|
|
|
|
],
|
2018-07-06 06:36:11 +02:00
|
|
|
'comparisonAfterContinue' => [
|
|
|
|
'<?php
|
|
|
|
$foo = null;
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$foo = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = rand(0, 1);
|
|
|
|
|
|
|
|
if ($a === $foo) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-07-12 18:08:15 +02:00
|
|
|
'noEmptyArrayAccessComplaintInsideDo' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
do {
|
|
|
|
if (isset($foo["bar"])) {}
|
|
|
|
$foo["bar"] = "bat";
|
|
|
|
} while (rand(0, 1));',
|
|
|
|
],
|
2018-09-21 01:19:00 +02:00
|
|
|
'updateExistingValueAfterLoopContinue' => [
|
|
|
|
'<?php
|
|
|
|
$i = false;
|
|
|
|
$b = (bool) rand(0, 1);
|
|
|
|
foreach ([$b] as $n) {
|
|
|
|
$i = $n;
|
|
|
|
if ($i) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($i) {}',
|
|
|
|
],
|
2018-10-23 19:00:15 +02:00
|
|
|
'preventNegativeZeroScrewingThingsUp' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : void {
|
|
|
|
$v = [1 => 0];
|
|
|
|
for ($d = 0; $d <= 10; $d++) {
|
|
|
|
for ($k = -$d; $k <= $d; $k += 2) {
|
|
|
|
if ($k === -$d || ($k !== $d && $v[$k-1] < $v[$k+1])) {
|
|
|
|
$x = $v[$k+1];
|
|
|
|
} else {
|
|
|
|
$x = $v[$k-1] + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$v[$k] = $x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-03-13 23:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-03-13 23:07:36 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
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' => [
|
|
|
|
'<?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',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'possiblyUndefinedArrayInForeach' => [
|
|
|
|
'<?php
|
|
|
|
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;',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:3 - 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
|
|
|
],
|
|
|
|
'possiblyUndefinedArrayInWhileAndForeach' => [
|
|
|
|
'<?php
|
|
|
|
for ($i = 0; $i < 4; $i++) {
|
|
|
|
while (rand(0,10) === 5) {
|
|
|
|
$array[] = "hello";
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $array;',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:4 - Possibly undefined ' .
|
2017-12-06 06:56:00 +01:00
|
|
|
'global variable $array, first seen on line 4',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'possiblyUndefinedVariableInForeach' => [
|
|
|
|
'<?php
|
|
|
|
foreach ([1, 2, 3, 4] as $b) {
|
|
|
|
$car = "Volvo";
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $car;',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:6 - Possibly undefined ' .
|
2017-12-06 06:56:00 +01:00
|
|
|
'global variable $car, first seen on line 3',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-01-18 23:16:50 +01:00
|
|
|
'possibleUndefinedVariableInForeachAndIfWithBreak' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
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;',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:9 - 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' => [
|
|
|
|
'<?php
|
|
|
|
foreach ([1,2,3,4] as $i) {
|
|
|
|
if ($i === 1) {
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
}',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:7 - 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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?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',
|
|
|
|
],
|
2017-12-07 05:46:41 +01:00
|
|
|
'whileTrueNoBreak' => [
|
|
|
|
'<?php
|
|
|
|
while (true) {
|
|
|
|
$a = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
'error_message' => 'UndefinedGlobalVariable',
|
|
|
|
],
|
|
|
|
'forInfiniteNoBreak' => [
|
|
|
|
'<?php
|
|
|
|
for (;;) {
|
|
|
|
$a = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
'error_message' => 'UndefinedGlobalVariable',
|
|
|
|
],
|
2017-12-17 16:58:03 +01:00
|
|
|
'foreachLoopInvalidation' => [
|
|
|
|
'<?php
|
|
|
|
$list = [1, 2, 3];
|
|
|
|
foreach ($list as $i) {
|
|
|
|
$list = [4, 5, 6];
|
|
|
|
}',
|
|
|
|
'error_message' => 'LoopInvalidation',
|
|
|
|
],
|
|
|
|
'forLoopInvalidation' => [
|
|
|
|
'<?php
|
|
|
|
for ($i = 0; $i < 4; $i++) {
|
|
|
|
foreach ([1, 2, 3] as $i) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'LoopInvalidation',
|
|
|
|
],
|
2018-07-02 22:44:32 +02:00
|
|
|
'invalidateByRefAssignmentWithRedundantCondition' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $i) : void {}
|
|
|
|
function bar(?string $i) : void {}
|
|
|
|
|
|
|
|
$c = null;
|
|
|
|
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (!$c) {
|
|
|
|
foo($c);
|
|
|
|
} else {
|
|
|
|
bar($c);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-03-13 23:07:36 +01:00
|
|
|
}
|
2017-02-21 22:33:20 +01:00
|
|
|
}
|