1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00
psalm/tests/TypeTest.php

1335 lines
43 KiB
PHP
Raw Normal View History

2016-04-04 01:47:06 +02:00
<?php
2016-07-26 00:37:44 +02:00
namespace Psalm\Tests;
2016-04-04 01:47:06 +02:00
class TypeTest extends TestCase
2016-04-04 01:47:06 +02:00
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
2016-04-04 06:17:19 +02:00
/**
* @return array
2016-04-04 06:17:19 +02:00
*/
public function providerFileCheckerValidCodeParse()
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) {
2018-01-11 21:50:45 +01:00
$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;
2018-01-11 21:50:45 +01:00
$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) {
if ($one !== null && ($two || 1 + 1 === 3)) {
$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 = 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 = 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 = 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 = 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 = 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 = 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 = 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' => [
2017-06-29 16:22:49 +02:00
'$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' => [
2017-06-29 16:22:49 +02:00
'$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' => [
2017-06-29 16:22:49 +02:00
'$a' => 'int',
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
}',
],
'issetWithSimpleAssignment' => [
'<?php
$array = [];
2017-06-29 16:22:49 +02:00
if (isset($array[$a = 5])) {
print "hello";
}
2017-06-29 16:22:49 +02:00
2017-05-27 02:05:57 +02:00
print $a;',
'assertions' => [],
'error_levels' => ['EmptyArrayAccess'],
],
'issetWithMultipleAssignments' => [
'<?php
if (rand(0, 4) > 2) {
$arr = [5 => [3 => "hello"]];
}
2017-06-29 16:22:49 +02:00
if (isset($arr[$a = 5][$b = 3])) {
2017-06-29 16:22:49 +02:00
}
2017-06-29 16:22:49 +02:00
echo $a;
2017-05-27 02:05:57 +02:00
echo $b;',
'assertions' => [],
'error_levels' => ['MixedArrayAccess'],
],
'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
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 {}',
],
];
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
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();
}
}',
2017-05-27 02:05:57 +02:00
'error_mesage' => '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 = 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 = 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 = 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 = 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',
],
];
2017-01-26 04:02:19 +01:00
}
2016-04-04 01:47:06 +02:00
}