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

192 lines
5.6 KiB
PHP

<?php
namespace Psalm\Tests\Loop;
use Psalm\Tests\Traits;
class DoTest extends \Psalm\Tests\TestCase
{
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @return array
*/
public function providerFileCheckerValidCodeParse()
{
return [
'doWhileVar' => [
'<?php
$worked = false;
do {
$worked = true;
}
while (rand(0,100) === 10);',
'assertions' => [
'$worked' => 'bool',
],
],
'doWhileUndefinedVar' => [
'<?php
do {
$result = rand(0,1);
} while (!$result);',
],
'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);',
],
'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);',
],
'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));',
],
];
}
}