2017-03-16 16:46:07 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class TypeAlgebraTest extends TestCase
|
2017-03-16 16:46:07 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-03-16 16:46:07 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-03-16 16:46:07 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2017-03-16 16:46:07 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'twoVarLogic' => [
|
|
|
|
'<?php
|
|
|
|
function takesString(string $s) : void {}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function foo(?string $a, ?string $b) : void {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function takesString(string $s) : void {}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function foo(?string $a, ?string $b, ?string $c) : void {
|
|
|
|
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
|
|
|
],
|
|
|
|
'twoVarLogicNotNested' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
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
|
|
|
],
|
|
|
|
'twoVarLogicNotNestedWithElseif' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
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
|
|
|
],
|
|
|
|
'threeVarLogicNotNested' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b, ?string $c) : string {
|
|
|
|
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
|
|
|
],
|
|
|
|
'threeVarLogicNotNestedAndOr' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b, ?string $c) : string {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if ($a) {
|
|
|
|
// do nothing here
|
|
|
|
} elseif ($b) {
|
|
|
|
$a = null;
|
|
|
|
} 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' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a) : void {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function foo(?A $a, ?A $b) : A {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $a) : void {
|
|
|
|
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
|
|
|
],
|
|
|
|
'repeatedSet' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : void {
|
|
|
|
if ($a = rand(0, 1) ? "" : null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a = rand(0, 1) ? "hello" : null;
|
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
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'repeatedSetInsideWhile' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : void {
|
|
|
|
if ($a = rand(0, 1) ? "" : null) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
$a = rand(0, 1) ? "hello" : null;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'byRefAssignment' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : void {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $a, string $b) : void {
|
|
|
|
if ($a && $b) {
|
|
|
|
echo "a";
|
|
|
|
} elseif ($a || $b) {
|
|
|
|
echo "b";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'issetOnOneStringAfterAnother' => [
|
|
|
|
'<?php
|
|
|
|
/** @param string[] $arr */
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
$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
|
|
|
}',
|
|
|
|
],
|
2017-08-19 04:41:25 +02:00
|
|
|
'noParadoxInLoop' => [
|
|
|
|
'<?php
|
|
|
|
function paradox2() : void {
|
|
|
|
$condition = rand() % 2 > 0;
|
|
|
|
|
|
|
|
if (!$condition) {
|
|
|
|
foreach ([1, 2] as $value) {
|
|
|
|
if ($condition) { }
|
|
|
|
$condition = true;
|
|
|
|
}
|
|
|
|
}
|
2017-08-19 05:14:38 +02:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'noParadoxInListAssignment' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $a) : void {
|
|
|
|
if (!$a) {
|
|
|
|
list($a) = explode(":", "a:b");
|
|
|
|
|
|
|
|
if ($a) { }
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-10-11 04:36:33 +02:00
|
|
|
'noParadoxAfterAssignment' => [
|
|
|
|
'<?php
|
|
|
|
function get_bool() : bool {
|
|
|
|
return rand() % 2 > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function leftover() : bool {
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
/** @return array|false */
|
|
|
|
function array_append2(array $errors) {
|
|
|
|
if ($errors) {
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
$errors[] = "deterministic";
|
|
|
|
if ($errors) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @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' => [
|
|
|
|
'<?php
|
|
|
|
function maybe_returns_array() : ?array {
|
|
|
|
if (rand() % 2 > 0) {
|
|
|
|
return ["key" => "value"];
|
|
|
|
}
|
|
|
|
if (rand() % 3 > 0) {
|
|
|
|
throw new Exception("An exception occurred");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function try_catch_check() : array {
|
|
|
|
$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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesA(A $a) : void {
|
|
|
|
if (get_class($a) === "B") {
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'ifNotEqualsGetClass' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesA(A $a) : void {
|
|
|
|
if (get_class($a) !== "B") {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$a->foo();
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-03-16 16:46:07 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2017-03-16 16:46:07 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'threeVarLogicWithChange' => [
|
|
|
|
'<?php
|
|
|
|
function takesString(string $s) : void {}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function foo(?string $a, ?string $b, ?string $c) : void {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function takesString(string $s) : void {}
|
2017-08-08 00:38:38 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function foo(?string $a, ?string $b, ?string $c) : void {
|
|
|
|
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' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if ($a || $b) {
|
|
|
|
$b = null;
|
|
|
|
} 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
|
|
|
'error_message' => 'InvalidReturnType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invertedTwoVarLogicNotNestedWithElseif' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
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;
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidReturnType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'threeVarLogicWithElseifAndAnd' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b, ?string $c) : string {
|
|
|
|
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
|
|
|
'error_message' => 'InvalidReturnType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'twoVarLogicNotNestedWithElseifNegatedInIf' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
if ($a) {
|
|
|
|
$a = null;
|
|
|
|
} 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
|
|
|
'error_message' => 'InvalidReturnType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'twoVarLogicNotNestedWithElseifIncorrectlyReinforcedInIf' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a, ?string $b) : string {
|
|
|
|
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;
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidReturnType',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'repeatedIfStatements' => [
|
|
|
|
'<?php
|
|
|
|
/** @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
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'ParadoxicalCondition',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'repeatedConditionals' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?string $a) : void {
|
|
|
|
if ($a) {
|
|
|
|
// do something
|
|
|
|
} elseif ($a) {
|
|
|
|
// can never get here
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'ParadoxicalCondition',
|
|
|
|
],
|
2017-08-08 00:38:38 +02:00
|
|
|
'repeatedAndConditional' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $a, string $b) : void {
|
|
|
|
if ($a && $b) {
|
|
|
|
echo "a";
|
|
|
|
} elseif ($a && $b) {
|
|
|
|
echo "b";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ParadoxicalCondition',
|
|
|
|
],
|
|
|
|
'andConditionalAfterOrConditional' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $a, string $b) : void {
|
|
|
|
if ($a || $b) {
|
|
|
|
echo "a";
|
|
|
|
} elseif ($a && $b) {
|
|
|
|
echo "b";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ParadoxicalCondition',
|
|
|
|
],
|
|
|
|
'repeatedVarFromOrConditional' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $a, string $b) : void {
|
|
|
|
if ($a || $b) {
|
|
|
|
echo "a";
|
|
|
|
} elseif ($a) {
|
|
|
|
echo "b";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'ParadoxicalCondition',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-04-02 23:37:56 +02:00
|
|
|
}
|
2017-03-16 16:46:07 +01:00
|
|
|
}
|