2016-04-04 01:47:06 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-12-06 20:58:18 +01:00
|
|
|
namespace Psalm\Tests\TypeReconciliation;
|
2016-04-04 01:47:06 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Tests\TestCase;
|
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
class TypeTest extends TestCase
|
2016-04-04 01:47:06 +02:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2016-06-17 00:52:12 +02:00
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
/**
|
2022-11-05 22:34:42 +01:00
|
|
|
*
|
2016-04-04 06:17:19 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2016-04-04 06:17:19 +02:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
2022-11-05 22:34:42 +01:00
|
|
|
'sealArray' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/** @var array */
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
assert(isset($a["a"]));
|
|
|
|
assert(count($a) === 1);
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-11-12 02:14:21 +01:00
|
|
|
'$a===' => 'array{a: mixed}'
|
2022-11-05 22:34:42 +01:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'sealedArrayCount' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$a = random_int(0,1) ? [] : [0, 1];
|
|
|
|
|
|
|
|
$b = null;
|
|
|
|
if (count($a) === 2) {
|
|
|
|
$b = $a;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2022-11-12 02:14:21 +01:00
|
|
|
'$a===' => 'list{0?: 0, 1?: 1}',
|
|
|
|
'$b===' => 'list{0, 1}|null'
|
2022-11-05 22:34:42 +01:00
|
|
|
]
|
|
|
|
],
|
|
|
|
'sealedArrayMagic' => [
|
|
|
|
'code' => '<?php
|
|
|
|
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{invoice?: string, utd?: "utd", cancel_agreement?: "test", installment?: "test"} */
|
2022-11-05 22:34:42 +01:00
|
|
|
$b = [];
|
|
|
|
|
|
|
|
|
|
|
|
$buttons = [];
|
|
|
|
foreach ($b as $text) {
|
|
|
|
$buttons[] = $text;
|
|
|
|
}
|
|
|
|
if (count($buttons) === 0) {
|
|
|
|
echo "Zero";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @var ?string */
|
|
|
|
$test = null;
|
|
|
|
$urls = array_filter([$test]);
|
|
|
|
|
|
|
|
$mainUrlSet = false;
|
|
|
|
foreach ($urls as $_) {
|
|
|
|
if (!$mainUrlSet) {
|
|
|
|
$mainUrlSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$mainUrlSet) {
|
|
|
|
echo "SKIP";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @param string|list<bool|array{0:string, 1:string}> $time
|
2022-11-05 22:34:42 +01:00
|
|
|
*/
|
|
|
|
function mapTime($time): void
|
|
|
|
{
|
|
|
|
$atime = is_array($time) ? $time : [];
|
|
|
|
if ($time === "24h") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($day = 0; $day < 7; ++$day) {
|
|
|
|
if (!array_key_exists($day, $atime) || !is_array($atime[$day])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dayWh = $atime[$day];
|
|
|
|
array_pop($dayWh);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
|
|
|
'$buttons===' => 'list<string>',
|
2022-11-12 02:14:21 +01:00
|
|
|
'$urls===' => 'list{0?: non-falsy-string}',
|
2022-11-05 22:34:42 +01:00
|
|
|
'$mainUrlSet===' => 'bool',
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'validSealedArrayAssertions' => [
|
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a: string, b: string, c?: string} */
|
2022-11-05 22:34:42 +01:00
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (count($a) > 2) {
|
|
|
|
echo "Have C!";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($a) < 3) {
|
|
|
|
echo "Do not have C!";
|
|
|
|
}
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'validSealedArrayAssertions2' => [
|
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a: string, b: string, c?: string} */
|
2022-11-05 22:34:42 +01:00
|
|
|
$a = [];
|
|
|
|
|
|
|
|
assert(count($a) > 2);
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-11-12 02:14:21 +01:00
|
|
|
'$a===' => 'array{a: string, b: string, c: string}',
|
2022-11-05 22:34:42 +01:00
|
|
|
]
|
|
|
|
],
|
2022-11-05 22:22:29 +01:00
|
|
|
'instanceOfInterface' => [
|
|
|
|
'code' => '<?php
|
|
|
|
interface Supplier {
|
|
|
|
public function get(): iterable;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SomeClass {
|
|
|
|
protected Supplier|iterable $prop;
|
|
|
|
|
|
|
|
public function __construct(Supplier|iterable $value) {
|
|
|
|
$this->prop = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function do(): void {
|
|
|
|
$var = $this->prop;
|
|
|
|
|
|
|
|
if ($var instanceof Supplier) {
|
|
|
|
$var->get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'nullableMethodWithTernaryGuard' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
2021-12-05 18:51:26 +01:00
|
|
|
$b = $a ? $a->fooFoo() : null;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithTernaryIfNullGuard' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$b = $a === null ? null : $a->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithTernaryEmptyGuard' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$b = empty($a) ? null : $a->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithTernaryIsNullGuard' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-04-04 20:29:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$b = is_null($a) ? null : $a->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithIfGuard' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
|
|
|
if ($a) {
|
|
|
|
$a->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithTernaryGuardWithThis' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @var A|null */
|
|
|
|
public $a;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$this->a = $a;
|
2021-12-05 18:51:26 +01:00
|
|
|
$b = $this->a ? $this->a->fooFoo() : null;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithTernaryIfNullGuardWithThis' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-06-30 00:15:51 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @var A|null */
|
|
|
|
public $a;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$this->a = $a;
|
|
|
|
$b = $this->a === null ? null : $this->a->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithIfGuardWithThis' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-07-08 00:10:01 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @var A|null */
|
|
|
|
public $a;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$this->a = $a;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($this->a) {
|
|
|
|
$this->a->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithExceptionThrown' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
|
|
|
if (!$one) {
|
|
|
|
throw new Exception();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithRedefinitionAndElse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @var int|null */
|
|
|
|
public $two;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
|
|
|
if (!$one) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one->two = 3;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithBooleanIfGuard' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
|
|
|
if ($one && $two) {
|
|
|
|
$two->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithNonNullBooleanIfGuard' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
|
|
|
if ($one !== null && $two) {
|
|
|
|
$one->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithNonNullBooleanIfGuardAndBooleanAnd' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2020-10-13 02:25:46 +02:00
|
|
|
if ($one !== null && ($two || rand(0, 1))) {
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodInConditionWithIfGuardBefore' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @var string */
|
|
|
|
public $a = "";
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
|
|
|
if ($one === null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$one->a && $one->fooFoo()) {
|
|
|
|
// do something
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithBooleanIfGuardBefore' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
|
|
|
if ($one === null || $two === null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedRedefinition' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
|
|
|
if ($one === null) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedRedefinitionInElse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
|
|
|
if ($one) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedNestedRedefinition' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
2018-05-03 19:56:30 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedSwitchRedefinition' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
2018-05-03 19:56:30 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
$one = new One();
|
|
|
|
break;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
default:
|
|
|
|
$one = new One();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedSwitchRedefinitionDueToException' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function barBar(One $one = null) {
|
2018-05-03 19:56:30 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
$one = new One();
|
|
|
|
break;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
default:
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedSwitchThatAlwaysReturns' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
2018-05-03 19:56:30 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
return;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedNestedRedefinitionWithReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
2018-05-03 19:56:30 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedNestedRedefinitionWithElseReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
2018-05-03 19:56:30 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedNestedRedefinitionWithElseifReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
2018-05-03 19:56:30 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
else if ($a === 3) {
|
|
|
|
// do nothing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedSwitchBreak' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
|
|
|
$a = 4;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
if ($one === null) {
|
|
|
|
break;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedRedefinitionOnThis' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @var One|null */
|
|
|
|
public $one;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
|
|
|
$this->one = $one;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($this->one === null) {
|
|
|
|
$this->one = new One();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$this->one->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayUnionTypeAssertion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$ids = (1 + 1 === 2) ? [] : null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($ids === null) {
|
|
|
|
$ids = [];
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2021-10-13 19:37:47 +02:00
|
|
|
'$ids' => 'array<never, never>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayUnionTypeAssertionWithIsArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$ids = (1 + 1 === 2) ? [] : null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!is_array($ids)) {
|
|
|
|
$ids = [];
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2021-10-13 19:37:47 +02:00
|
|
|
'$ids' => 'array<never, never>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'2dArrayUnionTypeAssertionWithIsArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return array<array<string>>|null */
|
|
|
|
function foo() {
|
|
|
|
$ids = rand(0, 1) ? [["hello"]] : null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (is_array($ids)) {
|
|
|
|
return $ids;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
return null;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'variableReassignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one = new One();
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one = new Two();
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
$one->barBar();',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'variableReassignmentInIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one = new One();
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (1 + 1 === 2) {
|
|
|
|
$one = new Two();
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->barBar();
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'unionTypeFlow' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Three {
|
|
|
|
/** @return void */
|
|
|
|
public function baz() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var One|Two|Three|null */
|
|
|
|
$var = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($var instanceof One) {
|
|
|
|
$var->fooFoo();
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($var instanceof Two) {
|
|
|
|
$var->barBar();
|
|
|
|
}
|
|
|
|
else if ($var) {
|
|
|
|
$var->baz();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'unionTypeFlowWithThrow' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
function a(One $var = null) {
|
|
|
|
if (!$var) {
|
|
|
|
throw new \Exception("some exception");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$var->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'unionTypeFlowWithElseif' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var One|null */
|
|
|
|
$var = null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (rand(0,100) === 5) {
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
elseif (!$var) {
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
2016-04-04 06:17:19 +02:00
|
|
|
else {
|
2017-04-25 05:45:02 +02:00
|
|
|
$var->fooFoo();
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'typedAdjustment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$var = 0;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (5 + 3 === 8) {
|
|
|
|
$var = "hello";
|
2016-04-04 06:17:19 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $var;',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$var' => 'int|string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'typeMixedAdjustment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$var = 0;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$arr = ["hello"];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (5 + 3 === 8) {
|
|
|
|
$var = $arr[0];
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
echo $var;',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$var' => 'int|string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'typeAdjustmentIfNull' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
|
|
|
class B {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$var = rand(0,10) > 5 ? new A : null;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($var === null) {
|
|
|
|
$var = new B;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$var' => 'A|B',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'whileTrue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/**
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
public function fooFoo(){
|
|
|
|
return rand(0,100) ? ["hello"] : false;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function barBar(){
|
|
|
|
while ($row = $this->fooFoo()) {
|
|
|
|
$row[0] = "bad";
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'passingParam' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a) {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = new B();
|
2017-05-27 02:05:57 +02:00
|
|
|
$b->barBar(new A);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullToNullableParam' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = new B();
|
2017-05-27 02:05:57 +02:00
|
|
|
$b->barBar(null);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'objectToNullableObjectParam' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = new B();
|
2017-05-27 02:05:57 +02:00
|
|
|
$b->barBar(new A);',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'paramCoercion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class C {
|
|
|
|
/** @return void */
|
|
|
|
function fooFoo(A $a) {
|
|
|
|
if ($a instanceof B) {
|
|
|
|
$a->barBar();
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'paramElseifCoercion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar() {}
|
|
|
|
}
|
|
|
|
class C extends A {
|
|
|
|
/** @return void */
|
|
|
|
public function baz() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class D {
|
|
|
|
/** @return void */
|
|
|
|
function fooFoo(A $a) {
|
|
|
|
if ($a instanceof B) {
|
|
|
|
$a->barBar();
|
|
|
|
}
|
|
|
|
elseif ($a instanceof C) {
|
|
|
|
$a->baz();
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'plusPlus' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = 0;
|
|
|
|
$b = $a++;',
|
|
|
|
'assertions' => [
|
2021-09-20 13:09:02 +02:00
|
|
|
'$a===' => '1',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'typedValueAssertion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @param array|string $a
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo($a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = "aadad";
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a === $b) {
|
|
|
|
echo substr($a, 1);
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'isIntOnUnaryPlus' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = +"5";
|
|
|
|
if (!is_int($a)) {
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-12-03 23:21:20 +01:00
|
|
|
'suppressOneSuppressesAll' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-03 23:21:20 +01:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
|
|
|
|
/** @return void */
|
|
|
|
public function barFoo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
|
|
|
/** @psalm-suppress PossiblyNullReference */
|
|
|
|
$a->fooFoo();
|
|
|
|
|
|
|
|
$a->barFoo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
],
|
2017-12-15 22:36:42 +01:00
|
|
|
'trueFalseTest' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-15 22:36:42 +01:00
|
|
|
class A {
|
|
|
|
/** @return true */
|
|
|
|
public function returnsTrue() { return true; }
|
|
|
|
|
|
|
|
/** @return false */
|
|
|
|
public function returnsFalse() { return false; }
|
|
|
|
|
|
|
|
/** @return bool */
|
|
|
|
public function returnsBool() {
|
|
|
|
if (rand() % 2 > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-20 07:04:15 +01:00
|
|
|
'intersectionTypeAfterInstanceof' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 01:39:27 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2018-01-20 07:04:15 +01:00
|
|
|
abstract class A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public static function getFoo(): void {
|
|
|
|
$a = new static();
|
|
|
|
if ($a instanceof I) {}
|
|
|
|
$a->foo = "bar";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface I {}',
|
|
|
|
],
|
2018-01-20 17:48:16 +01:00
|
|
|
'intersectionTypeInsideInstanceof' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 01:39:27 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2018-01-20 17:48:16 +01:00
|
|
|
abstract class A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public static function getFoo(): void {
|
|
|
|
$a = new static();
|
|
|
|
if ($a instanceof I) {
|
|
|
|
takesI($a);
|
|
|
|
takesA($a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface I {}
|
|
|
|
|
|
|
|
function takesI(I $i): void {}
|
|
|
|
function takesA(A $i): void {}',
|
|
|
|
],
|
2018-05-03 19:15:16 +02:00
|
|
|
'intersectionInNamespace' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-03 19:15:16 +02:00
|
|
|
namespace NS;
|
|
|
|
use Countable;
|
|
|
|
|
|
|
|
class Item {}
|
|
|
|
/**
|
|
|
|
* @var iterable<Item>&Countable $collection
|
|
|
|
*/
|
|
|
|
$collection = [];
|
|
|
|
count($collection);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param iterable<Item>&Countable $collection
|
|
|
|
*/
|
|
|
|
function mycount($collection): int {
|
|
|
|
return count($collection);
|
|
|
|
}
|
|
|
|
mycount($collection);',
|
|
|
|
],
|
2018-05-09 04:18:44 +02:00
|
|
|
'scalarTypeParam' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 04:18:44 +02:00
|
|
|
/**
|
|
|
|
* @param scalar $var
|
|
|
|
*/
|
|
|
|
function test($var): void {}
|
|
|
|
|
|
|
|
test("a");
|
|
|
|
test(1);
|
|
|
|
test(1.1);
|
|
|
|
test(true);
|
|
|
|
test(false);',
|
|
|
|
],
|
2019-12-29 14:36:38 +01:00
|
|
|
'assignOpUpdateArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-29 14:36:38 +01:00
|
|
|
$optgroup = ["a" => ""];
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$optgroup["a"] .= "v";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($optgroup["a"] !== "") {}'
|
|
|
|
],
|
2020-01-11 05:46:45 +01:00
|
|
|
'redefineArrayKeyInsideIsStringConditional' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-11 05:46:45 +01:00
|
|
|
/**
|
|
|
|
* @param string|int $key
|
|
|
|
*/
|
|
|
|
function get($key, array $arr) : void {
|
|
|
|
if (!isset($arr[$key])) {
|
|
|
|
if (is_string($key)) {
|
|
|
|
$key = "p" . $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($arr[$key])) {}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2022-01-24 22:35:42 +01:00
|
|
|
'redefineArrayKeyInsideIsStringConditionalElse' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @param string|int $key
|
|
|
|
*/
|
|
|
|
function get($key, array $arr) : void {
|
|
|
|
if (!isset($arr[$key])) {
|
|
|
|
if (!is_string($key)) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$key = "p" . $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($arr[$key])) {}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'redefineArrayKeyInsideIsStringConditionalElseif' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @param string|int $key
|
|
|
|
*/
|
|
|
|
function get($key, array $arr) : void {
|
|
|
|
if (!isset($arr[$key])) {
|
|
|
|
if (!is_string($key)) {
|
|
|
|
// do nothing
|
|
|
|
} elseif (rand(0, 1)) {
|
|
|
|
$key = "p" . $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($arr[$key])) {}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'redefineArrayKeyInsideIsStringConditionalWhile' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @param string|int $key
|
|
|
|
*/
|
|
|
|
function get($key, array $arr) : void {
|
|
|
|
if (!isset($arr[$key])) {
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
$key = "p" . $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($arr[$key])) {}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2020-01-11 05:46:45 +01:00
|
|
|
'redefineArrayKeyInsideIsIntConditional' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-11 05:46:45 +01:00
|
|
|
/**
|
|
|
|
* @param string|int $key
|
|
|
|
*/
|
|
|
|
function get($key, array $arr) : void {
|
|
|
|
if (!isset($arr[$key])) {
|
|
|
|
if (is_int($key)) {
|
|
|
|
$key++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($arr[$key])) {}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2020-02-22 18:34:27 +01:00
|
|
|
'arrayKeyCanBeNumeric' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-22 18:34:27 +01:00
|
|
|
/** @param array<string> $arr */
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
foreach ($arr as $k => $_) {
|
|
|
|
if (is_numeric($k)) {}
|
2020-02-22 18:53:27 +01:00
|
|
|
if (!is_numeric($k)) {}
|
2020-02-22 18:34:27 +01:00
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2020-02-23 02:25:13 +01:00
|
|
|
'narrowScalar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-23 02:25:13 +01:00
|
|
|
/** @var scalar $s */
|
|
|
|
$s = 1;
|
|
|
|
|
|
|
|
if (!is_int($s) && !is_bool($s) && !is_float($s)) {
|
|
|
|
strlen($s);
|
|
|
|
}'
|
|
|
|
],
|
2020-11-28 02:05:16 +01:00
|
|
|
'narrowWithCountToAllowNonTupleKeyedArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-28 02:05:16 +01:00
|
|
|
/**
|
|
|
|
* @param list<string> $arr
|
|
|
|
*/
|
|
|
|
function foo($arr): void {
|
|
|
|
if (count($arr) === 2) {
|
|
|
|
consume($arr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @param array{0:string, 1: string} $input
|
2020-11-28 02:05:16 +01:00
|
|
|
*/
|
|
|
|
function consume($input): void{}'
|
|
|
|
],
|
2021-07-26 09:50:12 +02:00
|
|
|
'notDateTimeWithDateTimeInterface' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-07-26 14:56:15 +02:00
|
|
|
function foo(DateTimeInterface $dateTime): DateTimeInterface {
|
2021-07-26 09:50:12 +02:00
|
|
|
$dateInterval = new DateInterval("P1D");
|
|
|
|
|
2021-07-26 14:56:15 +02:00
|
|
|
if ($dateTime instanceof DateTime) {
|
2021-07-26 09:50:12 +02:00
|
|
|
$dateTime->add($dateInterval);
|
|
|
|
|
|
|
|
return $dateTime;
|
|
|
|
} else {
|
|
|
|
return $dateTime->add($dateInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
],
|
|
|
|
'notDateTimeImmutableWithDateTimeInterface' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-07-26 14:56:15 +02:00
|
|
|
function foo(DateTimeInterface $dateTime): DateTimeInterface {
|
2021-07-26 09:50:12 +02:00
|
|
|
$dateInterval = new DateInterval("P1D");
|
|
|
|
|
2021-07-26 14:56:15 +02:00
|
|
|
if ($dateTime instanceof DateTimeImmutable) {
|
2021-07-26 09:50:12 +02:00
|
|
|
return $dateTime->add($dateInterval);
|
|
|
|
} else {
|
|
|
|
$dateTime->add($dateInterval);
|
|
|
|
|
|
|
|
return $dateTime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
',
|
|
|
|
],
|
2021-09-04 23:14:50 +02:00
|
|
|
'CountEqual0MakesNonEmptyArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-09-04 23:14:50 +02:00
|
|
|
function a(array $a): void {
|
|
|
|
if (count($a) === 0) {
|
|
|
|
throw new \LogicException;
|
|
|
|
}
|
|
|
|
expectNonEmptyArray($a);
|
|
|
|
}
|
|
|
|
function b(array $a): void {
|
|
|
|
if (count($a) !== 0) {
|
|
|
|
expectNonEmptyArray($a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function c(array $a): void {
|
|
|
|
if (count($a) === 0) {
|
|
|
|
throw new \LogicException;
|
|
|
|
} else {
|
|
|
|
expectNonEmptyArray($a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/** @param non-empty-array $a */
|
|
|
|
function expectNonEmptyArray(array $a): array { return $a; }'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
2018-01-11 05:29:18 +01:00
|
|
|
'possiblyUndefinedVariable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 05:29:18 +01:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;',
|
|
|
|
'error_message' => 'PossiblyUndefinedGlobalVariable',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'nullableMethodCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$a->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodCallWithThis' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @var A|null */
|
|
|
|
protected $a;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {
|
|
|
|
$this->a = $a;
|
|
|
|
$this->a->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithIfGuard' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
|
|
|
if ($one) {
|
|
|
|
$two->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithWrongBooleanIfGuard' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
|
|
|
if ($one || $two) {
|
|
|
|
$two->fooFoo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithWrongIfGuardedBefore' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
|
|
|
if ($two === null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithWrongBooleanIfGuardBefore' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
|
|
|
if ($one === null && $two === null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2019-03-01 21:55:20 +01:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedNestedIncompleteRedefinition' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null, Two $two = null) {
|
2018-05-03 19:56:30 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedSwitchRedefinitionNoDefault' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
2018-05-13 00:46:47 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
$one = new One();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedSwitchRedefinitionEmptyDefault' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
2018-05-13 00:46:47 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
switch ($a) {
|
|
|
|
case 4:
|
|
|
|
$one = new One();
|
|
|
|
break;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullableMethodWithGuardedNestedRedefinitionWithUselessElseReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(One $one = null) {
|
2018-05-03 19:56:30 +02:00
|
|
|
$a = rand(0, 4);
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($one === null) {
|
|
|
|
if ($a === 4) {
|
|
|
|
$one = new One();
|
|
|
|
}
|
|
|
|
else if ($a === 3) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$one = new One();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->fooFoo();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullReference',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'variableReassignmentInIfWithOutsideCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class One {
|
|
|
|
/** @return void */
|
|
|
|
public function fooFoo() {}
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class Two {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one = new One();
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (1 + 1 === 2) {
|
|
|
|
$one = new Two();
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->barBar();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$one->barBar();',
|
2017-11-16 03:04:25 +01:00
|
|
|
'error_message' => 'PossiblyUndefinedMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'wrongParam' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a) {}
|
2016-04-04 22:33:26 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = new B();
|
|
|
|
$b->barBar(5);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'intToNullableObjectParam' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
/** @return void */
|
|
|
|
public function barBar(A $a = null) {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = new B();
|
|
|
|
$b->barBar(5);',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'paramCoercionWithBadArg' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {
|
|
|
|
/** @return void */
|
|
|
|
public function blab() {}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class C {
|
|
|
|
/** @return void */
|
|
|
|
function fooFoo(A $a) {
|
|
|
|
if ($a instanceof B) {
|
|
|
|
$a->barBar();
|
|
|
|
}
|
2016-04-27 00:18:05 +02:00
|
|
|
}
|
2017-04-25 05:45:02 +02:00
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullCheckInsideForeachWithNoLeaveStatement' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
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
|
|
|
$a->fooBar();',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'NullReference',
|
|
|
|
],
|
2017-11-11 20:53:35 +01:00
|
|
|
'possiblyUndefinedMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-11 20:53:35 +01:00
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {}
|
2017-11-11 20:53:35 +01:00
|
|
|
}
|
|
|
|
class B {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function other(): void {}
|
2017-11-11 20:53:35 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function a(bool $cond): void {
|
2017-11-11 20:53:35 +01:00
|
|
|
if ($cond) {
|
|
|
|
$a = new A();
|
|
|
|
} else {
|
|
|
|
$a = new B();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cond) {
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyUndefinedMethod',
|
|
|
|
],
|
2017-12-15 22:36:42 +01:00
|
|
|
'notTrueTest' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-15 22:36:42 +01:00
|
|
|
/** @return true */
|
|
|
|
function returnsTrue() { return rand() % 2 > 0; }
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
|
|
|
'notFalseTest' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-15 22:36:42 +01:00
|
|
|
/** @return false */
|
|
|
|
function returnsFalse() { return rand() % 2 > 0; }
|
|
|
|
',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
2022-11-05 22:34:42 +01:00
|
|
|
'invalidSealedArrayAssertion1' => [
|
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a: string, b: string, c?: string} */
|
2022-11-05 22:34:42 +01:00
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (count($a) > 1) {
|
|
|
|
}',
|
|
|
|
'error_message' => 'RedundantConditionGivenDocblockType'
|
|
|
|
],
|
|
|
|
'invalidSealedArrayAssertion2' => [
|
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a: string, b: string, c?: string} */
|
2022-11-05 22:34:42 +01:00
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (count($a) > 3) {
|
|
|
|
}',
|
|
|
|
'error_message' => 'DocblockTypeContradiction'
|
|
|
|
],
|
|
|
|
'invalidSealedArrayAssertion3' => [
|
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a: string, b: string, c?: string} */
|
2022-11-05 22:34:42 +01:00
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (count($a) > 4) {
|
|
|
|
}',
|
|
|
|
'error_message' => 'DocblockTypeContradiction'
|
|
|
|
],
|
|
|
|
'invalidSealedArrayAssertion4' => [
|
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a: string, b: string, c?: string} */
|
2022-11-05 22:34:42 +01:00
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (count($a) < 1) {
|
|
|
|
}',
|
2022-12-04 18:05:36 +01:00
|
|
|
'error_message' => 'DocblockTypeContradiction'
|
2022-11-05 22:34:42 +01:00
|
|
|
],
|
|
|
|
'invalidSealedArrayAssertion5' => [
|
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a: string, b: string, c?: string} */
|
2022-11-05 22:34:42 +01:00
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (count($a) < 2) {
|
|
|
|
}',
|
|
|
|
'error_message' => 'DocblockTypeContradiction'
|
|
|
|
],
|
|
|
|
'invalidSealedArrayAssertion6' => [
|
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a: string, b: string, c?: string} */
|
2022-11-05 22:34:42 +01:00
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (count($a) < 4) {
|
|
|
|
}',
|
|
|
|
'error_message' => 'RedundantConditionGivenDocblockType'
|
|
|
|
],
|
2018-01-20 17:48:16 +01:00
|
|
|
'intersectionTypeClassCheckAfterInstanceof' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 01:39:27 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2018-01-20 17:48:16 +01:00
|
|
|
abstract class A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public static function getFoo(): void {
|
|
|
|
$a = new static();
|
|
|
|
if ($a instanceof B) {}
|
|
|
|
elseif ($a instanceof C) {}
|
|
|
|
else {}
|
|
|
|
takesB($a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
class C extends A {}
|
|
|
|
|
|
|
|
function takesB(B $i): void {}',
|
2020-08-06 01:39:27 +02:00
|
|
|
'error_message' => 'ArgumentTypeCoercion - src' . DIRECTORY_SEPARATOR . 'somefile.php:14:32 - Argument 1 of takesB expects B,'
|
2022-08-03 19:56:38 +02:00
|
|
|
. ' but parent type A&static provided',
|
2018-01-20 17:48:16 +01:00
|
|
|
],
|
|
|
|
'intersectionTypeInterfaceCheckAfterInstanceof' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-06 01:39:27 +02:00
|
|
|
/**
|
|
|
|
* @psalm-consistent-constructor
|
|
|
|
*/
|
2018-01-20 17:48:16 +01:00
|
|
|
abstract class A {
|
|
|
|
/** @var string|null */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
public static function getFoo(): void {
|
|
|
|
$a = new static();
|
|
|
|
if ($a instanceof I) {}
|
|
|
|
takesI($a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface I {}
|
|
|
|
|
|
|
|
function takesI(I $i): void {}',
|
2022-08-03 19:56:38 +02:00
|
|
|
'error_message' => 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:32 - Argument 1 of takesI expects I, but A&static provided',
|
2018-01-20 17:48:16 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-26 04:02:19 +01:00
|
|
|
}
|
2016-04-04 01:47:06 +02:00
|
|
|
}
|