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

283 lines
9.6 KiB
PHP
Raw Normal View History

2016-12-12 05:41:11 +01:00
<?php
namespace Psalm\Tests;
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
/**
* @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
{
return [
'instanceOfStringOffset' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public function fooFoo(): void { }
}
2018-01-11 21:50:45 +01:00
function bar (array $a): void {
if ($a["a"] instanceof A) {
$a["a"]->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'instanceOfIntOffset' => [
'<?php
class A {
2018-01-11 21:50:45 +01:00
public function fooFoo(): void { }
}
2018-01-11 21:50:45 +01:00
function bar (array $a): void {
if ($a[0] instanceof A) {
$a[0]->fooFoo();
}
2017-05-27 02:05:57 +02:00
}',
],
'notEmptyStringOffset' => [
'<?php
/**
* @param array<string> $a
*/
2018-01-11 21:50:45 +01:00
function bar (array $a): string {
if ($a["bat"]) {
return $a["bat"];
}
return "blah";
2017-05-27 02:05:57 +02:00
}',
],
'issetPropertyStringOffset' => [
'<?php
class A {
/** @var array<string, string> */
public $arr = [];
}
$a = new A();
if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }',
],
'notEmptyIntOffset' => [
'<?php
/**
* @param array<string> $a
*/
2018-01-11 21:50:45 +01:00
function bar (array $a): string {
if ($a[0]) {
return $a[0];
}
return "blah";
2017-05-27 02:05:57 +02:00
}',
],
'ignorePossiblyNullArrayAccess' => [
'<?php
$a = rand(0, 1) ? [1, 2] : null;
echo $a[0];',
'assertions' => [],
2017-05-27 02:05:57 +02:00
'error_levels' => ['PossiblyNullArrayAccess'],
],
'ignoreEmptyArrayAccess' => [
'<?php
$arr = [];
$x = $arr[0];
if (isset($arr[0]) && $arr[0]) { }',
'assertions' => [
2017-06-29 16:22:49 +02:00
'$x' => 'mixed',
],
'error_levels' => ['EmptyArrayAccess', 'MixedAssignment'],
],
'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 {}
/**
* @param array{int, string, bool} $b
*/
2018-01-11 21:50:45 +01:00
function a(array $b): void {
takesInt($b[0]);
takesString($b[1]);
takesBool($b[2]);
}',
],
'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"];',
],
'objectlikeOptionalNamespacedParam' => [
'<?php
namespace N;
/**
* @psalm-param array{key?:string} $p
*/
function f(array $p): void
{
echo isset($p["key"]) ? $p["key"] : "";
}',
],
'unsetObjectLikeOffset' => [
'<?php
function takesInt(int $i) : void {}
$x = ["a" => "value"];
unset($x["a"]);
$x[] = 5;
takesInt($x[0]);',
],
'domNodeListAccessible' => [
'<?php
$doc = new DOMDocument();
$doc->loadXML("<node key=\"value\"/>");
$doc->getElementsByTagName("node")[0];'
],
'getOnArrayAcccess' => [
'<?php
function foo(ArrayAccess $a) : void {
echo $a[0];
}',
'assertions' => [],
'error_levels' => ['MixedArgument'],
],
'mixedKeyMixedOffset' => [
'<?php
function example(array $x, $y) : void {
echo $x[$y];
}',
'assertions' => [],
'error_levels' => ['MixedArgument', 'MixedArrayOffset', 'MissingParamType'],
],
];
2016-12-12 05:41:11 +01:00
}
/**
* @return array
*/
2018-11-06 03:57:36 +01:00
public function providerInvalidCodeParse()
{
return [
'invalidArrayAccess' => [
'<?php
$a = 5;
echo $a[0];',
2017-05-27 02:05:57 +02:00
'error_message' => 'InvalidArrayAccess',
],
'invalidArrayOffset' => [
'<?php
$x = ["a"];
$y = $x["b"];',
'error_message' => 'InvalidArrayOffset',
],
'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',
],
'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',
],
'possiblyInvalidArrayAccess' => [
'<?php
$a = rand(0, 10) > 5 ? 5 : ["hello"];
echo $a[0];',
'error_message' => 'PossiblyInvalidArrayAccess',
],
'mixedArrayAccess' => [
'<?php
/** @var mixed */
$a = [];
echo $a[0];',
'error_message' => 'MixedArrayAccess',
2017-05-27 02:05:57 +02:00
'error_level' => ['MixedAssignment'],
],
'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'],
],
'nullArrayAccess' => [
'<?php
$a = null;
echo $a[0];',
2017-05-27 02:05:57 +02:00
'error_message' => 'NullArrayAccess',
],
'possiblyNullArrayAccess' => [
'<?php
$a = rand(0, 1) ? [1, 2] : null;
echo $a[0];',
2017-05-27 02:05:57 +02:00
'error_message' => 'PossiblyNullArrayAccess',
],
'specificErrorMessage' => [
'<?php
$params = ["key" => "value"];
echo $params["fieldName"];',
'error_message' => 'InvalidArrayOffset - src' . DIRECTORY_SEPARATOR . 'somefile.php:3 - Cannot access '
. 'value on variable $params using offset value of',
],
'missingArrayOffsetAfterUnset' => [
'<?php
$x = ["a" => "value", "b" => "value"];
unset($x["a"]);
echo $x["a"];',
'error_message' => 'InvalidArrayOffset',
],
'noImpossibleStringAccess' => [
'<?php
function foo(string $s) : void {
echo $s[0][1];
}',
'error_message' => 'InvalidArrayOffset',
],
'mixedKeyStdClassOffset' => [
'<?php
function example(array $y) : void {
echo $y[new stdClass()];
}',
'error_message' => 'InvalidArrayOffset',
],
'toStringOffset' => [
'<?php
class Foo {
public function __toString() {
return "Foo";
}
}
$a = ["Foo" => "bar"];
echo $a[new Foo];',
'error_message' => 'InvalidArrayOffset',
],
];
}
2016-12-12 05:41:11 +01:00
}