1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-05 21:19:03 +01:00
psalm/tests/Loop/WhileTest.php

735 lines
24 KiB
PHP
Raw Normal View History

<?php
2023-10-19 13:12:06 +02:00
declare(strict_types=1);
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;
2021-12-03 20:11:20 +01:00
class WhileTest extends TestCase
{
2021-12-04 21:55:53 +01:00
use InvalidCodeAnalysisTestTrait;
use ValidCodeAnalysisTestTrait;
public function providerValidCodeParse(): iterable
{
return [
2022-12-18 18:19:22 +01:00
'whileTrue' => [
'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
],
'whileVar' => [
'code' => '<?php
$worked = false;
while (rand(0,100) === 10) {
$worked = true;
}',
'assertions' => [
'$worked' => 'bool',
],
],
'objectValueWithTwoTypes' => [
'code' => '<?php
class B {}
class A {
/** @var A|B */
public $parent;
public function __construct() {
$this->parent = rand(0, 1) ? new A() : new B();
}
}
function makeA(): A {
return new A();
}
$a = makeA();
while ($a instanceof A) {
$a = $a->parent;
}',
'assertions' => [
'$a' => 'B',
],
],
'objectValueWithInstanceofProperty' => [
'code' => '<?php
class B {}
class A {
/** @var A|B */
public $parent;
public function __construct() {
$this->parent = rand(0, 1) ? new A() : new B();
}
}
function makeA(): A {
return new A();
}
$a = makeA();
while ($a->parent instanceof A) {
$a = $a->parent;
}
$b = $a->parent;',
'assertions' => [
'$a' => 'A',
'$b' => 'A|B',
],
],
'objectValueNullable' => [
'code' => '<?php
class A {
/** @var ?A */
public $parent;
public function __construct() {
$this->parent = rand(0, 1) ? new A() : null;
}
}
function makeA(): A {
return new A();
}
$a = makeA();
while ($a) {
$a = $a->parent;
}',
'assertions' => [
'$a' => 'null',
],
],
'objectValueWithAnd' => [
'code' => '<?php
class A {
/** @var ?A */
public $parent;
public function __construct() {
$this->parent = rand(0, 1) ? new A() : null;
}
}
function makeA(): A {
return new A();
}
$a = makeA();
while ($a && rand(0, 10) > 5) {
$a = $a->parent;
}',
'assertions' => [
'$a' => 'A|null',
],
],
'loopWithNoParadox' => [
'code' => '<?php
$a = ["b", "c", "d"];
array_pop($a);
while ($a) {
$letter = array_pop($a);
if (!$a) {}
}',
],
'noRedundantConditionInWhileAssignment' => [
'code' => '<?php
class A {
/** @var ?int */
public $bar;
}
function foo(): ?A {
return rand(0, 1) ? new A : null;
}
while ($a = foo()) {
if ($a->bar) {}
}',
],
'whileTrueWithBreak' => [
'code' => '<?php
while (true) {
$a = "hello";
break;
}
while (1) {
$b = 5;
break;
}',
'assertions' => [
'$a' => 'string',
'$b' => 'int',
],
],
'whileWithNotEmptyCheck' => [
'code' => '<?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',
],
],
'whileInstanceOf' => [
'code' => '<?php
class A {
/** @var null|A */
public $parent;
}
class B extends A {}
$a = new A();
while ($a->parent instanceof B) {
$a = $a->parent;
}',
],
'whileInstanceOfAndNotEmptyCheck' => [
'code' => '<?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;
}
}',
],
'noRedundantConditionAfterArrayAssignment' => [
'code' => '<?php
$data = ["a" => false];
while (!$data["a"]) {
if (rand() % 2 > 0) {
$data = ["a" => true];
}
}',
],
'additionSubtractionAssignment' => [
'code' => '<?php
$a = 0;
while (rand(0, 1)) {
if (rand(0, 1)) {
$a = $a + 1;
} elseif ($a) {
$a = $a - 1;
}
2022-12-18 17:15:15 +01:00
}',
],
'additionSubtractionInc' => [
'code' => '<?php
$a = 0;
while (rand(0, 1)) {
if (rand(0, 1)) {
$a++;
} elseif ($a) {
$a--;
}
2019-03-23 19:27:54 +01:00
}',
],
'invalidateBothByRefAssignments' => [
'code' => '<?php
function foo(?string &$i) : void {}
function bar(?string &$i) : void {}
$c = null;
while (rand(0, 1)) {
if (!$c) {
foo($c);
} else {
bar($c);
}
}',
],
'applyLoopConditionalAfterIf' => [
'code' => '<?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;
}
}
}',
],
'shouldBeFine' => [
'code' => '<?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;
}
}
}',
],
'comparisonAfterContinue' => [
'code' => '<?php
$foo = null;
while (rand(0, 1)) {
if (rand(0, 1)) {
$foo = 1;
continue;
}
$a = rand(0, 1);
if ($a === $foo) {}
}',
],
2018-11-10 20:06:31 +01:00
'noRedundantConditionAfterWhile' => [
'code' => '<?php
2018-11-10 20:06:31 +01:00
$i = 5;
while (--$i > 0) {}
echo $i === 0;',
],
'noRedundantConditionOnAddedSubtractedInLoop' => [
'code' => '<?php
$depth = 0;
$position = 0;
while (!$depth) {
if (rand(0, 1)) {
$depth++;
} elseif (rand(0, 1)) {
$depth--;
}
$position++;
2022-12-18 17:15:15 +01:00
}',
],
'variableDefinedInWhileConditional' => [
'code' => '<?php
function foo() : void {
$pointers = ["hi"];
2021-09-16 18:55:33 +02:00
while (rand(0, 1) && -1 < ($parent = 0)) {
print $pointers[$parent];
}
2022-12-18 17:15:15 +01:00
}',
],
2020-04-12 07:26:11 +02:00
'assingnedConditionallyReassignedToMixedInLoop' => [
'code' => '<?php
2020-04-12 07:26:11 +02:00
function foo(array $arr): void {
while (rand(0, 1)) {
$t = true;
if (!empty($arr[0])) {
/** @psalm-suppress MixedAssignment */
$t = $arr[0];
}
if ($t === true) {}
}
}',
],
'varChangedAfterUseInsideLoop' => [
'code' => '<?php
2020-04-12 07:26:11 +02:00
function takesString(string $s) : void {}
/**
* @param array<string> $fields
*/
function changeVarAfterUse(array $values, array $fields): void {
foreach ($fields as $field) {
if (!isset($values[$field])) {
continue;
}
/** @psalm-suppress MixedAssignment */
$value = $values[$field];
/** @psalm-suppress MixedArgument */
takesString($value);
$values[$field] = null;
}
}',
],
'invalidateWhileAssertion' => [
'code' => '<?php
function test(array $x, int $i) : void {
while (isset($x[$i]) && is_array($x[$i])) {
$i++;
}
2022-12-18 17:15:15 +01:00
}',
],
'possiblyUndefinedInWhile' => [
'code' => '<?php
function getRenderersForClass(string $a): void {
while ($b = getString($b ?? $a)) {
$c = "hello";
}
}
function getString(string $s) : ?string {
return rand(0, 1) ? $s : null;
2022-12-18 17:15:15 +01:00
}',
],
2020-05-11 15:34:07 +02:00
'thornyLoop' => [
'code' => '<?php
2020-05-11 15:34:07 +02:00
function searchCode(string $content, array &$tmp) : void {
// separer les balises du texte
$tmp = [];
$reg = \'/(<[^>]+>)|([^<]+)+/isU\';
// pour chaque element trouve :
$str = "";
$offset = 0;
while (preg_match($reg, $content, $parse, PREG_OFFSET_CAPTURE, $offset)) {
2020-05-11 15:39:25 +02:00
$str .= "hello";
2020-05-11 15:34:07 +02:00
unset($parse);
}
2022-12-18 17:15:15 +01:00
}',
2020-05-11 15:34:07 +02:00
],
'assignToTKeyedArrayListPreserveListness' => [
'code' => '<?php
/**
* @return non-empty-list<string>
*/
function foo(string $key): array {
$elements = [$key];
while (rand(0, 1)) {
$elements[] = $key;
}
return $elements;
}',
],
'reconcilePositiveInt' => [
'code' => '<?php
$counter = 0;
while (rand(0, 1)) {
if ($counter > 0) {
$counter = $counter - 1;
} else {
$counter = $counter + 1;
}
2022-12-18 17:15:15 +01:00
}',
],
'nonEmptyListIterationChangeVarWithContinue' => [
'code' => '<?php
/** @param non-empty-list<int> $arr */
function foo(array $arr) : void {
while (array_shift($arr)) {
if ($arr && $arr[0] === "a") {}
if (rand(0, 1)) {
$arr = array_merge($arr, ["a"]);
continue;
}
echo "here";
}
2022-12-18 17:15:15 +01:00
}',
],
'nonEmptyListIterationChangeVarWithoutContinue' => [
'code' => '<?php
/** @param non-empty-list<int> $arr */
function foo(array $arr) : void {
while (array_shift($arr)) {
if ($arr && $arr[0] === "a") {}
if (rand(0, 1)) {
$arr = array_merge($arr, ["a"]);
}
echo "here";
}
2022-12-18 17:15:15 +01:00
}',
],
'ifNestedInsideLoop' => [
'code' => '<?php
function analyse(): int {
$state = 1;
while (rand(0, 1)) {
if ($state === 3) {
echo "here";
} elseif ($state === 2) {
if (rand(0, 1)) {
$state = 3;
}
} else {
$state = 2;
}
}
return $state;
2022-12-18 17:15:15 +01:00
}',
],
'ifNotNestedInsideLoop' => [
'code' => '<?php
function analyse(): int {
$state = 1;
while (rand(0, 1)) {
if ($state === 3) {
echo "here";
} elseif ($state === 2) {
$state = 3;
} else {
$state = 2;
}
}
return $state;
2022-12-18 17:15:15 +01:00
}',
],
'continueShouldAddToContext' => [
'code' => '<?php
function foo() : void {
$link = null;
while (rand(0, 1)) {
if (rand(0, 1)) {
$link = "a";
continue;
}
if (rand(0, 1)) {
if ($link === null) {
return;
}
continue;
}
}
2022-12-18 17:15:15 +01:00
}',
],
'continue2Returns' => [
'code' => '<?php
function foo(): array {
while (rand(0, 1)) {
while (rand(0, 1)) {
if (rand(0, 1)) {
continue 2;
}
return [];
}
}
return [];
2022-12-18 17:15:15 +01:00
}',
],
'propertyTypeUpdatedInBranch' => [
'code' => '<?php
class A
{
public ?int $foo = null;
public function setFoo(): void
{
$this->foo = 5;
}
}
function bar(A $a): void {
$a->foo = null;
while (rand(0, 1)) {
if (rand(0, 1)) {
$a->setFoo();
} elseif ($a->foo !== null) {}
}
2022-12-18 17:15:15 +01:00
}',
],
'propertyTypeUpdatedInBranchWithBreak' => [
'code' => '<?php
class A
{
public ?int $foo = null;
public function setFoo(): void
{
$this->foo = 5;
}
}
function bar(A $a): void {
$a->foo = null;
while (rand(0, 1)) {
if (rand(0, 1)) {
$a->setFoo();
} elseif ($a->foo !== null) {
break;
}
}
if ($a->foo !== null) {}
2022-12-18 17:15:15 +01:00
}',
],
'whileTrueDontHaveExitPathForReturn' => [
'code' => '<?php
function getResultWithRetry(): string
{
while (new stdClass) {
return "";
}
2022-12-18 17:15:15 +01:00
}',
],
'ComplexWhileTrueDontHaveExitPathForReturn' => [
'code' => '<?php
class Test {
private int $retryAttempts = 10;
private function getResult(): string
{
// return tring or throw exception whatever
throw new Exception();
}
private function getResultWithRetry(): string
{
$attempt = 1;
while (true) {
try {
return $this->getResult();
} catch (Throwable $exception) {
if ($attempt >= $this->retryAttempts) {
throw $exception;
}
$attempt++;
continue;
}
}
}
2022-12-18 17:15:15 +01:00
}',
],
'continuingEducation' => [
'code' => '<?php
function breakUpPathIntoParts(): void {
$b = false;
while (rand(0, 1)) {
if ($b) {
echo "hello";
continue;
}
$b = true;
}
2022-12-18 17:15:15 +01:00
}',
2022-12-04 13:37:29 +01:00
],
'breakInWhileTrueIsNotInfiniteLoop' => [
'code' => '<?php
/** @return Generator<array-key, mixed> */
function f()
{
if (rand(0,1)) {
throw new Exception;
}
while (true) {
yield 1;
break;
}
2022-12-18 17:15:15 +01:00
}',
],
];
}
public function providerInvalidCodeParse(): iterable
{
return [
'whileTrueNoBreak' => [
'code' => '<?php
while (true) {
$a = "hello";
}
echo $a;',
'error_message' => 'UndefinedGlobalVariable',
],
'invalidateByRefAssignmentWithRedundantCondition' => [
'code' => '<?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',
],
];
}
}