2017-02-18 02:50:47 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class IssetTest extends TestCase
|
2017-02-18 02:50:47 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2018-01-10 01:33:39 +01:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
2017-02-18 02:50:47 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-02-18 02:50:47 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2017-02-18 02:50:47 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
2018-05-18 17:02:50 +02:00
|
|
|
'issetWithSimpleAssignment' => [
|
|
|
|
'<?php
|
|
|
|
$array = [];
|
|
|
|
|
|
|
|
if (isset($array[$a = 5])) {
|
|
|
|
print "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
print $a;',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['EmptyArrayAccess'],
|
|
|
|
],
|
|
|
|
'issetWithMultipleAssignments' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0, 4) > 2) {
|
|
|
|
$arr = [5 => [3 => "hello"]];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($arr[$a = 5][$b = 3])) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a;
|
|
|
|
echo $b;',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedArrayAccess'],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'isset' => [
|
|
|
|
'<?php
|
|
|
|
$a = isset($b) ? $b : null;',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'mixed',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_levels' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullCoalesce' => [
|
|
|
|
'<?php
|
|
|
|
$a = $b ?? null;',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'mixed',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_levels' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullCoalesceWithGoodVariable' => [
|
|
|
|
'<?php
|
2017-11-28 06:46:41 +01:00
|
|
|
$b = rand(0, 10) > 5 ? "hello" : null;
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = $b ?? null;',
|
|
|
|
'assertions' => [
|
2017-11-28 06:46:41 +01:00
|
|
|
'$a' => 'string|null',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'issetKeyedOffset' => [
|
|
|
|
'<?php
|
|
|
|
if (!isset($foo["a"])) {
|
|
|
|
$foo["a"] = "hello";
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo[\'a\']' => 'mixed',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'error_levels' => [],
|
|
|
|
'scope_vars' => [
|
2017-05-27 02:05:57 +02:00
|
|
|
'$foo' => \Psalm\Type::getArray(),
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'issetKeyedOffsetORFalse' => [
|
|
|
|
'<?php
|
|
|
|
/** @return void */
|
|
|
|
function takesString(string $str) {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bar = rand(0, 1) ? ["foo" => "bar"] : false;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (isset($bar["foo"])) {
|
|
|
|
takesString($bar["foo"]);
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2018-01-10 01:33:39 +01:00
|
|
|
'error_levels' => ['PossiblyInvalidArrayAccess'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullCoalesceKeyedOffset' => [
|
|
|
|
'<?php
|
|
|
|
$foo["a"] = $foo["a"] ?? "hello";',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo[\'a\']' => 'mixed',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'error_levels' => ['MixedAssignment'],
|
|
|
|
'scope_vars' => [
|
2017-05-27 02:05:57 +02:00
|
|
|
'$foo' => \Psalm\Type::getArray(),
|
|
|
|
],
|
|
|
|
],
|
2017-11-28 22:52:52 +01:00
|
|
|
'noRedundantConditionOnMixed' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function testarray(array $data): void {
|
2017-11-28 22:52:52 +01:00
|
|
|
foreach ($data as $item) {
|
|
|
|
if (isset($item["a"]) && isset($item["b"]) && isset($item["b"]["c"])) {
|
|
|
|
echo "Found\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2018-01-10 01:33:39 +01:00
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArrayAccess'],
|
2017-11-28 22:52:52 +01:00
|
|
|
],
|
2017-12-14 01:46:58 +01:00
|
|
|
'testUnset' => [
|
|
|
|
'<?php
|
|
|
|
$foo = ["a", "b", "c"];
|
|
|
|
foreach ($foo as $bar) {}
|
|
|
|
unset($foo, $bar);
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(): void {
|
2017-12-14 01:46:58 +01:00
|
|
|
$foo = ["a", "b", "c"];
|
|
|
|
foreach ($foo as $bar) {}
|
|
|
|
unset($foo, $bar);
|
|
|
|
}',
|
|
|
|
],
|
2017-12-19 00:47:17 +01:00
|
|
|
'issetObjectLike' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [
|
|
|
|
"profile" => [
|
|
|
|
"foo" => "bar",
|
|
|
|
],
|
|
|
|
"groups" => [
|
|
|
|
"foo" => "bar",
|
|
|
|
"hide" => rand() % 2 > 0,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($arr as $item) {
|
|
|
|
if (!isset($item["hide"]) || !$item["hide"]) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-01-29 21:47:25 +01:00
|
|
|
'issetPropertyAffirmsObject' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?int */
|
|
|
|
public $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesA(?A $a): A {
|
|
|
|
if (isset($a->id)) {
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new A();
|
|
|
|
}',
|
|
|
|
],
|
2018-02-17 17:24:08 +01:00
|
|
|
'issetVariableKeysWithoutChange' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [[1, 2, 3], null, [1, 2, 3], null];
|
|
|
|
$b = 2;
|
|
|
|
$c = 0;
|
|
|
|
if (isset($arr[$b][$c])) {
|
|
|
|
echo $arr[$b][$c];
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'issetNonNullArrayKey' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<int, int> $arr
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : int {
|
|
|
|
$b = rand(0, 3);
|
|
|
|
if (!isset($arr[$b])) {
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
return $arr[$b];
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'issetArrayOffsetConditionalCreationWithInt' => [
|
|
|
|
'<?php
|
|
|
|
/** @param array<int, string> $arr */
|
|
|
|
function foo(array $arr) : string {
|
|
|
|
if (!isset($arr[0])) {
|
|
|
|
$arr[0] = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr[0];
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'issetArrayOffsetConditionalCreationWithVariable' => [
|
|
|
|
'<?php
|
|
|
|
/** @param array<int, string> $arr */
|
|
|
|
function foo(array $arr) : string {
|
|
|
|
$b = 5;
|
|
|
|
|
|
|
|
if (!isset($arr[$b])) {
|
|
|
|
$arr[$b] = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr[$b];
|
|
|
|
}',
|
|
|
|
],
|
2018-03-08 21:57:46 +01:00
|
|
|
'noExceptionOnBracketString' => [
|
|
|
|
'<?php
|
|
|
|
if (isset($foo["bar[]"])) {}',
|
|
|
|
],
|
2018-04-06 22:23:10 +02:00
|
|
|
'issetArrayOffsetAndProperty' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?B */
|
|
|
|
public $b;
|
|
|
|
}
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param A[] $arr
|
|
|
|
*/
|
|
|
|
function takesAList(array $arr) : B {
|
|
|
|
if (isset($arr[1]->b)) {
|
|
|
|
return $arr[1]->b;
|
|
|
|
}
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}',
|
|
|
|
],
|
2018-04-07 00:28:22 +02:00
|
|
|
'allowUnknownAdditionToInt' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [1, 1, 1, 1, 2, 5, 3, 2];
|
|
|
|
$cumulative = [];
|
|
|
|
|
|
|
|
foreach ($arr as $val) {
|
|
|
|
if (isset($cumulative[$val])) {
|
|
|
|
$cumulative[$val] = $cumulative[$val] + 1;
|
|
|
|
} else {
|
|
|
|
$cumulative[$val] = 1;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'allowUnknownArrayMergeToInt' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [1, 1, 1, 1, 2, 5, 3, 2];
|
|
|
|
$cumulative = [];
|
|
|
|
|
|
|
|
foreach ($arr as $val) {
|
|
|
|
if (isset($cumulative[$val])) {
|
|
|
|
$cumulative[$val] = array_merge($cumulative[$val], [$val]);
|
|
|
|
} else {
|
|
|
|
$cumulative[$val] = [$val];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($cumulative as $arr) {
|
|
|
|
foreach ($arr as $val) {
|
|
|
|
takesInt($val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesInt(int $i) : void {}',
|
|
|
|
],
|
2018-04-11 20:19:42 +02:00
|
|
|
'returnArrayWithDefinedKeys' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{bar?: int, foo: int|string} $arr
|
|
|
|
* @return array{bar: int, foo: string}|null
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : ?array {
|
|
|
|
if (!isset($arr["bar"])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_int($arr["foo"])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}',
|
|
|
|
],
|
2018-05-31 00:56:44 +02:00
|
|
|
'arrayAccessAfterOneIsset' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [];
|
|
|
|
|
|
|
|
foreach ([1, 2, 3] as $foo) {
|
|
|
|
if (!isset($arr["bar"])) {
|
|
|
|
$arr["bar"] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $arr["bar"];
|
|
|
|
}',
|
|
|
|
],
|
2018-04-11 20:19:42 +02:00
|
|
|
'arrayAccessAfterTwoIssets' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [];
|
|
|
|
|
|
|
|
foreach ([1, 2, 3] as $foo) {
|
|
|
|
if (!isset($arr["foo"])) {
|
|
|
|
$arr["foo"] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($arr["bar"])) {
|
|
|
|
$arr["bar"] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $arr["bar"];
|
|
|
|
}',
|
|
|
|
],
|
2018-05-05 23:30:18 +02:00
|
|
|
'issetAdditionalVar' => [
|
|
|
|
'<?php
|
|
|
|
class Example {
|
|
|
|
const FOO = "foo";
|
|
|
|
/**
|
|
|
|
* @param array{bar:string} $params
|
|
|
|
*/
|
|
|
|
public function test(array $params) : bool {
|
|
|
|
if (isset($params[self::FOO])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params["bat"])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2018-05-18 17:02:50 +02:00
|
|
|
'noRedundantConditionAfterIsset' => [
|
|
|
|
'<?php
|
|
|
|
/** @param array<string, array<int, string>> $arr */
|
|
|
|
function foo(array $arr, string $k) : void {
|
|
|
|
if (!isset($arr[$k])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($arr[$k][0]) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-05-31 02:54:03 +02:00
|
|
|
'mixedArrayIsset' => [
|
|
|
|
'<?php
|
|
|
|
$a = isset($_GET["a"]) ? $_GET["a"] : "";
|
|
|
|
if ($a) {}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArrayAccess'],
|
|
|
|
],
|
2018-05-31 21:07:03 +02:00
|
|
|
'nestedArrayAccessInLoopAfterIsset' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [];
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
if (!isset($arr["a"]["b"])) {
|
|
|
|
$arr["a"]["b"] = "foo";
|
|
|
|
}
|
|
|
|
echo $arr["a"]["b"];
|
|
|
|
} else {
|
|
|
|
$arr["c"] = "foo";
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2018-06-01 17:16:42 +02:00
|
|
|
'issetVarInLoopBeforeAssignment' => [
|
|
|
|
'<?php
|
|
|
|
function foo() : void {
|
|
|
|
while (rand(0, 1)) {
|
|
|
|
if (!isset($foo)) {
|
|
|
|
$foo = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-06-08 15:31:21 +02:00
|
|
|
'issetOnArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
function foo(ArrayAccess $arr) : void {
|
|
|
|
$a = isset($arr["a"]) ? $arr["a"] : "a";
|
|
|
|
takesInt($a);
|
|
|
|
}
|
|
|
|
function takesInt(int $i) : void {}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedAssignment', 'MixedArgument'],
|
2018-08-21 06:43:45 +02:00
|
|
|
],
|
|
|
|
'noParadoxOnMultipleNotIssets' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array */
|
|
|
|
$array = [];
|
|
|
|
function sameString(string $string): string {
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($array[sameString("key1")]) || isset($array[sameString("key2")])) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($array[sameString("key3")]) || !isset($array[sameString("key4")])) {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-18 02:50:47 +01:00
|
|
|
}
|
2018-01-10 01:33:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'complainAboutBadCallInIsset' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
$a = isset(A::foo()[0]);',
|
|
|
|
'error_message' => 'UndefinedMethod',
|
|
|
|
],
|
2018-02-17 17:24:08 +01:00
|
|
|
'issetVariableKeysWithChange' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [[1, 2, 3], null, [1, 2, 3], null];
|
|
|
|
$b = 2;
|
|
|
|
$c = 0;
|
|
|
|
if (isset($arr[$b][$c])) {
|
|
|
|
$b = 1;
|
|
|
|
echo $arr[$b][$c];
|
|
|
|
}',
|
2018-05-05 23:30:18 +02:00
|
|
|
'error_message' => 'NullArrayAccess',
|
|
|
|
],
|
|
|
|
'issetAdditionalVarWithSealedObjectLike' => [
|
|
|
|
'<?php
|
|
|
|
class Example {
|
|
|
|
const FOO = "foo";
|
|
|
|
public function test() : bool {
|
|
|
|
$params = ["bar" => "bat"];
|
|
|
|
|
|
|
|
if (isset($params[self::FOO])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
2018-02-17 17:24:08 +01:00
|
|
|
],
|
2018-01-10 01:33:39 +01:00
|
|
|
];
|
|
|
|
}
|
2017-02-18 02:50:47 +01:00
|
|
|
}
|