2016-12-12 05:41:11 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ArrayAccessTest extends TestCase
|
2016-12-12 05:41:11 +01:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2016-12-12 05:41:11 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2016-12-12 05:41:11 +01:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2016-12-12 05:41:11 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'instanceOfStringOffset' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void { }
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar (array $a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a["a"] instanceof A) {
|
|
|
|
$a["a"]->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'instanceOfIntOffset' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(): void { }
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar (array $a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a[0] instanceof A) {
|
|
|
|
$a[0]->fooFoo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'notEmptyStringOffset' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string> $a
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar (array $a): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a["bat"]) {
|
|
|
|
return $a["bat"];
|
|
|
|
}
|
2017-06-29 05:37:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
return "blah";
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-06-29 23:39:46 +02:00
|
|
|
'issetPropertyStringOffset' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
public $arr = [];
|
|
|
|
}
|
|
|
|
$a = new A();
|
|
|
|
if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'notEmptyIntOffset' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array<string> $a
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar (array $a): string {
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($a[0]) {
|
|
|
|
return $a[0];
|
|
|
|
}
|
2017-06-29 05:37:02 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
return "blah";
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'ignorePossiblyNullArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? [1, 2] : null;
|
|
|
|
echo $a[0];',
|
2017-11-16 02:45:53 +01:00
|
|
|
'assertions' => [],
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_levels' => ['PossiblyNullArrayAccess'],
|
|
|
|
],
|
2017-11-16 02:45:53 +01:00
|
|
|
'ignoreEmptyArrayAccess' => [
|
2017-06-29 05:37:02 +02:00
|
|
|
'<?php
|
|
|
|
$arr = [];
|
|
|
|
$x = $arr[0];
|
|
|
|
if (isset($arr[0]) && $arr[0]) { }',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$x' => 'mixed',
|
2017-06-29 05:37:02 +02:00
|
|
|
],
|
|
|
|
'error_levels' => ['EmptyArrayAccess', 'MixedAssignment'],
|
|
|
|
],
|
2017-11-17 07:18:13 +01:00
|
|
|
'objectLikeWithoutKeys' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesInt(int $i): void {}
|
|
|
|
function takesString(string $s): void {}
|
|
|
|
function takesBool(bool $b): void {}
|
2017-11-17 07:18:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array{int, string, bool} $b
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
function a(array $b): void {
|
2017-11-17 07:18:13 +01:00
|
|
|
takesInt($b[0]);
|
|
|
|
takesString($b[1]);
|
|
|
|
takesBool($b[2]);
|
|
|
|
}',
|
|
|
|
],
|
2018-03-15 15:16:11 +01:00
|
|
|
'stringKeysWithInts' => [
|
|
|
|
'<?php
|
|
|
|
$array = ["01" => "01", "02" => "02"];
|
|
|
|
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
$len = strlen($key);
|
|
|
|
}',
|
|
|
|
],
|
2018-03-21 13:48:30 +01:00
|
|
|
'listAssignmentKeyOffset' => [
|
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
list($a["foo"]) = explode("+", "a+b");
|
|
|
|
echo $a["foo"];',
|
|
|
|
],
|
2018-04-02 06:39:59 +02:00
|
|
|
'objectlikeOptionalNamespacedParam' => [
|
|
|
|
'<?php
|
|
|
|
namespace N;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-param array{key?:string} $p
|
|
|
|
*/
|
|
|
|
function f(array $p): void
|
|
|
|
{
|
|
|
|
echo isset($p["key"]) ? $p["key"] : "";
|
|
|
|
}',
|
|
|
|
],
|
2018-05-03 01:39:11 +02:00
|
|
|
'unsetObjectLikeOffset' => [
|
|
|
|
'<?php
|
|
|
|
function takesInt(int $i) : void {}
|
|
|
|
$x = ["a" => "value"];
|
|
|
|
unset($x["a"]);
|
|
|
|
$x[] = 5;
|
|
|
|
takesInt($x[0]);',
|
|
|
|
],
|
2018-05-12 05:21:53 +02:00
|
|
|
'domNodeListAccessible' => [
|
|
|
|
'<?php
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
$doc->loadXML("<node key=\"value\"/>");
|
|
|
|
$doc->getElementsByTagName("node")[0];'
|
|
|
|
],
|
2018-06-30 21:29:37 +02:00
|
|
|
'getOnArrayAcccess' => [
|
|
|
|
'<?php
|
|
|
|
function foo(ArrayAccess $a) : void {
|
|
|
|
echo $a[0];
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedArgument'],
|
|
|
|
],
|
2018-09-01 02:02:36 +02:00
|
|
|
'mixedKeyMixedOffset' => [
|
|
|
|
'<?php
|
|
|
|
function example(array $x, $y) : void {
|
|
|
|
echo $x[$y];
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedArgument', 'MixedArrayOffset', 'MissingParamType'],
|
|
|
|
],
|
2018-11-30 19:21:08 +01:00
|
|
|
'suppressPossiblyUndefinedStringArrayOffet' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array{a?:string} */
|
|
|
|
$entry = ["a"];
|
|
|
|
|
|
|
|
["a" => $elt] = $entry;
|
2018-12-06 04:35:08 +01:00
|
|
|
strlen($elt);
|
|
|
|
strlen($entry["a"]);',
|
2018-11-30 19:21:08 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['PossiblyUndefinedArrayOffset'],
|
|
|
|
],
|
2018-12-11 00:33:26 +01:00
|
|
|
'noRedundantConditionOnMixedArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<int, int> */
|
|
|
|
$b = [];
|
|
|
|
|
|
|
|
/** @var array<int, int> */
|
|
|
|
$c = [];
|
|
|
|
|
|
|
|
/** @var array<int, mixed> */
|
|
|
|
$d = [];
|
|
|
|
|
|
|
|
if (!empty($d[0]) && !isset($c[$d[0]])) {
|
|
|
|
if (isset($b[$d[0]])) {}
|
|
|
|
}',
|
|
|
|
[],
|
|
|
|
'error_levels' => ['MixedArrayOffset'],
|
|
|
|
],
|
2018-12-14 18:30:13 +01:00
|
|
|
'noEmptyArrayAccessInLoop' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
* @psalm-suppress MixedArrayAccess
|
|
|
|
* @psalm-suppress MixedOperand
|
|
|
|
* @param mixed[] $line
|
|
|
|
*/
|
|
|
|
function _renderCells(array $line): void {
|
|
|
|
foreach ($line as $cell) {
|
|
|
|
$cellOptions = [];
|
|
|
|
if (is_array($cell)) {
|
|
|
|
$cellOptions = $cell[1];
|
|
|
|
}
|
|
|
|
if (isset($cellOptions[0])) {
|
|
|
|
$cellOptions[0] = $cellOptions[0] . "b";
|
|
|
|
} else {
|
|
|
|
$cellOptions[0] = "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|
2016-12-15 07:28:36 +01:00
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2016-12-15 07:28:36 +01:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2016-12-15 07:28:36 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'invalidArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
echo $a[0];',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidArrayAccess',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-11-16 03:10:07 +01:00
|
|
|
'invalidArrayOffset' => [
|
2017-11-16 03:04:25 +01:00
|
|
|
'<?php
|
|
|
|
$x = ["a"];
|
|
|
|
$y = $x["b"];',
|
2017-11-16 03:10:07 +01:00
|
|
|
'error_message' => 'InvalidArrayOffset',
|
2017-11-16 03:04:25 +01:00
|
|
|
],
|
2017-11-16 06:27:11 +01:00
|
|
|
'possiblyInvalidArrayOffsetWithInt' => [
|
|
|
|
'<?php
|
|
|
|
$x = rand(0, 5) > 2 ? ["a" => 5] : "hello";
|
|
|
|
$y = $x[0];',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayOffset',
|
|
|
|
],
|
|
|
|
'possiblyInvalidArrayOffsetWithString' => [
|
|
|
|
'<?php
|
|
|
|
$x = rand(0, 5) > 2 ? ["a" => 5] : "hello";
|
|
|
|
$y = $x["a"];',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayOffset',
|
|
|
|
],
|
2017-11-16 07:11:46 +01:00
|
|
|
'possiblyInvalidArrayAccessWithNestedArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return array<int,array<string,float>>|string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function return_array() {
|
|
|
|
return rand() % 5 > 3 ? [["key" => 3.5]] : "key:3.5";
|
|
|
|
}
|
|
|
|
$result = return_array();
|
|
|
|
$v = $result[0]["key"];',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayOffset',
|
|
|
|
],
|
2017-11-12 03:22:11 +01:00
|
|
|
'possiblyInvalidArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 10) > 5 ? 5 : ["hello"];
|
|
|
|
echo $a[0];',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayAccess',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'mixedArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
/** @var mixed */
|
|
|
|
$a = [];
|
|
|
|
echo $a[0];',
|
|
|
|
'error_message' => 'MixedArrayAccess',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_level' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'mixedArrayOffset' => [
|
|
|
|
'<?php
|
|
|
|
/** @var mixed */
|
|
|
|
$a = 5;
|
|
|
|
echo [1, 2, 3, 4][$a];',
|
|
|
|
'error_message' => 'MixedArrayOffset',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_level' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
|
|
|
echo $a[0];',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'NullArrayAccess',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'possiblyNullArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = rand(0, 1) ? [1, 2] : null;
|
|
|
|
echo $a[0];',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'PossiblyNullArrayAccess',
|
|
|
|
],
|
2018-02-06 17:27:01 +01:00
|
|
|
'specificErrorMessage' => [
|
|
|
|
'<?php
|
|
|
|
$params = ["key" => "value"];
|
|
|
|
echo $params["fieldName"];',
|
2018-04-13 01:42:24 +02:00
|
|
|
'error_message' => 'InvalidArrayOffset - src' . DIRECTORY_SEPARATOR . 'somefile.php:3 - Cannot access '
|
2018-02-06 17:27:01 +01:00
|
|
|
. 'value on variable $params using offset value of',
|
|
|
|
],
|
2018-05-03 01:39:11 +02:00
|
|
|
'missingArrayOffsetAfterUnset' => [
|
|
|
|
'<?php
|
|
|
|
$x = ["a" => "value", "b" => "value"];
|
|
|
|
unset($x["a"]);
|
|
|
|
echo $x["a"];',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2018-08-21 17:40:29 +02:00
|
|
|
'noImpossibleStringAccess' => [
|
|
|
|
'<?php
|
|
|
|
function foo(string $s) : void {
|
|
|
|
echo $s[0][1];
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2018-09-01 02:02:36 +02:00
|
|
|
'mixedKeyStdClassOffset' => [
|
|
|
|
'<?php
|
|
|
|
function example(array $y) : void {
|
|
|
|
echo $y[new stdClass()];
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2018-09-01 02:24:50 +02:00
|
|
|
'toStringOffset' => [
|
|
|
|
'<?php
|
|
|
|
class Foo {
|
|
|
|
public function __toString() {
|
|
|
|
return "Foo";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = ["Foo" => "bar"];
|
|
|
|
echo $a[new Foo];',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2018-11-30 19:21:08 +01:00
|
|
|
'possiblyUndefinedIntArrayOffet' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array{0?:string} */
|
|
|
|
$entry = ["a"];
|
|
|
|
|
|
|
|
[$elt] = $entry;',
|
|
|
|
'error_message' => 'PossiblyUndefinedArrayOffset',
|
|
|
|
],
|
|
|
|
'possiblyUndefinedStringArrayOffet' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array{a?:string} */
|
|
|
|
$entry = ["a"];
|
|
|
|
|
|
|
|
["a" => $elt] = $entry;',
|
|
|
|
'error_message' => 'PossiblyUndefinedArrayOffset',
|
|
|
|
],
|
2018-12-14 21:10:10 +01:00
|
|
|
'possiblyInvalidMixedArrayOffset' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param string|array $key
|
|
|
|
*/
|
|
|
|
function foo(array $a, $key) : void {
|
|
|
|
echo $a[$key];
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayOffset',
|
|
|
|
],
|
|
|
|
'possiblyInvalidMixedUnionArrayOffset' => [
|
|
|
|
'<?php
|
|
|
|
function foo(?array $index): void {
|
|
|
|
if (!$index) {
|
|
|
|
$index = ["foo", []];
|
|
|
|
}
|
|
|
|
$index[1][] = "bar";
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayOffset',
|
|
|
|
'error_level' => ['MixedArrayAssignment'],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-12 23:13:03 +01:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|