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

1548 lines
51 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
/**
2019-03-01 21:55:20 +01:00
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
2016-04-04 06:17:19 +02:00
*/
public function providerValidCodeParse(): iterable
2016-04-04 06:17:19 +02:00
{
return [
'nullableMethodWithTernaryGuard' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?php
$ids = (1 + 1 === 2) ? [] : null;
2017-06-29 16:22:49 +02:00
if ($ids === null) {
$ids = [];
}',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$ids' => 'array<empty, empty>',
2017-05-27 02:05:57 +02:00
],
],
'arrayUnionTypeAssertionWithIsArray' => [
'<?php
$ids = (1 + 1 === 2) ? [] : null;
2017-06-29 16:22:49 +02:00
if (!is_array($ids)) {
$ids = [];
}',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$ids' => 'array<empty, empty>',
2017-05-27 02:05:57 +02:00
],
],
'2dArrayUnionTypeAssertionWithIsArray' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?php
$a = +"5";
if (!is_int($a)) {
2017-05-27 02:05:57 +02:00
}',
],
'suppressOneSuppressesAll' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?php
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' => [
'<?php
/**
* @param scalar $var
*/
function test($var): void {}
test("a");
test(1);
test(1.1);
test(true);
test(false);',
],
'assignOpUpdateArray' => [
'<?php
$optgroup = ["a" => ""];
if (rand(0, 1)) {
$optgroup["a"] .= "v";
}
if ($optgroup["a"] !== "") {}'
],
'redefineArrayKeyInsideIsStringConditional' => [
'<?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' => [
'<?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' => [
'<?php
/** @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' => [
'<?php
/** @var scalar $s */
$s = 1;
if (!is_int($s) && !is_bool($s) && !is_float($s)) {
strlen($s);
}'
],
'narrowWithCountToAllowNonTupleKeyedArray' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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; }'
],
];
}
/**
* @return iterable<string,array{string,error_message:string,1?:string[],2?:bool,3?:string}>
*/
public function providerInvalidCodeParse(): iterable
{
return [
'possiblyUndefinedVariable' => [
'<?php
if (rand(0, 1)) {
$a = 5;
}
echo $a;',
'error_message' => 'PossiblyUndefinedGlobalVariable',
],
'nullableMethodCall' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?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' => [
'<?php
/** @return true */
function returnsTrue() { return rand() % 2 > 0; }
',
'error_message' => 'InvalidReturnStatement',
],
'notFalseTest' => [
'<?php
/** @return false */
function returnsFalse() { return rand() % 2 > 0; }
',
'error_message' => 'InvalidReturnStatement',
],
2018-01-20 17:48:16 +01:00
'intersectionTypeClassCheckAfterInstanceof' => [
'<?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' => [
'<?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
}