1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
psalm/tests/Loop/DoTest.php

361 lines
11 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\Loop;
use Psalm\Tests\Traits;
use const DIRECTORY_SEPARATOR;
class DoTest extends \Psalm\Tests\TestCase
{
2018-11-06 03:57:36 +01:00
use Traits\InvalidCodeAnalysisTestTrait;
use Traits\ValidCodeAnalysisTestTrait;
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
*/
2018-11-06 03:57:36 +01:00
public function providerValidCodeParse()
{
return [
'doWhileVar' => [
'<?php
$worked = false;
do {
$worked = true;
}
while (rand(0,100) === 10);',
'assertions' => [
2018-11-10 20:06:31 +01:00
'$worked' => 'true',
],
],
'doWhileVarWithPossibleBreak' => [
'<?php
$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' => [
'<?php
$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' => [
'<?php
$a = false;
do {
if (rand(0, 1)) {
if (rand(0, 1)) {
$a = true;
}
break;
}
$a = true;
}
while (rand(0,1));',
'assertions' => [
'$a' => 'bool',
],
],
'doWhileVarWithPossibleInitialisingBreakNoInitialDefinition' => [
'<?php
do {
if (rand(0, 1)) {
$worked = true;
break;
}
$worked = true;
}
while (rand(0,100) === 10);',
'assertions' => [
'$worked' => 'true',
],
],
'doWhileUndefinedVar' => [
'<?php
do {
2018-11-10 20:06:31 +01:00
$result = (bool) rand(0,1);
} while (!$result);',
2018-11-10 20:06:31 +01:00
'assertions' => [
'$result' => 'true',
],
],
'doWhileVarAndBreak' => [
'<?php
/** @return void */
function foo(string $b) {}
do {
if (null === ($a = rand(0, 1) ? "hello" : null)) {
break;
}
foo($a);
}
while (rand(0,100) === 10);',
],
'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',
],
],
'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',
],
],
'doWhileFirstGood' => [
'<?php
do {
$done = rand(0, 1) > 0;
} while (!$done);',
],
'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));
}
}',
],
'doWhileDefinedVar' => [
'<?php
$value = null;
do {
$count = rand(0, 1);
$value = 6;
} while ($count);',
],
2018-11-10 20:06:31 +01:00
'doWhileDefinedVarWithPossibleBreak' => [
'<?php
$value = null;
do {
if (rand(0, 1)) {
break;
}
$count = rand(0, 1);
$value = 6;
} while ($count);',
],
'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));',
],
'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);',
],
2019-01-07 07:15:30 +01:00
'doCallInWhile' => [
'<?php
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
],
'doWithContinue' => [
'<?php
do {
if (rand(0, 1)) {
continue;
}
} while (rand(0, 1));',
],
'noEmptyArrayAccessComplaintInsideDo' => [
'<?php
$foo = [];
do {
if (isset($foo["bar"])) {}
$foo["bar"] = "bat";
} while (rand(0, 1));',
],
2018-11-10 20:06:31 +01:00
'noRedundantConditionAfterDoWhile' => [
'<?php
$i = 5;
do {} while (--$i > 0);
echo $i === 0;',
],
];
}
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
2018-11-10 20:06:31 +01:00
*/
2018-11-06 03:57:36 +01:00
public function providerInvalidCodeParse()
2018-11-10 20:06:31 +01:00
{
return [
'doWhileVarWithPossibleBreakWithoutDefining' => [
'<?php
do {
if (rand(0, 1)) {
break;
}
$worked = true;
}
while (rand(0,1));
echo $worked;',
'error_message' => 'PossiblyUndefinedGlobalVariable',
],
'doWhileVarWithPossibleBreakThatMaybeSetsToTrueWithoutDefining' => [
'<?php
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' => [
'<?php
do {
if (rand(0, 1)) {
continue;
}
$worked = true;
}
while (rand(0,1));
echo $worked;',
'error_message' => 'PossiblyUndefinedGlobalVariable',
],
'possiblyUndefinedArrayInDo' => [
'<?php
do {
$array[] = "hello";
} while (rand(0, 1));
echo $array;',
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',
],
];
}
}