1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
psalm/tests/ArrayAccessTest.php

220 lines
7.3 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
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
2016-12-12 05:41:11 +01:00
/**
* @return array
2016-12-12 05:41:11 +01:00
*/
public function providerFileCheckerValidCodeParse()
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"] : "";
}',
],
];
2016-12-12 05:41:11 +01:00
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
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',
],
];
}
2016-12-12 05:41:11 +01:00
}