1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 02:47:02 +01:00
psalm/tests/TypeReconciliation/TypeTest.php

1548 lines
52 KiB
PHP
Raw Normal View History

2016-04-04 01:47:06 +02:00
<?php
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;
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-04-04 06:17:19 +02:00
/**
2022-01-13 20:38:17 +01:00
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>}>
2016-04-04 06:17:19 +02:00
*/
public function providerValidCodeParse(): iterable
2016-04-04 06:17:19 +02:00
{
return [
'nullableMethodWithTernaryGuard' => [
'code' => '<?php
class A {
/** @return void */
public function fooFoo() {}
2016-04-04 06:17:19 +02:00
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(A $a = null) {
$b = $a ? $a->fooFoo() : null;
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithTernaryIfNullGuard' => [
'code' => '<?php
class A {
/** @return void */
public function fooFoo() {}
2016-04-04 06:17:19 +02:00
}
2017-06-29 16:22:49 +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
}',
],
'nullableMethodWithTernaryEmptyGuard' => [
'code' => '<?php
class A {
/** @return void */
public function fooFoo() {}
2016-04-04 06:17:19 +02:00
}
2017-06-29 16:22:49 +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
}',
],
'nullableMethodWithTernaryIsNullGuard' => [
'code' => '<?php
class A {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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
}',
],
'nullableMethodWithIfGuard' => [
'code' => '<?php
class A {
/** @return void */
public function fooFoo() {}
2016-06-30 00:15:51 +02:00
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(A $a = null) {
if ($a) {
$a->fooFoo();
}
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithTernaryGuardWithThis' => [
'code' => '<?php
class A {
/** @return void */
public function fooFoo() {}
2016-06-30 00:15:51 +02:00
}
2017-06-29 16:22:49 +02:00
class B {
/** @var A|null */
public $a;
2017-06-29 16:22:49 +02:00
/** @return void */
public function barBar(A $a = null) {
$this->a = $a;
$b = $this->a ? $this->a->fooFoo() : null;
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithTernaryIfNullGuardWithThis' => [
'code' => '<?php
class A {
/** @return void */
public function fooFoo() {}
2016-06-30 00:15:51 +02:00
}
2017-06-29 16:22:49 +02:00
class B {
/** @var A|null */
public $a;
2017-06-29 16:22:49 +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
}',
],
'nullableMethodWithIfGuardWithThis' => [
'code' => '<?php
class A {
/** @return void */
public function fooFoo() {}
2016-07-08 00:10:01 +02:00
}
2017-06-29 16:22:49 +02:00
class B {
/** @var A|null */
public $a;
2017-06-29 16:22:49 +02:00
/** @return void */
public function barBar(A $a = null) {
$this->a = $a;
2017-06-29 16:22:49 +02:00
if ($this->a) {
$this->a->fooFoo();
}
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithExceptionThrown' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
2016-04-04 06:17:19 +02:00
}
2017-06-29 16:22:49 +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
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithRedefinitionAndElse' => [
'code' => '<?php
class One {
/** @var int|null */
public $two;
2017-06-29 16:22:49 +02:00
/** @return void */
public function fooFoo() {}
2016-04-04 06:17:19 +02:00
}
2017-06-29 16:22:49 +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
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithBooleanIfGuard' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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
}',
],
'nullableMethodWithNonNullBooleanIfGuard' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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
}',
],
'nullableMethodWithNonNullBooleanIfGuardAndBooleanAnd' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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))) {
$one->fooFoo();
}
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodInConditionWithIfGuardBefore' => [
'code' => '<?php
class One {
/** @var string */
public $a = "";
2017-06-29 16:22:49 +02:00
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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
if (!$one->a && $one->fooFoo()) {
// do something
}
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithBooleanIfGuardBefore' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedRedefinition' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedRedefinitionInElse' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedNestedRedefinition' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +02:00
if ($one === null) {
if ($a === 4) {
$one = new One();
}
else {
$one = new One();
}
}
2017-06-29 16:22:49 +02:00
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedSwitchRedefinition' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +02:00
if ($one === null) {
switch ($a) {
case 4:
$one = new One();
break;
2017-06-29 16:22:49 +02:00
default:
$one = new One();
break;
}
}
2017-06-29 16:22:49 +02:00
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedSwitchRedefinitionDueToException' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/**
* @return void
*/
public function barBar(One $one = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +02:00
if ($one === null) {
switch ($a) {
case 4:
$one = new One();
break;
2017-06-29 16:22:49 +02:00
default:
throw new \Exception("bad");
}
}
2017-06-29 16:22:49 +02:00
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedSwitchThatAlwaysReturns' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +02:00
if ($one === null) {
switch ($a) {
case 4:
return;
2017-06-29 16:22:49 +02:00
default:
return;
}
}
2017-06-29 16:22:49 +02:00
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedNestedRedefinitionWithReturn' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +02:00
if ($one === null) {
if ($a === 4) {
$one = new One();
return;
}
else {
$one = new One();
}
}
2017-06-29 16:22:49 +02:00
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedNestedRedefinitionWithElseReturn' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +02:00
if ($one === null) {
if ($a === 4) {
$one = new One();
}
else {
$one = new One();
return;
}
}
2017-06-29 16:22:49 +02:00
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedNestedRedefinitionWithElseifReturn' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +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
$one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedSwitchBreak' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null) {
$a = 4;
2017-06-29 16:22:49 +02:00
switch ($a) {
case 4:
if ($one === null) {
break;
}
2017-06-29 16:22:49 +02:00
$one->fooFoo();
break;
}
}
2017-05-27 02:05:57 +02:00
}',
],
'nullableMethodWithGuardedRedefinitionOnThis' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @var One|null */
public $one;
2017-06-29 16:22:49 +02:00
/** @return void */
public function barBar(One $one = null) {
$this->one = $one;
2017-06-29 16:22:49 +02:00
if ($this->one === null) {
$this->one = new One();
}
2017-06-29 16:22:49 +02:00
$this->one->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'arrayUnionTypeAssertion' => [
'code' => '<?php
$ids = (1 + 1 === 2) ? [] : null;
2017-06-29 16:22:49 +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
],
],
'arrayUnionTypeAssertionWithIsArray' => [
'code' => '<?php
$ids = (1 + 1 === 2) ? [] : null;
2017-06-29 16:22:49 +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
],
],
'2dArrayUnionTypeAssertionWithIsArray' => [
'code' => '<?php
/** @return array<array<string>>|null */
function foo() {
$ids = rand(0, 1) ? [["hello"]] : null;
2017-06-29 16:22:49 +02:00
if (is_array($ids)) {
return $ids;
}
2017-06-29 16:22:49 +02:00
return null;
2017-05-27 02:05:57 +02:00
}',
],
'variableReassignment' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function barBar() {}
}
2017-06-29 16:22:49 +02:00
$one = new One();
2017-06-29 16:22:49 +02:00
$one = new Two();
2017-06-29 16:22:49 +02:00
2017-05-27 02:05:57 +02:00
$one->barBar();',
],
'variableReassignmentInIf' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function barBar() {}
}
2017-06-29 16:22:49 +02:00
$one = new One();
2017-06-29 16:22:49 +02:00
if (1 + 1 === 2) {
$one = new Two();
2017-06-29 16:22:49 +02:00
$one->barBar();
2017-05-27 02:05:57 +02:00
}',
],
'unionTypeFlow' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function barBar() {}
}
2017-06-29 16:22:49 +02:00
class Three {
/** @return void */
public function baz() {}
}
2017-06-29 16:22:49 +02:00
/** @var One|Two|Three|null */
$var = null;
2017-06-29 16:22:49 +02:00
if ($var instanceof One) {
$var->fooFoo();
2016-04-04 22:33:26 +02:00
}
else {
if ($var instanceof Two) {
$var->barBar();
}
else if ($var) {
$var->baz();
}
2017-05-27 02:05:57 +02:00
}',
],
'unionTypeFlowWithThrow' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
2016-04-04 22:33:26 +02:00
}
2017-06-29 16:22:49 +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
}',
],
'unionTypeFlowWithElseif' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
/** @var One|null */
$var = null;
2017-06-29 16:22:49 +02:00
if (rand(0,100) === 5) {
2017-06-29 16:22:49 +02:00
2016-04-04 06:17:19 +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 {
$var->fooFoo();
2017-05-27 02:05:57 +02:00
}',
],
'typedAdjustment' => [
'code' => '<?php
$var = 0;
2017-06-29 16:22:49 +02:00
if (5 + 3 === 8) {
$var = "hello";
2016-04-04 06:17:19 +02:00
}
2017-06-29 16:22:49 +02:00
echo $var;',
'assertions' => [
'$var' => 'int|string',
2017-05-27 02:05:57 +02:00
],
],
'typeMixedAdjustment' => [
'code' => '<?php
$var = 0;
2017-06-29 16:22:49 +02:00
$arr = ["hello"];
2017-06-29 16:22:49 +02:00
if (5 + 3 === 8) {
$var = $arr[0];
}
2017-06-29 16:22:49 +02:00
echo $var;',
'assertions' => [
'$var' => 'int|string',
2017-05-27 02:05:57 +02:00
],
],
'typeAdjustmentIfNull' => [
'code' => '<?php
class A {}
class B {}
2017-06-29 16:22:49 +02:00
$var = rand(0,10) > 5 ? new A : null;
2017-06-29 16:22:49 +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
],
],
'whileTrue' => [
'code' => '<?php
class One {
/**
* @return array|false
*/
public function fooFoo(){
return rand(0,100) ? ["hello"] : false;
}
2017-06-29 16:22:49 +02:00
/** @return void */
public function barBar(){
while ($row = $this->fooFoo()) {
$row[0] = "bad";
}
}
2017-05-27 02:05:57 +02:00
}',
],
'passingParam' => [
'code' => '<?php
class A {}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(A $a) {}
}
2017-06-29 16:22:49 +02:00
$b = new B();
2017-05-27 02:05:57 +02:00
$b->barBar(new A);',
],
'nullToNullableParam' => [
'code' => '<?php
class A {}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(A $a = null) {}
}
2017-06-29 16:22:49 +02:00
$b = new B();
2017-05-27 02:05:57 +02:00
$b->barBar(null);',
],
'objectToNullableObjectParam' => [
'code' => '<?php
class A {}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(A $a = null) {}
}
2017-06-29 16:22:49 +02:00
$b = new B();
2017-05-27 02:05:57 +02:00
$b->barBar(new A);',
],
'paramCoercion' => [
'code' => '<?php
class A {}
class B extends A {
/** @return void */
public function barBar() {}
}
2017-06-29 16:22:49 +02:00
class C {
/** @return void */
function fooFoo(A $a) {
if ($a instanceof B) {
$a->barBar();
}
}
2017-05-27 02:05:57 +02:00
}',
],
'paramElseifCoercion' => [
'code' => '<?php
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
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
}',
],
'plusPlus' => [
'code' => '<?php
$a = 0;
$b = $a++;',
'assertions' => [
2021-09-20 13:09:02 +02:00
'$a===' => '1',
2017-05-27 02:05:57 +02:00
],
],
'typedValueAssertion' => [
'code' => '<?php
/**
* @param array|string $a
*/
2018-01-11 21:50:45 +01:00
function fooFoo($a): void {
$b = "aadad";
2017-06-29 16:22:49 +02:00
if ($a === $b) {
echo substr($a, 1);
}
2017-05-27 02:05:57 +02:00
}',
],
'isIntOnUnaryPlus' => [
'code' => '<?php
$a = +"5";
if (!is_int($a)) {
2017-05-27 02:05:57 +02:00
}',
],
'suppressOneSuppressesAll' => [
'code' => '<?php
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' => [],
],
'trueFalseTest' => [
'code' => '<?php
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;
}
}',
],
'intersectionTypeAfterInstanceof' => [
'code' => '<?php
/**
* @psalm-consistent-constructor
*/
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' => [
'code' => '<?php
/**
* @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' => [
'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);',
],
'scalarTypeParam' => [
'code' => '<?php
/**
* @param scalar $var
*/
function test($var): void {}
test("a");
test(1);
test(1.1);
test(true);
test(false);',
],
'assignOpUpdateArray' => [
'code' => '<?php
$optgroup = ["a" => ""];
if (rand(0, 1)) {
$optgroup["a"] .= "v";
}
if ($optgroup["a"] !== "") {}'
],
'redefineArrayKeyInsideIsStringConditional' => [
'code' => '<?php
/**
* @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])) {}
}
}'
],
'redefineArrayKeyInsideIsIntConditional' => [
'code' => '<?php
/**
* @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' => [
'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)) {}
if (!is_numeric($k)) {}
2020-02-22 18:34:27 +01:00
}
}'
],
'narrowScalar' => [
'code' => '<?php
/** @var scalar $s */
$s = 1;
if (!is_int($s) && !is_bool($s) && !is_float($s)) {
strlen($s);
}'
],
'narrowWithCountToAllowNonTupleKeyedArray' => [
'code' => '<?php
/**
* @param list<string> $arr
*/
function foo($arr): void {
if (count($arr) === 2) {
consume($arr);
}
}
/**
* @param array{0:string, 1: string} $input
*/
function consume($input): void{}'
],
2021-07-26 09:50:12 +02:00
'notDateTimeWithDateTimeInterface' => [
'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' => [
'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;
}
}
',
],
'CountEqual0MakesNonEmptyArray' => [
'code' => '<?php
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; }'
],
];
}
/**
2022-01-13 20:38:17 +01:00
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
*/
public function providerInvalidCodeParse(): iterable
{
return [
'possiblyUndefinedVariable' => [
'code' => '<?php
if (rand(0, 1)) {
$a = 5;
}
echo $a;',
'error_message' => 'PossiblyUndefinedGlobalVariable',
],
'nullableMethodCall' => [
'code' => '<?php
class A {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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',
],
'nullableMethodCallWithThis' => [
'code' => '<?php
class A {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @var A|null */
protected $a;
2017-06-29 16:22:49 +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',
],
'nullableMethodWithIfGuard' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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',
],
'nullableMethodWithWrongBooleanIfGuard' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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',
],
'nullableMethodWithWrongIfGuardedBefore' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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
$one->fooFoo();
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'PossiblyNullReference',
],
'nullableMethodWithWrongBooleanIfGuardBefore' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +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
$one->fooFoo();
}
}',
2019-03-01 21:55:20 +01:00
'error_message' => 'PossiblyNullReference',
],
'nullableMethodWithGuardedNestedIncompleteRedefinition' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null, Two $two = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +02:00
if ($one === null) {
if ($a === 4) {
$one = new One();
}
}
2017-06-29 16:22:49 +02:00
$one->fooFoo();
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'PossiblyNullReference',
],
'nullableMethodWithGuardedSwitchRedefinitionNoDefault' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +02:00
if ($one === null) {
switch ($a) {
case 4:
$one = new One();
break;
}
}
2017-06-29 16:22:49 +02:00
$one->fooFoo();
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'PossiblyNullReference',
],
'nullableMethodWithGuardedSwitchRedefinitionEmptyDefault' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +02:00
if ($one === null) {
switch ($a) {
case 4:
$one = new One();
break;
2017-06-29 16:22:49 +02:00
default:
break;
}
}
2017-06-29 16:22:49 +02:00
$one->fooFoo();
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'PossiblyNullReference',
],
'nullableMethodWithGuardedNestedRedefinitionWithUselessElseReturn' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(One $one = null) {
$a = rand(0, 4);
2017-06-29 16:22:49 +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
$one->fooFoo();
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'PossiblyNullReference',
],
'variableReassignmentInIfWithOutsideCall' => [
'code' => '<?php
class One {
/** @return void */
public function fooFoo() {}
2016-04-04 22:33:26 +02:00
}
2017-06-29 16:22:49 +02:00
class Two {
/** @return void */
public function barBar() {}
}
2017-06-29 16:22:49 +02:00
$one = new One();
2017-06-29 16:22:49 +02:00
if (1 + 1 === 2) {
$one = new Two();
2017-06-29 16:22:49 +02:00
$one->barBar();
}
2017-06-29 16:22:49 +02:00
$one->barBar();',
'error_message' => 'PossiblyUndefinedMethod',
],
'wrongParam' => [
'code' => '<?php
class A {}
2017-06-29 16:22:49 +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
$b = new B();
$b->barBar(5);',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidArgument',
],
'intToNullableObjectParam' => [
'code' => '<?php
class A {}
2017-06-29 16:22:49 +02:00
class B {
/** @return void */
public function barBar(A $a = null) {}
}
2017-06-29 16:22:49 +02:00
$b = new B();
$b->barBar(5);',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidArgument',
],
'paramCoercionWithBadArg' => [
'code' => '<?php
class A {}
class B extends A {
/** @return void */
public function blab() {}
}
2017-06-29 16:22:49 +02:00
class C {
/** @return void */
function fooFoo(A $a) {
if ($a instanceof B) {
$a->barBar();
}
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'UndefinedMethod',
],
'nullCheckInsideForeachWithNoLeaveStatement' => [
'code' => '<?php
$a = null;
2017-06-29 16:22:49 +02:00
$a->fooBar();',
2017-05-27 02:05:57 +02:00
'error_message' => 'NullReference',
],
'possiblyUndefinedMethod' => [
'code' => '<?php
class A {
2018-01-11 21:50:45 +01:00
public function foo(): void {}
}
class B {
2018-01-11 21:50:45 +01:00
public function other(): void {}
}
2018-01-11 21:50:45 +01:00
function a(bool $cond): void {
if ($cond) {
$a = new A();
} else {
$a = new B();
}
if ($cond) {
$a->foo();
}
}',
'error_message' => 'PossiblyUndefinedMethod',
],
'notTrueTest' => [
'code' => '<?php
/** @return true */
function returnsTrue() { return rand() % 2 > 0; }
',
'error_message' => 'InvalidReturnStatement',
],
'notFalseTest' => [
'code' => '<?php
/** @return false */
function returnsFalse() { return rand() % 2 > 0; }
',
'error_message' => 'InvalidReturnStatement',
],
2018-01-20 17:48:16 +01:00
'intersectionTypeClassCheckAfterInstanceof' => [
'code' => '<?php
/**
* @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 {}',
'error_message' => 'ArgumentTypeCoercion - src' . DIRECTORY_SEPARATOR . 'somefile.php:14:32 - Argument 1 of takesB expects B,'
2020-03-25 14:18:49 +01:00
. ' parent type A&static provided',
2018-01-20 17:48:16 +01:00
],
'intersectionTypeInterfaceCheckAfterInstanceof' => [
'code' => '<?php
/**
* @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 {}',
'error_message' => 'InvalidArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:12:32 - Argument 1 of takesI expects I, A&static provided',
2018-01-20 17:48:16 +01:00
],
];
2017-01-26 04:02:19 +01:00
}
2016-04-04 01:47:06 +02:00
}