2018-11-10 01:54:10 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2018-11-10 01:54:10 +01:00
|
|
|
namespace Psalm\Tests\Loop;
|
|
|
|
|
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 DoTest extends TestCase
|
2018-11-10 01:54:10 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2018-11-10 01:54:10 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2018-11-10 01:54:10 +01:00
|
|
|
{
|
|
|
|
return [
|
2022-12-18 18:19:22 +01:00
|
|
|
'doWhileTrue' => [
|
|
|
|
'code' => '<?php
|
|
|
|
function ret(): int {
|
|
|
|
do {
|
|
|
|
return 1;
|
|
|
|
} while (true);
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2022-12-18 18:19:22 +01:00
|
|
|
],
|
2018-11-10 01:54:10 +01:00
|
|
|
'doWhileVar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
$worked = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
$worked = true;
|
|
|
|
}
|
|
|
|
while (rand(0,100) === 10);',
|
|
|
|
'assertions' => [
|
2018-11-10 20:06:31 +01:00
|
|
|
'$worked' => 'true',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'doWhileVarWithPossibleBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 20:06:31 +01:00
|
|
|
$a = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
while (rand(0,100) === 10);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'SKIPPED-doWhileVarWithPossibleBreakThatSetsToTrue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 20:06:31 +01:00
|
|
|
$a = false;
|
|
|
|
$b = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
$b = true;
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
while (rand(0,1));',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'true',
|
|
|
|
'$b' => 'true',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'doWhileVarWithPossibleBreakThatMaybeSetsToTrue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 20:06:31 +01:00
|
|
|
$a = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
while (rand(0,1));',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'doWhileVarWithPossibleInitialisingBreakNoInitialDefinition' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 20:06:31 +01:00
|
|
|
do {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$worked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$worked = true;
|
|
|
|
}
|
|
|
|
while (rand(0,100) === 10);',
|
|
|
|
'assertions' => [
|
|
|
|
'$worked' => 'true',
|
2018-11-10 01:54:10 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'doWhileUndefinedVar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
do {
|
2018-11-10 20:06:31 +01:00
|
|
|
$result = (bool) rand(0,1);
|
2018-11-10 01:54:10 +01:00
|
|
|
} while (!$result);',
|
2018-11-10 20:06:31 +01:00
|
|
|
'assertions' => [
|
|
|
|
'$result' => 'true',
|
|
|
|
],
|
2018-11-10 01:54:10 +01:00
|
|
|
],
|
|
|
|
'doWhileVarAndBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
/** @return void */
|
|
|
|
function foo(string $b) {}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (null === ($a = rand(0, 1) ? "hello" : null)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-29 00:30:47 +01:00
|
|
|
/** @psalm-suppress MixedArgument */
|
2018-11-10 01:54:10 +01:00
|
|
|
foo($a);
|
|
|
|
}
|
|
|
|
while (rand(0,100) === 10);',
|
|
|
|
],
|
|
|
|
'doWhileWithNotEmptyCheck' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
class A {
|
2018-11-10 22:10:59 +01:00
|
|
|
/** @var A|null */
|
|
|
|
public $a;
|
2018-11-10 01:54:10 +01:00
|
|
|
|
2018-11-10 22:10:59 +01:00
|
|
|
public function __construct() {
|
|
|
|
$this->a = rand(0, 1) ? new A : null;
|
|
|
|
}
|
2018-11-10 01:54:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function takesA(A $a): void {}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
do {
|
2018-11-10 22:10:59 +01:00
|
|
|
takesA($a);
|
|
|
|
$a = $a->a;
|
2018-11-10 01:54:10 +01:00
|
|
|
} while ($a);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'null',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'doWhileWithMethodCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
class A {
|
2018-11-10 22:10:59 +01:00
|
|
|
public function getParent(): ?A {
|
|
|
|
return rand(0, 1) ? new A() : null;
|
|
|
|
}
|
2018-11-10 01:54:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
|
|
|
do {
|
2018-11-10 22:10:59 +01:00
|
|
|
$a = $a->getParent();
|
2018-11-10 01:54:10 +01:00
|
|
|
} while ($a);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'null',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'doWhileFirstGood' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
do {
|
|
|
|
$done = rand(0, 1) > 0;
|
|
|
|
} while (!$done);',
|
|
|
|
],
|
|
|
|
'doWhileWithIfException' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'doWhileDefinedVar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
$value = null;
|
|
|
|
do {
|
|
|
|
$count = rand(0, 1);
|
|
|
|
$value = 6;
|
|
|
|
} while ($count);',
|
|
|
|
],
|
2018-11-10 20:06:31 +01:00
|
|
|
'doWhileDefinedVarWithPossibleBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 20:06:31 +01:00
|
|
|
$value = null;
|
|
|
|
do {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$count = rand(0, 1);
|
|
|
|
$value = 6;
|
|
|
|
} while ($count);',
|
|
|
|
],
|
2018-11-10 01:54:10 +01:00
|
|
|
'invalidateBothByRefAssignmentsInDo' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
function foo(?string &$i) : void {}
|
|
|
|
function bar(?string &$i) : void {}
|
|
|
|
|
|
|
|
$c = null;
|
|
|
|
|
|
|
|
do {
|
2024-01-12 23:37:45 +01:00
|
|
|
if ($c === null || $c === "" || $c === "0") {
|
2018-11-10 01:54:10 +01:00
|
|
|
foo($c);
|
|
|
|
} else {
|
|
|
|
bar($c);
|
|
|
|
}
|
|
|
|
} while (rand(0, 1));',
|
|
|
|
],
|
|
|
|
'doParentCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
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);',
|
|
|
|
],
|
2019-01-07 07:15:30 +01:00
|
|
|
'doCallInWhile' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-07 07:15:30 +01:00
|
|
|
class A {
|
|
|
|
public function getParent() : ?A {
|
|
|
|
return rand(0, 1) ? new A : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$i = 0;
|
|
|
|
do {
|
|
|
|
$i++;
|
2019-03-23 19:27:54 +01:00
|
|
|
} while ($a = $a->getParent());',
|
2019-01-07 07:15:30 +01:00
|
|
|
],
|
2018-11-10 01:54:10 +01:00
|
|
|
'doWithContinue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
do {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} while (rand(0, 1));',
|
|
|
|
],
|
|
|
|
'noEmptyArrayAccessComplaintInsideDo' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 01:54:10 +01:00
|
|
|
$foo = [];
|
|
|
|
do {
|
|
|
|
if (isset($foo["bar"])) {}
|
|
|
|
$foo["bar"] = "bat";
|
|
|
|
} while (rand(0, 1));',
|
|
|
|
],
|
2018-11-10 20:06:31 +01:00
|
|
|
'noRedundantConditionAfterDoWhile' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 20:06:31 +01:00
|
|
|
$i = 5;
|
|
|
|
do {} while (--$i > 0);
|
|
|
|
echo $i === 0;',
|
|
|
|
],
|
2019-09-29 22:12:52 +02:00
|
|
|
'doWhileNonInfinite' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-29 22:12:52 +02:00
|
|
|
function foo(): int {
|
|
|
|
do {
|
|
|
|
$value = mt_rand(0, 10);
|
|
|
|
if ($value > 5) continue;
|
|
|
|
break;
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}',
|
|
|
|
],
|
2020-05-16 22:32:26 +02:00
|
|
|
'doNoRedundant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-05-16 22:32:26 +02:00
|
|
|
class Event {}
|
|
|
|
|
|
|
|
function fetchEvent(): ?Event {
|
|
|
|
return rand(0, 1) ? new Event() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextEvent(bool $c): void {
|
|
|
|
do {
|
|
|
|
$e = fetchEvent();
|
|
|
|
} while ($c && $e);
|
|
|
|
}',
|
|
|
|
],
|
2020-07-02 19:59:59 +02:00
|
|
|
'doConditionInWhileAndIfWithSingleVar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-02 19:59:59 +02:00
|
|
|
$b = !!rand(0, 1);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!$b) {
|
|
|
|
$b = !rand(0, 1);
|
|
|
|
}
|
|
|
|
} while (!$b);',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2022-12-18 17:15:15 +01:00
|
|
|
'$b' => 'true',
|
|
|
|
],
|
2020-07-02 19:59:59 +02:00
|
|
|
],
|
|
|
|
'doConditionInWhileAndIfWithTwoVars' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-02 19:59:59 +02:00
|
|
|
$b = !!rand(0, 1);
|
|
|
|
|
|
|
|
do {
|
|
|
|
$s = rand(0, 1);
|
|
|
|
if (!$b && $s) {}
|
|
|
|
} while (!$b && $s);
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
if ($b) {}',
|
2020-07-02 19:59:59 +02:00
|
|
|
],
|
2021-01-11 18:46:02 +01:00
|
|
|
'regularAssignmentInsideDo' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-01-11 18:46:02 +01:00
|
|
|
do {
|
|
|
|
$code = rand(0, 1);
|
|
|
|
echo "here";
|
2022-12-18 17:15:15 +01:00
|
|
|
} while ($code === 1);',
|
2021-01-11 18:46:02 +01:00
|
|
|
],
|
|
|
|
'destructuringAssignmentInsideDo' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-01-11 18:46:02 +01:00
|
|
|
do {
|
|
|
|
[$code] = [rand(0, 1)];
|
|
|
|
echo "here";
|
2022-12-18 17:15:15 +01:00
|
|
|
} while ($code === 1);',
|
2021-01-11 18:46:02 +01:00
|
|
|
],
|
2018-11-10 20:06:31 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2018-11-10 20:06:31 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'doWhileVarWithPossibleBreakWithoutDefining' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 20:06:31 +01:00
|
|
|
do {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$worked = true;
|
|
|
|
}
|
|
|
|
while (rand(0,1));
|
|
|
|
|
|
|
|
echo $worked;',
|
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable',
|
|
|
|
],
|
|
|
|
'doWhileVarWithPossibleBreakThatMaybeSetsToTrueWithoutDefining' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 20:06:31 +01:00
|
|
|
do {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
while (rand(0,1));
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable',
|
|
|
|
],
|
|
|
|
'SKIPPED-doWhileVarWithPossibleContinueWithoutDefining' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 20:06:31 +01:00
|
|
|
do {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$worked = true;
|
|
|
|
}
|
|
|
|
while (rand(0,1));
|
|
|
|
|
|
|
|
echo $worked;',
|
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable',
|
|
|
|
],
|
|
|
|
'possiblyUndefinedArrayInDo' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-10 20:06:31 +01:00
|
|
|
do {
|
|
|
|
$array[] = "hello";
|
2020-07-02 19:59:59 +02:00
|
|
|
} while (rand(0, 1));',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:25 - Possibly undefined ' .
|
2018-11-10 20:06:31 +01:00
|
|
|
'global variable $array, first seen on line 3',
|
|
|
|
],
|
2018-11-10 01:54:10 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|