2017-03-16 16:46:07 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2019-12-07 19:38:06 +01:00
|
|
|
namespace Psalm\Tests\TypeReconciliation;
|
2017-03-16 16:46:07 +01:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Tests\TestCase;
|
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
|
|
|
class TypeAlgebraTest extends TestCase
|
2017-03-16 16:46:07 +01:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2017-03-16 16:46:07 +01:00
|
|
|
|
|
|
|
/**
|
2022-01-13 20:38:17 +01:00
|
|
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>}>
|
2017-03-16 16:46:07 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2017-03-16 16:46:07 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
2019-12-06 20:47:05 +01:00
|
|
|
'twoVarLogicSimple' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesString(string $s): void {}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a !== null || $b !== null) {
|
|
|
|
if ($a !== null) {
|
|
|
|
$c = $a;
|
|
|
|
} else {
|
|
|
|
$c = $b;
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
takesString($c);
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'threeVarLogic' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesString(string $s): void {}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b, ?string $c): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a !== null || $b !== null || $c !== null) {
|
|
|
|
if ($a !== null) {
|
|
|
|
$d = $a;
|
|
|
|
} elseif ($b !== null) {
|
|
|
|
$d = $b;
|
|
|
|
} else {
|
|
|
|
$d = $c;
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
takesString($d);
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2019-12-06 20:47:05 +01:00
|
|
|
'twoVarLogicNotNestedSimple' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a && !$b) return "bad";
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'twoVarLogicNotNestedWithAllPathsReturning' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a && !$b) {
|
|
|
|
return "bad";
|
|
|
|
} else {
|
|
|
|
if (!$a) {
|
|
|
|
return $b;
|
|
|
|
} else {
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'twoVarLogicNotNestedWithAssignmentBeforeReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a && !$b) {
|
|
|
|
$a = 5;
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a) {
|
|
|
|
$a = 7;
|
|
|
|
return $b;
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invertedTwoVarLogicNotNested' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a || $b) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invertedTwoVarLogicNotNestedWithAssignmentBeforeReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a || $b) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$a = 5;
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-11-28 06:46:41 +01:00
|
|
|
'twoVarLogicNotNestedWithElseifAndNoNegations' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($b) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-05-07 07:26:06 +02:00
|
|
|
'threeVarLogicNotNestedWithNoRedefinitionsWithClasses' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-07 07:26:06 +02:00
|
|
|
function foo(?stdClass $a, ?stdClass $b, ?stdClass $c): stdClass {
|
|
|
|
if ($a) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($b) {
|
|
|
|
// do nothing here
|
|
|
|
} elseif ($c) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return new stdClass;
|
|
|
|
}
|
|
|
|
|
2019-12-07 19:38:06 +01:00
|
|
|
if (!$a && !$b) {
|
|
|
|
return $c;
|
|
|
|
}
|
2018-05-07 07:26:06 +02:00
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'threeVarLogicNotNestedWithNoRedefinitionsWithStrings' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b, ?string $c): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($b) {
|
|
|
|
// do nothing here
|
|
|
|
} elseif ($c) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a && !$b) return $c;
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-11-28 06:46:41 +01:00
|
|
|
'threeVarLogicNotNestedAndOrWithNoRedefinitions' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b, ?string $c): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($b || $c) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a && !$b) return $c;
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'twoVarLogicNotNestedWithElseifCorrectlyNegatedInElseIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-13 21:51:43 +01:00
|
|
|
function foo(string $a, string $b): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a) {
|
|
|
|
// do nothing here
|
|
|
|
} elseif ($b) {
|
2019-12-13 23:52:37 +01:00
|
|
|
$a = null;
|
2017-04-25 05:45:02 +02:00
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nestedReassignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a === null) {
|
|
|
|
$a = "blah-blah";
|
|
|
|
} else {
|
|
|
|
$a = rand(0, 1) ? "blah" : null;
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a === null) {
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'twoVarLogicNotNestedWithElseifCorrectlyReinforcedInIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?A $a, ?A $b): A {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a) {
|
|
|
|
$a = new B;
|
|
|
|
} elseif ($b) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
return new A;
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'differentValueChecks' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(string $a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a === "foo") {
|
|
|
|
// do something
|
|
|
|
} elseif ($a === "bar") {
|
|
|
|
// can never get here
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'byRefAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
preg_match("/hello/", "hello molly", $matches);
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$matches) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
preg_match("/hello/", "hello dolly", $matches);
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$matches) {
|
2017-08-08 00:38:38 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'orConditionalAfterAndConditional' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(string $a, string $b): void {
|
2017-08-08 00:38:38 +02:00
|
|
|
if ($a && $b) {
|
|
|
|
echo "a";
|
|
|
|
} elseif ($a || $b) {
|
|
|
|
echo "b";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'issetOnOneStringAfterAnother' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-08-08 00:38:38 +02:00
|
|
|
/** @param string[] $arr */
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(array $arr): void {
|
2017-08-08 00:38:38 +02:00
|
|
|
$a = "a";
|
|
|
|
|
|
|
|
if (!isset($arr[$a])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ([0, 1, 2, 3] as $i) {
|
|
|
|
if (!isset($arr[$a . $i])) {
|
|
|
|
echo "a";
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = "hello";
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2018-05-07 22:22:04 +02:00
|
|
|
'issetArrayCreation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-07 20:34:25 +02:00
|
|
|
$arr = [];
|
|
|
|
|
|
|
|
foreach ([0, 1, 2, 3] as $i) {
|
|
|
|
$a = rand(0, 1) ? 5 : "010";
|
|
|
|
|
|
|
|
if (!isset($arr[(int) $a])) {
|
|
|
|
$arr[(int) $a] = 5;
|
|
|
|
} else {
|
|
|
|
$arr[(int) $a] += 4;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-07 23:15:27 +02:00
|
|
|
'moreConvolutedArrayCreation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-07 23:15:27 +02:00
|
|
|
function fetchRow() : array {
|
|
|
|
return ["c" => "UK"];
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr = [];
|
|
|
|
|
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
$row = fetchRow();
|
|
|
|
|
|
|
|
if (!isset($arr[$row["c"]])) {
|
|
|
|
$arr[$row["c"]] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr[$row["c"]] = 1;
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedArrayOffset'],
|
2018-05-07 23:15:27 +02:00
|
|
|
],
|
|
|
|
'moreConvolutedNestedArrayCreation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-07 23:15:27 +02:00
|
|
|
function fetchRow() : array {
|
|
|
|
return ["c" => "UK"];
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr = [];
|
|
|
|
|
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
$row = fetchRow();
|
|
|
|
|
|
|
|
if (!isset($arr[$row["c"]]["foo"])) {
|
|
|
|
$arr[$row["c"]]["foo"] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr[$row["c"]]["foo"] = 1;
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedArrayOffset'],
|
2018-05-07 23:15:27 +02:00
|
|
|
],
|
2017-08-19 04:41:25 +02:00
|
|
|
'noParadoxInLoop' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function paradox2(): void {
|
2017-08-19 04:41:25 +02:00
|
|
|
$condition = rand() % 2 > 0;
|
|
|
|
|
|
|
|
if (!$condition) {
|
|
|
|
foreach ([1, 2] as $value) {
|
|
|
|
if ($condition) { }
|
|
|
|
$condition = true;
|
|
|
|
}
|
|
|
|
}
|
2017-08-19 05:14:38 +02:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'noParadoxInListAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(string $a): void {
|
2017-08-19 05:14:38 +02:00
|
|
|
if (!$a) {
|
|
|
|
list($a) = explode(":", "a:b");
|
|
|
|
|
|
|
|
if ($a) { }
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-10-11 04:36:33 +02:00
|
|
|
'noParadoxAfterAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function get_bool(): bool {
|
2017-10-11 04:36:33 +02:00
|
|
|
return rand() % 2 > 0;
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function leftover(): bool {
|
2017-10-11 04:36:33 +02:00
|
|
|
$res = get_bool();
|
|
|
|
if ($res === false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$res = get_bool();
|
|
|
|
if ($res === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2017-10-12 20:02:06 +02:00
|
|
|
}',
|
2017-10-11 05:01:52 +02:00
|
|
|
],
|
|
|
|
'noParadoxAfterArrayAppending' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-10-11 05:01:52 +02:00
|
|
|
/** @return array|false */
|
|
|
|
function array_append(array $errors) {
|
|
|
|
if ($errors) {
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
if (rand() % 2 > 0) {
|
|
|
|
$errors[] = "unlucky";
|
|
|
|
}
|
|
|
|
if ($errors) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $errors;
|
2017-10-12 20:02:06 +02:00
|
|
|
}',
|
2017-10-11 05:01:52 +02:00
|
|
|
],
|
2017-10-11 05:09:19 +02:00
|
|
|
'noParadoxInCatch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function maybe_returns_array(): ?array {
|
2017-10-11 05:09:19 +02:00
|
|
|
if (rand() % 2 > 0) {
|
|
|
|
return ["key" => "value"];
|
|
|
|
}
|
|
|
|
if (rand() % 3 > 0) {
|
|
|
|
throw new Exception("An exception occurred");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function try_catch_check(): array {
|
2017-10-11 05:09:19 +02:00
|
|
|
$arr = null;
|
|
|
|
try {
|
|
|
|
$arr = maybe_returns_array();
|
|
|
|
if (!$arr) { return []; }
|
|
|
|
} catch (Exception $e) {
|
|
|
|
if (!$arr) { return []; }
|
|
|
|
}
|
|
|
|
return $arr;
|
2017-10-12 20:02:06 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-10-23 01:11:28 +02:00
|
|
|
'lotsaTruthyStatements' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-10-23 01:11:28 +02:00
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var ?string
|
|
|
|
*/
|
|
|
|
public $a = null;
|
|
|
|
/**
|
|
|
|
* @var ?string
|
|
|
|
*/
|
|
|
|
public $b = null;
|
|
|
|
}
|
|
|
|
function f(A $obj): string {
|
|
|
|
if (($obj->a !== null) == true) {
|
|
|
|
return $obj->a; // definitely not null
|
|
|
|
} elseif (!is_null($obj->b) == true) {
|
|
|
|
return $obj->b;
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException("$obj->a or $obj->b must be set");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'lotsaFalsyStatements' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-10-23 01:11:28 +02:00
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @var ?string
|
|
|
|
*/
|
|
|
|
public $a = null;
|
|
|
|
/**
|
|
|
|
* @var ?string
|
|
|
|
*/
|
|
|
|
public $b = null;
|
|
|
|
}
|
|
|
|
function f(A $obj): string {
|
|
|
|
if (($obj->a === null) == false) {
|
|
|
|
return $obj->a; // definitely not null
|
|
|
|
} elseif (is_null($obj->b) == false) {
|
|
|
|
return $obj->b;
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException("$obj->a or $obj->b must be set");
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-10-23 02:17:04 +02:00
|
|
|
'ifGetClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-10-23 02:17:04 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {}
|
2017-10-23 02:17:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesA(A $a): void {
|
2018-11-29 05:59:43 +01:00
|
|
|
if (get_class($a) === B::class) {
|
2017-10-23 02:17:04 +02:00
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'ifNotEqualsGetClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-10-23 02:17:04 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): void {}
|
2017-10-23 02:17:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesA(A $a): void {
|
2018-11-29 05:59:43 +01:00
|
|
|
if (get_class($a) !== B::class) {
|
2017-10-23 02:17:04 +02:00
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-11-28 06:46:41 +01:00
|
|
|
'nestedCheckWithSingleVarPerLevel' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?stdClass $a, ?stdClass $b): void {
|
2017-11-28 06:46:41 +01:00
|
|
|
if ($a) {
|
|
|
|
if ($b) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'nestedCheckWithTwoVarsPerLevel' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?stdClass $a, ?stdClass $b, ?stdClass $c, ?stdClass $d): void {
|
2017-11-28 06:46:41 +01:00
|
|
|
if ($a && $b) {
|
|
|
|
if ($c && $d) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'nestedCheckWithReturn' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?stdClass $a, ?stdClass $b): void {
|
2017-11-28 06:46:41 +01:00
|
|
|
if ($a === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($b) {
|
|
|
|
echo "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'propertyFetchAfterNotNullCheck' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-28 06:46:41 +01:00
|
|
|
class A {
|
|
|
|
/** @var ?string */
|
|
|
|
public $foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A;
|
|
|
|
|
|
|
|
if ($a->foo === null) {
|
|
|
|
$a->foo = "hello";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a->foo === "somestring") {}',
|
|
|
|
],
|
2017-12-11 02:21:21 +01:00
|
|
|
'noParadoxForGetopt' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-11 02:21:21 +01:00
|
|
|
$options = getopt("t:");
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!isset($options["t"])) {
|
|
|
|
throw new Exception("bad");
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {}',
|
|
|
|
],
|
2018-05-06 02:32:04 +02:00
|
|
|
'instanceofInOr' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-18 06:06:23 +01:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends A {}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesA(A $a): void {}
|
2017-12-18 06:06:23 +01:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?A $a): void {
|
2017-12-18 06:06:23 +01:00
|
|
|
if ($a instanceof B
|
|
|
|
|| ($a instanceof C && rand(0, 1))
|
|
|
|
) {
|
|
|
|
takesA($a);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-06 02:32:04 +02:00
|
|
|
'instanceofInOrNegated' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-06 02:32:04 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends A {}
|
|
|
|
|
|
|
|
function takesA(A $a): void {}
|
|
|
|
|
|
|
|
function foo(?A $a, ?A $b, ?A $c): void {
|
2018-05-07 07:26:06 +02:00
|
|
|
if (!$a || ($b && $c)) {
|
2018-05-06 02:32:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
takesA($a);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'instanceofInBothOrs' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-06 02:32:04 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends A {}
|
|
|
|
|
|
|
|
function takesA(A $a): void {}
|
|
|
|
|
|
|
|
function foo(?A $a): void {
|
|
|
|
if (($a instanceof B && rand(0, 1))
|
|
|
|
|| ($a instanceof C && rand(0, 1))
|
|
|
|
) {
|
|
|
|
takesA($a);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'instanceofInBothOrsWithSecondVar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-06 02:32:04 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends A {}
|
|
|
|
|
|
|
|
function takesA(A $a): void {}
|
|
|
|
|
|
|
|
function foo(?A $a, ?A $b): void {
|
|
|
|
if (($a instanceof B && $b instanceof B)
|
|
|
|
|| ($a instanceof C && $b instanceof C)
|
|
|
|
) {
|
|
|
|
takesA($a);
|
|
|
|
takesA($b);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'explosionOfCNF' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-06 02:32:04 +02:00
|
|
|
class A {
|
|
|
|
/** @var ?string */
|
|
|
|
public $foo;
|
|
|
|
|
|
|
|
/** @var ?string */
|
|
|
|
public $bar;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a1 = rand(0, 1) ? new A() : null;
|
|
|
|
$a4 = rand(0, 1) ? new A() : null;
|
|
|
|
$a5 = rand(0, 1) ? new A() : null;
|
|
|
|
$a7 = rand(0, 1) ? new A() : null;
|
|
|
|
$a8 = rand(0, 1) ? new A() : null;
|
|
|
|
|
|
|
|
if ($a1 || (($a4 && $a5) || ($a7 && $a8))) {}',
|
|
|
|
],
|
2017-12-18 06:39:00 +01:00
|
|
|
'instanceofInCNFOr' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-18 06:39:00 +01:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends A {}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesA(A $a): void {}
|
2017-12-18 06:39:00 +01:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?A $a): void {
|
2017-12-18 06:47:36 +01:00
|
|
|
$c = rand(0, 1);
|
2017-12-18 06:39:00 +01:00
|
|
|
if (($a instanceof B || $a instanceof C)
|
2017-12-18 06:47:36 +01:00
|
|
|
&& ($a instanceof B || $c)
|
2017-12-18 06:39:00 +01:00
|
|
|
) {
|
|
|
|
takesA($a);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'reconcileNestedOrsInElse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-12-18 06:39:00 +01:00
|
|
|
class A {}
|
|
|
|
class B {}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesA(A $a): void {}
|
2017-12-18 06:39:00 +01:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?A $a, ?B $b): void {
|
2017-12-18 06:39:00 +01:00
|
|
|
if ($a === null || $b === null || rand(0, 1)) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
takesA($a);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-04-17 16:28:49 +02:00
|
|
|
'getClassComparison' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-17 16:28:49 +02:00
|
|
|
class Foo {
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
class Bar extends Foo{
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Baz {
|
|
|
|
public function test(Foo $foo) : void {
|
|
|
|
if (get_class($foo) !== Foo::class) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$foo->bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-06 02:52:10 +02:00
|
|
|
'callWithNonNullInTernary' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-06 02:52:10 +02:00
|
|
|
function sayHello(?int $a, ?int $b): void {
|
|
|
|
if ($a === null && $b === null) {
|
|
|
|
throw new \LogicException();
|
|
|
|
}
|
|
|
|
|
|
|
|
takesInt($a !== null ? $a : $b);
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesInt(int $c) : void {}',
|
|
|
|
],
|
|
|
|
'callWithNonNullInIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-06 02:52:10 +02:00
|
|
|
function sayHello(?int $a, ?int $b): void {
|
|
|
|
if ($a === null && $b === null) {
|
|
|
|
throw new \LogicException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a !== null) {
|
|
|
|
takesInt($a);
|
|
|
|
} else {
|
|
|
|
takesInt($b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesInt(int $c) : void {}',
|
|
|
|
],
|
|
|
|
'callWithNonNullInIfWithCallInElseif' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-06 02:52:10 +02:00
|
|
|
function sayHello(?int $a, ?int $b): void {
|
|
|
|
if ($a === null && $b === null) {
|
|
|
|
throw new \LogicException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a !== null) {
|
|
|
|
takesInt($a);
|
|
|
|
} elseif (rand(0, 1)) {
|
|
|
|
takesInt($b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesInt(int $c) : void {}',
|
|
|
|
],
|
2018-05-06 18:05:55 +02:00
|
|
|
'typeSimplification' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-06 18:05:55 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
function foo(A $a, A $b) : ?B {
|
|
|
|
if (($a instanceof B || !$b instanceof B) && $a instanceof B && $b instanceof B) {
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}',
|
|
|
|
],
|
2018-05-07 07:26:06 +02:00
|
|
|
'instanceofNoRedundant' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-16 17:47:39 +02:00
|
|
|
function logic(Foo $a, Foo $b) : void {
|
|
|
|
if ((!$a instanceof Bat || !$b instanceof Bat)
|
|
|
|
&& (!$a instanceof Bat || !$b instanceof Bar)
|
|
|
|
&& (!$a instanceof Bar || !$b instanceof Bat)
|
2018-05-07 07:26:06 +02:00
|
|
|
&& (!$a instanceof Bar || !$b instanceof Bar)
|
|
|
|
) {
|
|
|
|
|
|
|
|
} else {
|
2018-05-16 17:47:39 +02:00
|
|
|
if ($b instanceof Bat) {}
|
2018-05-07 07:26:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {}
|
|
|
|
class Bar extends Foo {}
|
|
|
|
class Bat extends Foo {}',
|
|
|
|
],
|
2020-03-30 00:28:49 +02:00
|
|
|
'explicitValuesInOrIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-07 23:40:16 +02:00
|
|
|
$s = rand(0, 1) ? "a" : "b";
|
|
|
|
|
2020-03-30 00:28:49 +02:00
|
|
|
if (($s === "a" && rand(0, 1)) || ($s === "b" && rand(0, 1))) {}',
|
|
|
|
],
|
|
|
|
'explicitValuesInOrTernary' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-03-30 00:28:49 +02:00
|
|
|
$s = rand(0, 1) ? "a" : "b";
|
2018-05-07 23:52:18 +02:00
|
|
|
|
|
|
|
$a = (($s === "a" && rand(0, 1)) || ($s === "b" && rand(0, 1))) ? 1 : 0;',
|
2018-05-07 23:40:16 +02:00
|
|
|
],
|
2018-05-08 00:09:40 +02:00
|
|
|
'boolComparison' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-08 00:09:40 +02:00
|
|
|
$a = (bool) rand(0, 1);
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a !== (bool) rand(0, 1)) {
|
|
|
|
echo $a === false ? "a" : "b";
|
|
|
|
}',
|
|
|
|
],
|
2018-05-09 01:11:18 +02:00
|
|
|
'stringConcatenationTrackedValid' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 01:11:18 +02:00
|
|
|
$x = "a";
|
|
|
|
$x = "_" . $x;
|
|
|
|
$array = [$x => 2];
|
|
|
|
echo $array["_a"];',
|
|
|
|
],
|
2019-01-08 15:02:41 +01:00
|
|
|
'noMemoryIssueWithLongConditional' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-08 15:02:41 +01:00
|
|
|
|
2019-01-08 15:19:33 +01:00
|
|
|
function foo(int $c) : string {
|
|
|
|
if (!($c >= 0x5be && $c <= 0x10b7f)) {
|
|
|
|
return "LTR";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($c <= 0x85e) {
|
|
|
|
if ($c === 0x5be ||
|
|
|
|
$c === 0x5c0 ||
|
|
|
|
$c === 0x5c3 ||
|
|
|
|
$c === 0x5c6 ||
|
|
|
|
($c >= 0x5d0 && $c <= 0x5ea) ||
|
|
|
|
($c >= 0x5f0 && $c <= 0x5f4) ||
|
|
|
|
$c === 0x608 ||
|
|
|
|
($c >= 0x712 && $c <= 0x72f) ||
|
|
|
|
($c >= 0x74d && $c <= 0x7a5) ||
|
|
|
|
$c === 0x7b1 ||
|
|
|
|
($c >= 0x7c0 && $c <= 0x7ea) ||
|
|
|
|
($c >= 0x7f4 && $c <= 0x7f5) ||
|
|
|
|
$c === 0x7fa ||
|
|
|
|
($c >= 0x800 && $c <= 0x815) ||
|
|
|
|
$c === 0x81a ||
|
|
|
|
$c === 0x824 ||
|
|
|
|
$c === 0x828 ||
|
|
|
|
($c >= 0x830 && $c <= 0x83e) ||
|
|
|
|
($c >= 0x840 && $c <= 0x858) ||
|
|
|
|
$c === 0x85e
|
|
|
|
) {
|
|
|
|
return "RTL";
|
|
|
|
}
|
|
|
|
} elseif ($c === 0x200f) {
|
2019-01-08 15:02:41 +01:00
|
|
|
return "RTL";
|
2019-01-08 15:19:33 +01:00
|
|
|
} elseif ($c >= 0xfb1d) {
|
|
|
|
if ($c === 0xfb1d ||
|
|
|
|
($c >= 0xfb1f && $c <= 0xfb28) ||
|
|
|
|
($c >= 0xfb2a && $c <= 0xfb36) ||
|
|
|
|
($c >= 0xfb38 && $c <= 0xfb3c) ||
|
|
|
|
$c === 0xfb3e ||
|
|
|
|
($c >= 0x10a10 && $c <= 0x10a13) ||
|
|
|
|
($c >= 0x10a15 && $c <= 0x10a17) ||
|
|
|
|
($c >= 0x10a19 && $c <= 0x10a33) ||
|
|
|
|
($c >= 0x10a40 && $c <= 0x10a47) ||
|
|
|
|
($c >= 0x10a50 && $c <= 0x10a58) ||
|
|
|
|
($c >= 0x10a60 && $c <= 0x10a7f) ||
|
|
|
|
($c >= 0x10b00 && $c <= 0x10b35) ||
|
|
|
|
($c >= 0x10b40 && $c <= 0x10b55) ||
|
|
|
|
($c >= 0x10b58 && $c <= 0x10b72) ||
|
|
|
|
($c >= 0x10b78 && $c <= 0x10b7f)
|
|
|
|
) {
|
|
|
|
return "RTL";
|
|
|
|
}
|
2019-01-08 15:02:41 +01:00
|
|
|
}
|
|
|
|
|
2019-01-08 15:19:33 +01:00
|
|
|
return "LTR";
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => ['MixedInferredReturnType'],
|
2019-01-08 15:02:41 +01:00
|
|
|
],
|
2019-01-18 16:08:16 +01:00
|
|
|
'grandParentInstanceofConfusion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-18 16:08:16 +01:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
|
|
|
class C extends B {}
|
|
|
|
|
|
|
|
function bad(A $x) : void {
|
|
|
|
if (($x instanceof C && rand(0, 1)) || rand(0, 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($x instanceof B) {
|
|
|
|
if ($x instanceof C) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-04-12 00:51:21 +02:00
|
|
|
'invertEquation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-04-12 17:26:33 +02:00
|
|
|
/**
|
|
|
|
* @param mixed $width
|
|
|
|
* @param mixed $height
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
function Foo($width, $height) : void {
|
|
|
|
if (!(is_int($width) || is_float($width)) || !(is_int($height) || is_float($height))) {
|
|
|
|
throw new RuntimeException("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
echo sprintf("padding-top:%s%%;", 100 * ($height/$width));
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-04-12 17:26:33 +02:00
|
|
|
],
|
|
|
|
'invertLogic' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-04-12 17:26:33 +02:00
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
2019-04-12 00:51:21 +02:00
|
|
|
|
2019-04-12 17:26:33 +02:00
|
|
|
function foo(?A $a) : A {
|
|
|
|
if (!$a || !($a instanceof B && rand(0, 1))) {
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $a;
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-04-12 00:51:21 +02:00
|
|
|
],
|
2019-09-21 22:58:31 +02:00
|
|
|
'allowAssertionInElseif' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-09-21 22:58:31 +02:00
|
|
|
class X {
|
|
|
|
public bool $a = false;
|
|
|
|
public bool $b = false;
|
|
|
|
public bool $c = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(X $x) : void {
|
|
|
|
$a = false;
|
|
|
|
if ($x->b && $x->a) {
|
|
|
|
} elseif ($x->c) {
|
|
|
|
$a = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($x->c) {}
|
|
|
|
if ($a) {}
|
|
|
|
}'
|
|
|
|
],
|
2020-04-07 21:13:17 +02:00
|
|
|
'twoVarChangeInElseOnly' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-07 21:13:17 +02:00
|
|
|
class A {
|
|
|
|
public function takesA(A $a) : void {}
|
|
|
|
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatRange(?A $from, ?A $to): void {
|
|
|
|
if (!$to && !$from) {
|
|
|
|
$to = new A();
|
|
|
|
$from = new A();
|
|
|
|
} elseif (!$from) {
|
|
|
|
$from = new A();
|
|
|
|
$from->takesA($to);
|
|
|
|
} else {
|
|
|
|
if (!$to) {
|
|
|
|
$to = new A();
|
|
|
|
$to->takesA($from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$from->foo();
|
|
|
|
$to->foo();
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'twoVarChangeInElseif' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-07 21:13:17 +02:00
|
|
|
class A {
|
|
|
|
public function takesA(A $a) : void {}
|
|
|
|
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatRange(?A $from, ?A $to): void {
|
|
|
|
if (!$to && !$from) {
|
|
|
|
$to = new A();
|
|
|
|
$from = new A();
|
|
|
|
} elseif (!$from) {
|
|
|
|
$from = new A();
|
|
|
|
$from->takesA($to);
|
|
|
|
} elseif (!$to) {
|
|
|
|
$to = new A();
|
|
|
|
$to->takesA($from);
|
|
|
|
}
|
|
|
|
|
|
|
|
$from->foo();
|
|
|
|
$to->foo();
|
|
|
|
}',
|
|
|
|
],
|
2020-08-05 22:53:30 +02:00
|
|
|
'testSimplishThing' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-05 22:53:30 +02:00
|
|
|
function foo(
|
|
|
|
bool $a,
|
|
|
|
bool $b,
|
|
|
|
bool $c,
|
|
|
|
bool $d,
|
|
|
|
bool $e,
|
|
|
|
bool $f,
|
|
|
|
bool $g,
|
|
|
|
bool $h,
|
|
|
|
bool $i,
|
|
|
|
bool $j
|
|
|
|
): bool {
|
|
|
|
return ($a && $b)
|
|
|
|
|| ($c && $d)
|
|
|
|
|| ($e && $f)
|
|
|
|
|| ($g && $h)
|
|
|
|
|| ($i && $j);
|
|
|
|
}'
|
|
|
|
],
|
2020-08-26 21:35:29 +02:00
|
|
|
'fineCheck' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-26 21:35:29 +02:00
|
|
|
function foo(bool $b, bool $c) : void {
|
|
|
|
if ((!$b || rand(0, 1)) && (!$c || rand(0, 1))) {}
|
|
|
|
}'
|
|
|
|
],
|
2020-09-03 02:08:09 +02:00
|
|
|
'noParadoxInTernary' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-03 02:08:09 +02:00
|
|
|
function foo(?bool $b) : string {
|
|
|
|
return $b ? "a" : ($b === null ? "foo" : "b");
|
|
|
|
}',
|
|
|
|
],
|
2020-10-24 17:31:36 +02:00
|
|
|
'cancelOutSameStatement' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-24 17:31:36 +02:00
|
|
|
function edit(?string $a, ?string $b): string {
|
|
|
|
if ((!$a && !$b) || ($a && !$b)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $b;
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'cancelOutDifferentStatement' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-24 17:31:36 +02:00
|
|
|
function edit(?string $a, ?string $b): string {
|
|
|
|
if (!$a && !$b) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a && !$b) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $b;
|
|
|
|
}'
|
|
|
|
],
|
2020-10-26 21:18:42 +01:00
|
|
|
'moreChecks' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-26 21:18:42 +01:00
|
|
|
class B {}
|
|
|
|
class C {}
|
|
|
|
|
|
|
|
function foo(?B $b, ?C $c): B|C {
|
|
|
|
if (!$b && !$c) {
|
|
|
|
throw new Exception("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($b && $c) {
|
|
|
|
return rand(0, 1) ? $b : $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($b) {
|
|
|
|
return $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $c;
|
|
|
|
}'
|
|
|
|
],
|
2020-11-24 20:50:35 +01:00
|
|
|
'dependentType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-24 20:50:35 +01:00
|
|
|
class A {
|
|
|
|
public function isValid() : bool {
|
|
|
|
return (bool) rand(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesA(?A $a) : void {
|
|
|
|
$is_valid_a = $a && $a->isValid();
|
|
|
|
|
|
|
|
if ($is_valid_a) {
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2020-12-03 15:42:20 +01:00
|
|
|
'assignSameName' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-03 15:42:20 +01:00
|
|
|
function foo(string $value): string {
|
|
|
|
$value = "yes" === $value;
|
|
|
|
return !$value ? "foo" : "bar";
|
|
|
|
}'
|
|
|
|
],
|
2021-05-09 18:03:42 +02:00
|
|
|
'dependentTypeUsedAfterCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-09 18:03:42 +02:00
|
|
|
function a(string $_b): void {}
|
|
|
|
|
|
|
|
function foo(?string $c): string {
|
|
|
|
$iftrue = $c !== null;
|
|
|
|
|
|
|
|
if ($c !== null) {
|
|
|
|
a($c);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($iftrue) {
|
|
|
|
return $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}'
|
|
|
|
],
|
2021-05-15 01:21:01 +02:00
|
|
|
'notNullAfterSuccessfulNullsafeMethodCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-15 01:21:01 +02:00
|
|
|
interface X {
|
|
|
|
public function a(): bool;
|
|
|
|
public function b(): string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(?X $x): void {
|
|
|
|
if ($x?->a()) {
|
|
|
|
echo $x->b();
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
2021-05-15 01:21:01 +02:00
|
|
|
],
|
2021-05-18 22:14:58 +02:00
|
|
|
'narrowedTypeAfterIdenticalCheckWithOtherType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-18 22:14:58 +02:00
|
|
|
function a(int $a, ?int $b = null): void
|
|
|
|
{
|
|
|
|
if ($a === $b) {
|
|
|
|
throw new InvalidArgumentException(sprintf("a can not be the same as b (b: %s).", $b));
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2021-10-25 20:16:58 +02:00
|
|
|
'ThrowableInstanceOfThrowableMayBeFalse' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-10-25 20:16:58 +02:00
|
|
|
|
|
|
|
final class Handler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var class-string<Throwable>[]
|
|
|
|
*/
|
|
|
|
private array $dontReport = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<Throwable> $throwable
|
|
|
|
*/
|
|
|
|
public function dontReport(string $throwable): void
|
|
|
|
{
|
|
|
|
$this->dontReport[] = $throwable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shouldReport(Throwable $t): bool
|
|
|
|
{
|
|
|
|
foreach ($this->dontReport as $tc) {
|
|
|
|
if ($t instanceof $tc) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$h = new Handler();
|
|
|
|
$h->dontReport(RuntimeException::class);
|
|
|
|
|
|
|
|
$h->shouldReport(new Exception());
|
|
|
|
$h->shouldReport(new RuntimeException());'
|
|
|
|
],
|
2021-10-26 22:42:29 +02:00
|
|
|
'ThrowableInstanceOfThrowableMayBeTrue' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-10-26 22:42:29 +02:00
|
|
|
|
|
|
|
class Mapper
|
|
|
|
{
|
|
|
|
/** @param class-string<Throwable> $class */
|
|
|
|
final public function map(Throwable $throwable, string $class): ?Throwable
|
|
|
|
{
|
|
|
|
if (! $throwable instanceof $class) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $throwable;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-13 20:38:17 +01:00
|
|
|
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
|
2017-03-16 16:46:07 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2017-03-16 16:46:07 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'threeVarLogicWithChange' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesString(string $s): void {}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b, ?string $c): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a !== null || $b !== null || $c !== null) {
|
|
|
|
$c = null;
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a !== null) {
|
|
|
|
$d = $a;
|
|
|
|
} elseif ($b !== null) {
|
|
|
|
$d = $b;
|
|
|
|
} else {
|
|
|
|
$d = $c;
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
takesString($d);
|
|
|
|
}
|
|
|
|
}',
|
2017-11-16 03:04:25 +01:00
|
|
|
'error_message' => 'PossiblyNullArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'threeVarLogicWithException' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesString(string $s): void {}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b, ?string $c): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a !== null || $b !== null || $c !== null) {
|
|
|
|
if ($c !== null) {
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a !== null) {
|
|
|
|
$d = $a;
|
|
|
|
} elseif ($b !== null) {
|
|
|
|
$d = $b;
|
|
|
|
} else {
|
|
|
|
$d = $c;
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
takesString($d);
|
|
|
|
}
|
|
|
|
}',
|
2017-11-16 03:04:25 +01:00
|
|
|
'error_message' => 'PossiblyNullArgument',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invertedTwoVarLogicNotNestedWithVarChange' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b): string {
|
2017-11-28 06:46:41 +01:00
|
|
|
if ($a !== null || $b !== null) {
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = null;
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-11-28 06:46:41 +01:00
|
|
|
if ($a !== null) return $b;
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a;
|
|
|
|
}',
|
2018-01-05 03:36:16 +01:00
|
|
|
'error_message' => 'NullableReturnStatement',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invertedTwoVarLogicNotNestedWithElseif' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($a || $b) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}',
|
2018-01-05 03:36:16 +01:00
|
|
|
'error_message' => 'NullableReturnStatement',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'threeVarLogicWithElseifAndAnd' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b, ?string $c): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a) {
|
|
|
|
// do nothing
|
|
|
|
} elseif ($b && $c) {
|
|
|
|
// do nothing here
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a && !$b) return $c;
|
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}',
|
2021-09-22 19:47:37 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'twoVarLogicNotNestedWithElseifIncorrectlyReinforcedInIf' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(?string $a, ?string $b): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a) {
|
|
|
|
$a = "";
|
|
|
|
} elseif ($b) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
return "bad";
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (!$a) return $b;
|
|
|
|
return $a;
|
|
|
|
}',
|
2018-05-07 07:26:06 +02:00
|
|
|
'error_message' => 'RedundantCondition',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'repeatedIfStatements' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return string|null */
|
|
|
|
function foo(?string $a) {
|
|
|
|
if ($a) {
|
|
|
|
return $a;
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a) {
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}',
|
2018-05-07 20:34:25 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'repeatedConditionals' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-07 19:38:06 +01:00
|
|
|
function foo(?object $a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a) {
|
|
|
|
// do something
|
|
|
|
} elseif ($a) {
|
|
|
|
// can never get here
|
|
|
|
}
|
|
|
|
}',
|
2018-05-07 20:34:25 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-08-08 00:38:38 +02:00
|
|
|
'repeatedAndConditional' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(string $a, string $b): void {
|
2017-08-08 00:38:38 +02:00
|
|
|
if ($a && $b) {
|
|
|
|
echo "a";
|
|
|
|
} elseif ($a && $b) {
|
|
|
|
echo "b";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ParadoxicalCondition',
|
|
|
|
],
|
|
|
|
'andConditionalAfterOrConditional' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(string $a, string $b): void {
|
2017-08-08 00:38:38 +02:00
|
|
|
if ($a || $b) {
|
|
|
|
echo "a";
|
|
|
|
} elseif ($a && $b) {
|
|
|
|
echo "b";
|
|
|
|
}
|
|
|
|
}',
|
2018-05-07 20:34:25 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2017-08-08 00:38:38 +02:00
|
|
|
],
|
|
|
|
'repeatedVarFromOrConditional' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(string $a, string $b): void {
|
2017-08-08 00:38:38 +02:00
|
|
|
if ($a || $b) {
|
|
|
|
echo "a";
|
|
|
|
} elseif ($a) {
|
|
|
|
echo "b";
|
|
|
|
}
|
|
|
|
}',
|
2018-05-07 20:34:25 +02:00
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
2017-08-08 00:38:38 +02:00
|
|
|
],
|
2018-04-17 21:39:09 +02:00
|
|
|
'typeDoesntEqualType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-17 21:39:09 +02:00
|
|
|
$a = "hello";
|
|
|
|
$b = 5;
|
|
|
|
if ($a !== $b) {}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
2018-05-09 01:11:18 +02:00
|
|
|
'stringConcatenationTrackedInvalid' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-09 01:11:18 +02:00
|
|
|
$x = "a";
|
|
|
|
$x = "_" . $x;
|
|
|
|
$array = [$x => 2];
|
|
|
|
echo $array["other"];',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2020-04-07 19:04:24 +02:00
|
|
|
'redundantTwoVarInElseif' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-07 19:04:24 +02:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
$from = rand(0, 1) ? new A() : null;
|
|
|
|
$to = rand(0, 1) ? new A() : null;
|
|
|
|
|
|
|
|
if ($from === null && $to === null) {
|
|
|
|
} elseif ($from !== null) {
|
|
|
|
} elseif ($to !== null) {}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
2020-09-03 02:08:09 +02:00
|
|
|
'paradoxInTernary' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-03 02:08:09 +02:00
|
|
|
function foo(string $input) : string {
|
|
|
|
return $input === "a" ? "bar" : ($input === "a" ? "foo" : "b");
|
|
|
|
}',
|
|
|
|
'error_message' => 'ParadoxicalCondition',
|
|
|
|
],
|
2020-10-24 18:46:27 +02:00
|
|
|
'mismatchingChecks' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-24 18:46:27 +02:00
|
|
|
function doesntFindBug(?string $old, ?string $new): void {
|
|
|
|
if (empty($old) && empty($new)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($old && empty($new)) || ($new && empty($old))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'RedundantCondition',
|
|
|
|
],
|
2020-11-24 20:50:35 +01:00
|
|
|
'dependentTypeInvalidated' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-24 20:50:35 +01:00
|
|
|
class A {
|
|
|
|
public function isValid() : bool {
|
|
|
|
return (bool) rand(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesA(?A $a) : void {
|
|
|
|
$is_valid_a = $a && $a->isValid();
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$is_valid_a = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_valid_a) {
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyNullReference',
|
|
|
|
],
|
2021-05-15 01:21:01 +02:00
|
|
|
'stillNullAfterNullsafeMethodCall' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-15 01:21:01 +02:00
|
|
|
interface X {
|
|
|
|
public function a(): bool;
|
|
|
|
public function b(): string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function foo(?X $x): void {
|
|
|
|
if (!($x?->a())) {
|
|
|
|
echo $x->b();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'NullReference',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1',
|
2021-05-15 01:21:01 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-02 23:37:56 +02:00
|
|
|
}
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|