2016-12-12 05:41:11 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2016-12-12 05:41:11 +01:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
2021-12-03 20:29:06 +01:00
|
|
|
use Psalm\Exception\CodeException;
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
2021-12-03 20:11:20 +01:00
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ArrayAccessTest extends TestCase
|
2016-12-12 05:41:11 +01:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2016-12-12 05:41:11 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnsureArrayOffsetsExist(): void
|
2019-09-18 20:21:06 +02:00
|
|
|
{
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(CodeException::class);
|
2019-11-11 16:48:14 +01:00
|
|
|
$this->expectExceptionMessage('PossiblyUndefinedStringArrayOffset');
|
2019-09-18 20:21:06 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_string_offsets_exist = true;
|
2019-09-18 20:21:06 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
function takesString(string $s): void {}
|
|
|
|
|
|
|
|
/** @param array<string, string> $arr */
|
|
|
|
function takesArrayIteratorOfString(array $arr): void {
|
|
|
|
echo $arr["hello"];
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-09-18 20:21:06 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-09-18 20:21:06 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnsureArrayOffsetsExistWithIssetCheck(): void
|
2019-09-18 20:21:06 +02:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_string_offsets_exist = true;
|
2019-09-18 20:21:06 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
function takesString(string $s): void {}
|
|
|
|
|
|
|
|
/** @param array<string, string> $arr */
|
|
|
|
function takesArrayIteratorOfString(array $arr): void {
|
|
|
|
if (isset($arr["hello"])) {
|
|
|
|
echo $arr["hello"];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-09-18 20:21:06 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-09-18 20:21:06 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testDontEnsureArrayOffsetsExist(): void
|
2019-09-18 20:21:06 +02:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_string_offsets_exist = false;
|
2019-09-18 20:21:06 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
function takesString(string $s): void {}
|
|
|
|
|
|
|
|
/** @param array<string, string> $arr */
|
|
|
|
function takesArrayIteratorOfString(array $arr): void {
|
|
|
|
echo $arr["hello"];
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-09-18 20:21:06 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-09-18 20:21:06 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnsureArrayOffsetsExistWithIssetCheckFollowedByIsArray(): void
|
2019-09-26 01:02:49 +02:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_string_offsets_exist = true;
|
2019-09-26 01:02:49 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param array<string, mixed> $s */
|
|
|
|
function foo(array $s) : void {
|
|
|
|
if (isset($s["a"]) && \is_array($s["a"])) {}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-09-26 01:02:49 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-09-26 01:02:49 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testComplainAfterFirstIsset(): void
|
2019-10-04 03:34:56 +02:00
|
|
|
{
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(CodeException::class);
|
2019-11-11 16:48:14 +01:00
|
|
|
$this->expectExceptionMessage('PossiblyUndefinedStringArrayOffset');
|
2019-10-04 03:34:56 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_string_offsets_exist = true;
|
2019-09-26 01:02:49 +02:00
|
|
|
|
2019-10-04 03:34:56 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (isset($arr["a"]) && $arr["b"]) {}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-10-04 03:34:56 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-10-04 03:34:56 +02:00
|
|
|
}
|
2019-09-26 01:02:49 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnsureArrayIntOffsetsExist(): void
|
2019-10-04 17:08:08 +02:00
|
|
|
{
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(CodeException::class);
|
2019-11-11 16:48:14 +01:00
|
|
|
$this->expectExceptionMessage('PossiblyUndefinedIntArrayOffset');
|
2019-10-04 17:08:08 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
2019-10-04 17:08:08 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
function takesString(string $s): void {}
|
|
|
|
|
|
|
|
/** @param array<int, string> $arr */
|
|
|
|
function takesArrayIteratorOfString(array $arr): void {
|
|
|
|
echo $arr[4];
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-10-04 17:08:08 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-10-04 17:08:08 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testNoIssueWhenUsingArrayValuesOnNonEmptyArray(): void
|
2019-10-09 04:22:58 +02:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
2019-10-09 04:22:58 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param string[][] $arr */
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (count($arr) === 1 && count(array_values($arr)[0]) === 1) {}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-10-09 04:22:58 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-10-09 04:22:58 +02:00
|
|
|
}
|
|
|
|
|
2023-02-22 23:11:10 +01:00
|
|
|
public function testNoIssueWhenUsingArrayValuesOnNonEmptyArrayCheckedWithSizeof(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param string[][] $arr */
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (sizeof($arr) === 1 && sizeof(array_values($arr)[0]) === 1) {}
|
|
|
|
}',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function testNoIssueAfterManyIssets(): void
|
2019-10-17 06:29:51 +02:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
2019-10-17 06:29:51 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function f(array $a) {
|
|
|
|
if (isset($a[1])
|
|
|
|
&& is_array($a[1])
|
|
|
|
&& isset($a[1][2])
|
|
|
|
) {
|
|
|
|
return $a[1][2];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-10-17 06:29:51 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-10-17 06:29:51 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnsureListOffsetExistsNotEmpty(): void
|
2019-10-09 16:36:55 +02:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
2019-10-09 16:36:55 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param list<string> $arr */
|
|
|
|
function takesList(array $arr) : void {
|
|
|
|
if ($arr) {
|
|
|
|
echo $arr[0];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-10-09 16:36:55 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-10-09 16:36:55 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnsureListOffsetExistsAfterArrayPop(): void
|
2019-10-09 16:36:55 +02:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
2019-10-09 16:36:55 +02:00
|
|
|
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(CodeException::class);
|
2019-11-11 16:48:14 +01:00
|
|
|
$this->expectExceptionMessage('PossiblyUndefinedIntArrayOffset');
|
2019-10-09 16:36:55 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param list<string> $arr */
|
|
|
|
function takesList(array $arr) : void {
|
|
|
|
if ($arr) {
|
|
|
|
echo $arr[0];
|
|
|
|
array_pop($arr);
|
|
|
|
echo $arr[0];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-10-09 16:36:55 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-10-09 16:36:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function testEnsureOffsetExistsAfterArrayPush(): void
|
2019-12-29 16:20:38 +01:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
2019-12-29 16:20:38 +01:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
$a = [1, 2, 3];
|
|
|
|
array_push($a, 4);
|
2022-12-18 17:15:15 +01:00
|
|
|
echo $a[3];',
|
2019-12-29 16:20:38 +01:00
|
|
|
);
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-12-29 16:20:38 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 18:43:21 +01:00
|
|
|
public function testEnsureOffsetExistsAfterNestedIsset(): void
|
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_string_offsets_exist = true;
|
2020-12-02 18:43:21 +01:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public int $foo = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, A> $value
|
|
|
|
*/
|
|
|
|
function test(array $value): int
|
|
|
|
{
|
|
|
|
return isset($value["a"]->foo) ? $value["a"]->foo : 0;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-12-02 18:43:21 +01:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2020-12-02 18:43:21 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnsureListOffsetExistsAfterCountValueInRange(): void
|
2019-11-26 22:34:13 +01:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
2019-11-26 22:34:13 +01:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param list<string> $arr */
|
|
|
|
function takesList(array $arr) : void {
|
|
|
|
if (count($arr) >= 3) {
|
|
|
|
echo $arr[0];
|
|
|
|
echo $arr[1];
|
|
|
|
echo $arr[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($arr) > 2) {
|
|
|
|
echo $arr[0];
|
|
|
|
echo $arr[1];
|
|
|
|
echo $arr[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($arr) === 3) {
|
|
|
|
echo $arr[0];
|
|
|
|
echo $arr[1];
|
|
|
|
echo $arr[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (3 === count($arr)) {
|
|
|
|
echo $arr[0];
|
|
|
|
echo $arr[1];
|
|
|
|
echo $arr[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (3 <= count($arr)) {
|
|
|
|
echo $arr[0];
|
|
|
|
echo $arr[1];
|
|
|
|
echo $arr[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (2 < count($arr)) {
|
|
|
|
echo $arr[0];
|
|
|
|
echo $arr[1];
|
|
|
|
echo $arr[2];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-11-26 22:34:13 +01:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-11-26 22:34:13 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 23:33:06 +01:00
|
|
|
public function testCountOnKeyedArrayInRange(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param non-empty-list<string> $list */
|
|
|
|
function bar(array $list) : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$list = ["a"];
|
|
|
|
}
|
|
|
|
if (count($list) > 1) {
|
|
|
|
echo $list[1];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2022-01-20 23:33:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCountOnKeyedArrayInRangeWithUpdate(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param non-empty-list<string> $list */
|
|
|
|
function bar(array $list) : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$list = ["a"];
|
|
|
|
}
|
|
|
|
if (count($list) > 1) {
|
|
|
|
if ($list[1][0] === "a") {
|
|
|
|
$list[1] = "foo";
|
|
|
|
}
|
|
|
|
echo $list[1];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2022-01-20 23:33:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCountOnKeyedArrayOutOfRange(): void
|
|
|
|
{
|
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
|
|
|
|
|
|
|
$this->expectException(CodeException::class);
|
|
|
|
$this->expectExceptionMessage('PossiblyUndefinedIntArrayOffset');
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param non-empty-list<string> $list */
|
|
|
|
function bar(array $list) : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$list = ["a"];
|
|
|
|
}
|
|
|
|
if (count($list) > 1) {
|
|
|
|
echo $list[2];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2022-01-20 23:33:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnsureListOffsetExistsAfterCountValueOutOfRange(): void
|
2019-11-26 22:34:13 +01:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
2019-11-26 22:34:13 +01:00
|
|
|
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(CodeException::class);
|
2019-11-26 22:34:13 +01:00
|
|
|
$this->expectExceptionMessage('PossiblyUndefinedIntArrayOffset');
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param list<string> $arr */
|
|
|
|
function takesList(array $arr) : void {
|
|
|
|
if (count($arr) >= 2) {
|
|
|
|
echo $arr[0];
|
|
|
|
echo $arr[1];
|
|
|
|
echo $arr[2];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-11-26 22:34:13 +01:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-11-26 22:34:13 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testEnsureListOffsetExistsAfterCountValueOutOfRangeSmallerThan(): void
|
2019-11-26 22:34:13 +01:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
2019-11-26 22:34:13 +01:00
|
|
|
|
2021-12-03 20:29:06 +01:00
|
|
|
$this->expectException(CodeException::class);
|
2019-11-26 22:34:13 +01:00
|
|
|
$this->expectExceptionMessage('PossiblyUndefinedIntArrayOffset');
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @param list<string> $arr */
|
|
|
|
function takesList(array $arr) : void {
|
|
|
|
if (2 <= count($arr)) {
|
|
|
|
echo $arr[0];
|
|
|
|
echo $arr[1];
|
|
|
|
echo $arr[2];
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2019-11-26 22:34:13 +01:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2019-11-26 22:34:13 +01:00
|
|
|
}
|
|
|
|
|
2021-05-23 19:59:40 +02:00
|
|
|
public function testDontWorryWhenUnionedWithPositiveInt(): void
|
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
Config::getInstance()->ensure_array_int_offsets_exist = true;
|
2021-05-23 19:59:40 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param list<string> $a
|
|
|
|
* @param 0|positive-int $b
|
|
|
|
*/
|
|
|
|
function foo(array $a, int $b): void {
|
|
|
|
echo $a[$b];
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-05-23 19:59:40 +02:00
|
|
|
);
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
2021-05-23 19:59:40 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2016-12-12 05:41:11 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
2022-12-19 12:55:32 +01:00
|
|
|
'testBuildList' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (random_int(0, 1)) {
|
|
|
|
$a []= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (random_int(0, 1)) {
|
|
|
|
$a []= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pre = $a;
|
|
|
|
|
|
|
|
$a []= 2;
|
|
|
|
|
|
|
|
',
|
|
|
|
'assertions' => [
|
|
|
|
'$pre===' => 'list{0?: 0|1, 1?: 1}',
|
|
|
|
'$a===' => 'list{0: 0|1|2, 1?: 1|2, 2?: 2}',
|
2022-12-19 13:04:24 +01:00
|
|
|
],
|
2022-12-19 12:55:32 +01:00
|
|
|
],
|
|
|
|
'testBuildListOther' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$list = [];
|
|
|
|
$entropy = random_int(0, 2);
|
|
|
|
if ($entropy === 0) {
|
|
|
|
$list[] = "A";
|
|
|
|
} elseif ($entropy === 1) {
|
|
|
|
$list[] = "B";
|
|
|
|
}
|
|
|
|
|
|
|
|
$list[] = "C";
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-12-19 13:04:24 +01:00
|
|
|
'$list===' => "list{0: 'A'|'B'|'C', 1?: 'C'}",
|
|
|
|
],
|
2022-12-19 12:55:32 +01:00
|
|
|
],
|
2022-12-19 21:11:10 +01:00
|
|
|
'testBuildList3' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$a = [0];
|
|
|
|
if (random_int(0, 1)) {
|
|
|
|
$a []= 1;
|
|
|
|
}
|
|
|
|
if (random_int(0, 1)) {
|
|
|
|
$a []= 2;
|
|
|
|
}
|
|
|
|
$a []= 3;
|
|
|
|
',
|
|
|
|
'assertions' => [
|
2022-12-19 21:25:06 +01:00
|
|
|
'$a===' => "list{0: 0, 1: 1|2|3, 2?: 2|3, 3?: 3}",
|
2022-12-19 21:11:10 +01:00
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'instanceOfStringOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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
|
|
|
],
|
2023-04-19 19:14:49 +02:00
|
|
|
'nonEmptyStringAccess' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/** @var non-empty-string $a */
|
|
|
|
$a = "blah";
|
|
|
|
$b = $a[0];',
|
|
|
|
'assertions' => [
|
|
|
|
'$b===' => 'non-empty-string',
|
|
|
|
],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'notEmptyStringOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-06-29 23:39:46 +02:00
|
|
|
class A {
|
|
|
|
/** @var array<string, string> */
|
|
|
|
public $arr = [];
|
|
|
|
}
|
|
|
|
$a = new A();
|
|
|
|
if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }',
|
|
|
|
],
|
2018-12-20 20:26:55 +01:00
|
|
|
'issetPropertyStringOffsetUndefinedClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-20 20:26:55 +01:00
|
|
|
/** @psalm-suppress UndefinedClass */
|
|
|
|
$a = new A();
|
2020-12-02 02:10:48 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress UndefinedClass
|
|
|
|
*/
|
2018-12-20 20:26:55 +01:00
|
|
|
if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedArgument', 'MixedArrayAccess'],
|
2018-12-20 20:26:55 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'notEmptyIntOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$a = rand(0, 1) ? [1, 2] : null;
|
|
|
|
echo $a[0];',
|
2017-11-16 02:45:53 +01:00
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['PossiblyNullArrayAccess'],
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-11-16 02:45:53 +01:00
|
|
|
'ignoreEmptyArrayAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-06-29 05:37:02 +02:00
|
|
|
$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
|
|
|
],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['EmptyArrayAccess', 'MixedAssignment'],
|
2017-06-29 05:37:02 +02:00
|
|
|
],
|
2017-11-17 07:18:13 +01:00
|
|
|
'objectLikeWithoutKeys' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @param array{int, string, bool} $b
|
2017-11-17 07:18:13 +01:00
|
|
|
*/
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-15 15:16:11 +01:00
|
|
|
$array = ["01" => "01", "02" => "02"];
|
|
|
|
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
$len = strlen($key);
|
|
|
|
}',
|
|
|
|
],
|
2018-03-21 13:48:30 +01:00
|
|
|
'listAssignmentKeyOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-21 13:48:30 +01:00
|
|
|
$a = [];
|
|
|
|
list($a["foo"]) = explode("+", "a+b");
|
|
|
|
echo $a["foo"];',
|
|
|
|
],
|
2018-04-02 06:39:59 +02:00
|
|
|
'objectlikeOptionalNamespacedParam' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-02 06:39:59 +02:00
|
|
|
namespace N;
|
|
|
|
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @psalm-param array{key?:string} $p
|
2018-04-02 06:39:59 +02:00
|
|
|
*/
|
|
|
|
function f(array $p): void
|
|
|
|
{
|
|
|
|
echo isset($p["key"]) ? $p["key"] : "";
|
|
|
|
}',
|
|
|
|
],
|
2020-08-30 17:44:14 +02:00
|
|
|
'unsetTKeyedArrayOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-10-03 15:13:47 +02:00
|
|
|
$x1 = ["a" => "value"];
|
|
|
|
unset($x1["a"]);
|
|
|
|
|
|
|
|
$x2 = ["a" => "value", "b" => "value"];
|
|
|
|
unset($x2["a"]);
|
|
|
|
|
|
|
|
$x3 = ["a" => "value", "b" => "value"];
|
|
|
|
$k = "a";
|
|
|
|
unset($x3[$k]);',
|
|
|
|
'assertions' => [
|
|
|
|
'$x1===' => 'array<never, never>',
|
2022-11-12 02:14:21 +01:00
|
|
|
'$x2===' => "array{b: 'value'}",
|
|
|
|
'$x3===' => "array{b: 'value'}",
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2018-05-03 01:39:11 +02:00
|
|
|
],
|
2024-01-13 11:18:48 +01:00
|
|
|
'possiblyUndefinedArrayOffsetKeyedArray' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$d = [];
|
|
|
|
if (!rand(0,1)) {
|
|
|
|
$d[0] = "a";
|
|
|
|
}
|
|
|
|
|
|
|
|
$x = $d[0];',
|
|
|
|
'assertions' => [
|
2024-01-12 23:37:45 +01:00
|
|
|
'$x===' => '\'a\'',
|
2024-01-13 11:18:48 +01:00
|
|
|
],
|
|
|
|
'ignored_issues' => ['PossiblyUndefinedArrayOffset'],
|
|
|
|
],
|
2018-05-12 05:21:53 +02:00
|
|
|
'domNodeListAccessible' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-12 05:21:53 +02:00
|
|
|
$doc = new DOMDocument();
|
|
|
|
$doc->loadXML("<node key=\"value\"/>");
|
2019-01-30 21:40:37 +01:00
|
|
|
$e = $doc->getElementsByTagName("node")[0];',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$e' => 'DOMElement|null',
|
2019-03-23 19:27:54 +01:00
|
|
|
],
|
2018-05-12 05:21:53 +02:00
|
|
|
],
|
2018-06-30 21:29:37 +02:00
|
|
|
'getOnArrayAcccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-30 21:40:37 +01:00
|
|
|
/** @param ArrayAccess<int, string> $a */
|
|
|
|
function foo(ArrayAccess $a) : string {
|
|
|
|
return $a[0];
|
2018-06-30 21:29:37 +02:00
|
|
|
}',
|
|
|
|
],
|
2018-09-01 02:02:36 +02:00
|
|
|
'mixedKeyMixedOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-09-01 02:02:36 +02:00
|
|
|
function example(array $x, $y) : void {
|
|
|
|
echo $x[$y];
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedArgument', 'MixedArrayOffset', 'MissingParamType'],
|
2018-09-01 02:02:36 +02:00
|
|
|
],
|
2024-01-13 13:05:43 +01:00
|
|
|
'suppressPossiblyUndefinedStringArrayOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a?:string} */
|
2018-11-30 19:21:08 +01:00
|
|
|
$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' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['PossiblyUndefinedArrayOffset'],
|
2018-11-30 19:21:08 +01:00
|
|
|
],
|
2018-12-11 00:33:26 +01:00
|
|
|
'noRedundantConditionOnMixedArrayAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-11 00:33:26 +01:00
|
|
|
/** @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]])) {}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => ['MixedArrayOffset'],
|
2018-12-11 00:33:26 +01:00
|
|
|
],
|
2018-12-14 18:30:13 +01:00
|
|
|
'noEmptyArrayAccessInLoop' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-14 18:30:13 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
* @psalm-suppress MixedOperand
|
2020-06-25 06:24:37 +02:00
|
|
|
* @psalm-suppress MixedArrayAssignment
|
2018-12-14 18:30:13 +01:00
|
|
|
* @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";
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-12-14 18:30:13 +01:00
|
|
|
],
|
2018-12-19 06:28:11 +01:00
|
|
|
'arrayAccessPropertyAssertion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-19 06:28:11 +01:00
|
|
|
class A {}
|
|
|
|
class B extends A {
|
|
|
|
/** @var array<int, string> */
|
|
|
|
public $arr = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var array<A> */
|
|
|
|
$as = [];
|
|
|
|
|
|
|
|
if (!$as
|
|
|
|
|| !$as[0] instanceof B
|
|
|
|
|| !$as[0]->arr
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = $as[0]->arr;',
|
|
|
|
],
|
2018-12-21 19:53:22 +01:00
|
|
|
'arrayAccessAfterPassByref' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-21 19:53:22 +01:00
|
|
|
class Arr {
|
|
|
|
/**
|
|
|
|
* @param mixed $c
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function pull(array &$a, string $b, $c = null) {
|
|
|
|
return $a[$b] ?? $c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _renderButton(array $settings): void {
|
|
|
|
Arr::pull($settings, "a", true);
|
|
|
|
|
|
|
|
if (isset($settings["b"])) {
|
|
|
|
Arr::pull($settings, "b");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($settings["c"])) {}
|
2019-03-23 19:27:54 +01:00
|
|
|
}',
|
2018-12-21 19:53:22 +01:00
|
|
|
],
|
2019-01-05 06:15:53 +01:00
|
|
|
'arrayKeyChecks' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-05 06:15:53 +01:00
|
|
|
/**
|
|
|
|
* @param string[] $arr
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (!$arr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($arr as $i => $_) {}
|
|
|
|
|
|
|
|
if ($i === "hello") {}
|
|
|
|
if ($i !== "hello") {}
|
|
|
|
if ($i === 5) {}
|
|
|
|
if ($i !== 5) {}
|
|
|
|
if (is_string($i)) {}
|
|
|
|
if (is_int($i)) {}
|
|
|
|
|
|
|
|
foreach ($arr as $i => $_) {}
|
|
|
|
|
|
|
|
if ($i === "hell") {
|
|
|
|
$i = "hellp";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($i === "hel") {}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'arrayKeyChecksAfterDefinition' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-05 06:15:53 +01:00
|
|
|
/**
|
|
|
|
* @param string[] $arr
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (!$arr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($arr as $i => $_) {}
|
|
|
|
|
|
|
|
if ($i === "hell") {
|
|
|
|
$i = "hellp";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($i === "hel") {}
|
|
|
|
}',
|
|
|
|
],
|
2019-03-23 19:27:54 +01:00
|
|
|
'allowMixedTypeCoercionArrayKeyAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-05 06:15:53 +01:00
|
|
|
/**
|
|
|
|
* @param array<array-key, int> $i
|
|
|
|
* @param array<int, string> $arr
|
|
|
|
*/
|
|
|
|
function foo(array $i, array $arr) : void {
|
|
|
|
foreach ($i as $j => $k) {
|
|
|
|
echo $arr[$j];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MixedArrayTypeCoercion'],
|
2019-01-05 06:15:53 +01:00
|
|
|
],
|
2019-03-23 19:27:54 +01:00
|
|
|
'allowNegativeStringOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-25 16:23:50 +01:00
|
|
|
$a = "hello";
|
|
|
|
echo $a[-5];
|
|
|
|
echo $a[-4];
|
|
|
|
echo $a[-3];
|
|
|
|
echo $a[-2];
|
|
|
|
echo $a[-1];
|
|
|
|
echo $a[0];
|
|
|
|
echo $a[1];
|
|
|
|
echo $a[2];
|
|
|
|
echo $a[3];
|
|
|
|
echo $a[4];',
|
|
|
|
],
|
2019-02-19 17:42:24 +01:00
|
|
|
'arrayAccessAfterPossibleGeneralisation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-19 17:42:24 +01:00
|
|
|
function getArray() : array { return []; }
|
|
|
|
$params = array(
|
|
|
|
"a" => 1,
|
|
|
|
"b" => [
|
|
|
|
"c" => "a",
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$params = getArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $params["b"]["c"];',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => ['MixedArrayAccess', 'MixedArgument'],
|
2019-02-19 17:42:24 +01:00
|
|
|
],
|
2019-04-10 23:25:25 +02:00
|
|
|
'arrayAccessOnObjectWithNullGet' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-02 03:12:03 +01:00
|
|
|
$array = new C([]);
|
|
|
|
$array["key"] = [];
|
|
|
|
/** @psalm-suppress PossiblyInvalidArrayAssignment */
|
|
|
|
$array["key"][] = "testing";
|
|
|
|
|
|
|
|
$c = isset($array["foo"]) ? $array["foo"] : null;
|
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingTemplateParam
|
|
|
|
*/
|
2019-04-10 23:25:25 +02:00
|
|
|
class C implements ArrayAccess
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<C|scalar>
|
|
|
|
*/
|
|
|
|
protected $data = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<scalar|array> $array
|
2020-08-30 17:44:14 +02:00
|
|
|
* @psalm-suppress MixedArgumentTypeCoercion
|
2019-04-10 23:25:25 +02:00
|
|
|
*/
|
2020-08-06 01:39:27 +02:00
|
|
|
final public function __construct(array $array)
|
2019-04-10 23:25:25 +02:00
|
|
|
{
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
$this->data[$key] = new static($value);
|
|
|
|
} else {
|
|
|
|
$this->data[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return C|scalar
|
|
|
|
*/
|
|
|
|
public function offsetGet($name)
|
|
|
|
{
|
|
|
|
return $this->data[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-15 09:45:56 +01:00
|
|
|
* @param ?string $offset
|
2019-04-10 23:25:25 +02:00
|
|
|
* @param scalar|array $value
|
2020-08-30 17:44:14 +02:00
|
|
|
* @psalm-suppress MixedArgumentTypeCoercion
|
2019-04-10 23:25:25 +02:00
|
|
|
*/
|
2023-11-15 09:45:56 +01:00
|
|
|
public function offsetSet($offset, $value) : void
|
2019-04-10 23:25:25 +02:00
|
|
|
{
|
|
|
|
if (is_array($value)) {
|
|
|
|
$value = new static($value);
|
|
|
|
}
|
|
|
|
|
2023-11-15 09:45:56 +01:00
|
|
|
if (null === $offset) {
|
2019-04-10 23:25:25 +02:00
|
|
|
$this->data[] = $value;
|
|
|
|
} else {
|
2023-11-15 09:45:56 +01:00
|
|
|
$this->data[$offset] = $value;
|
2019-04-10 23:25:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __isset(string $name) : bool
|
|
|
|
{
|
|
|
|
return isset($this->data[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __unset(string $name) : void
|
|
|
|
{
|
|
|
|
unset($this->data[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
public function offsetExists($offset) : bool
|
|
|
|
{
|
|
|
|
return $this->__isset($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
public function offsetUnset($offset) : void
|
|
|
|
{
|
|
|
|
$this->__unset($offset);
|
|
|
|
}
|
2020-12-02 03:12:03 +01:00
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2020-04-12 20:00:12 +02:00
|
|
|
'$c' => 'C|null|scalar',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2019-04-10 23:25:25 +02:00
|
|
|
],
|
2019-04-26 15:35:16 +02:00
|
|
|
'singleLetterOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-04-26 15:35:16 +02:00
|
|
|
["s" => "str"]["str"[0]];',
|
|
|
|
],
|
2019-06-08 16:32:40 +02:00
|
|
|
'arrayAccessAfterByRefArrayOffsetAssignment' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-08 16:32:40 +02:00
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @param array{param1: array} $params
|
2019-06-08 16:32:40 +02:00
|
|
|
*/
|
|
|
|
function dispatch(array $params) : void {
|
|
|
|
$params["param1"]["foo"] = "bar";
|
|
|
|
}
|
|
|
|
|
|
|
|
$ar = [];
|
|
|
|
dispatch(["param1" => &$ar]);
|
|
|
|
$value = "foo";
|
|
|
|
if (isset($ar[$value])) {
|
|
|
|
echo (string) $ar[$value];
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => ['MixedArrayAccess'],
|
2019-06-08 16:32:40 +02:00
|
|
|
],
|
2019-06-12 15:13:59 +02:00
|
|
|
'byRefArrayAccessWithoutKnownVarNoNotice' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-06-12 15:13:59 +02:00
|
|
|
$a = new stdClass();
|
|
|
|
/** @psalm-suppress MixedPropertyFetch */
|
|
|
|
print_r([&$a->foo->bar]);',
|
|
|
|
],
|
2019-10-09 00:44:46 +02:00
|
|
|
'accessOffsetOnList' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-10-09 00:44:46 +02:00
|
|
|
/** @param list<int> $arr */
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
echo $arr[3] ?? null;
|
|
|
|
}',
|
|
|
|
],
|
2020-02-07 05:52:27 +01:00
|
|
|
'destructureMixed' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-07 05:52:27 +01:00
|
|
|
class S {
|
|
|
|
protected array $a = [];
|
|
|
|
protected array $b = [];
|
|
|
|
protected array $c = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
*/
|
|
|
|
public function pop(): void {
|
|
|
|
if (!$this->a) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$popped = array_pop($this->a);
|
|
|
|
|
2020-05-03 02:36:41 +02:00
|
|
|
/** @psalm-suppress MixedArrayAccess */
|
2020-02-07 05:52:27 +01:00
|
|
|
[$this->b, $this->c] = $popped;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-02-07 05:52:27 +01:00
|
|
|
],
|
2020-02-18 16:58:56 +01:00
|
|
|
'simpleXmlArrayFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2023-07-25 14:59:00 +02:00
|
|
|
function foo(SimpleXMLElement $s) : ?SimpleXMLElement {
|
2020-02-18 16:58:56 +01:00
|
|
|
return $s["a"];
|
|
|
|
}',
|
|
|
|
],
|
2021-01-06 15:44:12 +01:00
|
|
|
'simpleXmlArrayFetchChildren' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-01-06 15:44:12 +01:00
|
|
|
function iterator(SimpleXMLElement $xml): iterable {
|
2022-06-22 08:53:42 +02:00
|
|
|
$children = $xml->children();
|
|
|
|
assert($children !== null);
|
|
|
|
foreach ($children as $img) {
|
2021-01-06 15:44:12 +01:00
|
|
|
yield $img["src"] ?? "";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-04-12 20:43:12 +02:00
|
|
|
'assertOnArrayAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-04-12 20:43:12 +02:00
|
|
|
class A {
|
|
|
|
public function foo() : void {}
|
|
|
|
}
|
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingTemplateParam
|
|
|
|
*/
|
2020-04-12 20:43:12 +02:00
|
|
|
class C implements ArrayAccess
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $data = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function offsetGet($name)
|
|
|
|
{
|
|
|
|
return $this->data[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-15 09:45:56 +01:00
|
|
|
* @param string $offset
|
2020-04-12 20:43:12 +02:00
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2023-11-15 09:45:56 +01:00
|
|
|
public function offsetSet($offset, $value) : void
|
2020-04-12 20:43:12 +02:00
|
|
|
{
|
2023-11-15 09:45:56 +01:00
|
|
|
$this->data[$offset] = $value;
|
2020-04-12 20:43:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __isset(string $name) : bool
|
|
|
|
{
|
|
|
|
return isset($this->data[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __unset(string $name) : void
|
|
|
|
{
|
|
|
|
unset($this->data[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
public function offsetExists($offset) : bool
|
|
|
|
{
|
|
|
|
return $this->__isset($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
|
|
|
public function offsetUnset($offset) : void
|
|
|
|
{
|
|
|
|
$this->__unset($offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$container = new C();
|
|
|
|
if ($container["a"] instanceof A) {
|
|
|
|
$container["a"]->foo();
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-04-12 20:43:12 +02:00
|
|
|
],
|
2020-07-05 15:12:00 +02:00
|
|
|
'assignmentListCheckForNull' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-07-05 15:12:00 +02:00
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array{0: int, 1:string}|null
|
2020-07-05 15:12:00 +02:00
|
|
|
*/
|
|
|
|
function bar(int $i) {
|
|
|
|
if ( $i < 0)
|
|
|
|
return [$i, "hello"];
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress PossiblyNullArrayAccess */
|
|
|
|
[1 => $foo] = bar(0);
|
|
|
|
|
2022-12-18 17:15:15 +01:00
|
|
|
if ($foo !== null) {}',
|
2020-07-05 15:12:00 +02:00
|
|
|
],
|
2022-01-23 22:05:38 +01:00
|
|
|
'SKIPPED-accessKnownArrayWithPositiveInt' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-09-01 19:03:57 +02:00
|
|
|
/** @param list<int> $arr */
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
$o = [4, 15, 18, 21, 51];
|
|
|
|
$i = 0;
|
|
|
|
foreach ($arr as $a) {
|
|
|
|
if ($o[$i] === $a) {}
|
|
|
|
$i++;
|
|
|
|
}
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-09-01 19:03:57 +02:00
|
|
|
],
|
2020-10-12 18:45:31 +02:00
|
|
|
'arrayAccessOnArraylikeObjectOrArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-12 18:45:31 +02:00
|
|
|
/**
|
|
|
|
* @param arraylike-object<int, string>|array<int, string> $arr
|
|
|
|
*/
|
|
|
|
function test($arr): string {
|
2020-11-26 02:04:57 +01:00
|
|
|
return $arr[0];
|
2020-10-12 18:45:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
test(["a", "b"]);
|
2022-12-18 17:15:15 +01:00
|
|
|
test(new ArrayObject(["a", "b"]));',
|
2020-10-12 18:45:31 +02:00
|
|
|
],
|
2020-10-26 03:16:43 +01:00
|
|
|
'nullCoalesceArrayAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-26 03:16:43 +01:00
|
|
|
/** @param ArrayAccess<int, string> $a */
|
|
|
|
function foo(?ArrayAccess $a) : void {
|
|
|
|
echo $a[0] ?? "default";
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-10-26 03:16:43 +01:00
|
|
|
],
|
2020-11-14 14:57:25 +01:00
|
|
|
'allowUnsettingNested' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-14 14:57:25 +01:00
|
|
|
/** @psalm-immutable */
|
|
|
|
final class test {
|
|
|
|
public function __construct(public int $value) {}
|
|
|
|
}
|
|
|
|
$test = new test(1);
|
|
|
|
$a = [1 => $test];
|
2022-12-18 17:15:15 +01:00
|
|
|
unset($a[$test->value]);',
|
2020-11-14 14:57:25 +01:00
|
|
|
],
|
2020-12-02 02:10:48 +01:00
|
|
|
'arrayAssertionShouldNotBeNull' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-02 02:10:48 +01:00
|
|
|
function foo(?array $arr, string $s) : void {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress PossiblyNullArrayAccess
|
|
|
|
* @psalm-suppress MixedArrayAccess
|
|
|
|
*/
|
|
|
|
if ($arr[$s]["b"] !== true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
* @psalm-suppress MixedArrayAccess
|
|
|
|
* @psalm-suppress PossiblyNullArrayAccess
|
|
|
|
*/
|
|
|
|
echo $arr[$s]["c"];
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-12-02 02:10:48 +01:00
|
|
|
],
|
2021-05-23 20:43:30 +02:00
|
|
|
'TransformBadOffsetToGoodOnes' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-05-23 20:43:30 +02:00
|
|
|
$index = 1.1;
|
|
|
|
|
|
|
|
/** @psalm-suppress InvalidArrayOffset */
|
|
|
|
$_arr1 = [$index => 5];
|
|
|
|
|
|
|
|
$_arr2 = [];
|
|
|
|
/** @psalm-suppress InvalidArrayOffset */
|
|
|
|
$_arr2[$index] = 5;',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [
|
2021-05-23 20:43:30 +02:00
|
|
|
'$_arr1===' => 'non-empty-array<1, 5>',
|
2022-11-12 02:14:21 +01:00
|
|
|
'$_arr2===' => 'array{1: 5}',
|
2022-12-18 17:15:15 +01:00
|
|
|
],
|
2021-05-23 20:43:30 +02:00
|
|
|
],
|
2021-10-22 18:17:08 +02:00
|
|
|
'accessArrayWithSingleStringLiteralOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-10-22 18:17:08 +02:00
|
|
|
/** @param non-empty-array<"name", int> $p */
|
|
|
|
function f($p): int {
|
|
|
|
return $p["name"];
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2021-10-22 18:17:08 +02:00
|
|
|
],
|
2021-11-26 20:52:34 +01:00
|
|
|
'unsetListKeyedArrayDisableListFlag' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-26 20:52:34 +01:00
|
|
|
$a = ["a", "b"];
|
|
|
|
unset($a[0]);
|
|
|
|
',
|
2022-12-18 17:15:15 +01:00
|
|
|
'assertions' => ['$a===' => "array{1: 'b'}"],
|
2021-11-26 20:52:34 +01:00
|
|
|
],
|
2023-08-20 23:39:21 +02:00
|
|
|
'noCrashOnUnknownClassArrayAccess' => [
|
|
|
|
'code' => <<<'PHP'
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Psalmtest\Psalmtest;
|
|
|
|
|
|
|
|
use SomeMissingClass;
|
|
|
|
|
|
|
|
class Test
|
|
|
|
{
|
|
|
|
public function f(): void {
|
|
|
|
/** @var SomeMissingClass */
|
|
|
|
$result = null;
|
|
|
|
|
|
|
|
if ($result['errors'] === true) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP,
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => ['UndefinedDocblockClass'],
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|
2016-12-15 07:28:36 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2016-12-15 07:28:36 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'invalidArrayAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-16 03:04:25 +01:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-16 06:27:11 +01:00
|
|
|
$x = rand(0, 5) > 2 ? ["a" => 5] : "hello";
|
|
|
|
$y = $x[0];',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayOffset',
|
|
|
|
],
|
|
|
|
'possiblyInvalidArrayOffsetWithString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-16 06:27:11 +01:00
|
|
|
$x = rand(0, 5) > 2 ? ["a" => 5] : "hello";
|
|
|
|
$y = $x["a"];',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayOffset',
|
|
|
|
],
|
2017-11-16 07:11:46 +01:00
|
|
|
'possiblyInvalidArrayAccessWithNestedArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-16 07:11:46 +01:00
|
|
|
/**
|
|
|
|
* @return array<int,array<string,float>>|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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-12 03:22:11 +01:00
|
|
|
$a = rand(0, 10) > 5 ? 5 : ["hello"];
|
|
|
|
echo $a[0];',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayAccess',
|
|
|
|
],
|
2024-02-26 09:19:29 +01:00
|
|
|
'insideIssetDisabledForDim' => [
|
|
|
|
'code' => '<?php
|
|
|
|
isset($a[$b]);',
|
|
|
|
'error_message' => 'UndefinedGlobalVariable',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'mixedArrayAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var mixed */
|
|
|
|
$a = [];
|
|
|
|
echo $a[0];',
|
|
|
|
'error_message' => 'MixedArrayAccess',
|
2022-11-05 22:34:42 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'mixedArrayOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var mixed */
|
|
|
|
$a = 5;
|
|
|
|
echo [1, 2, 3, 4][$a];',
|
|
|
|
'error_message' => 'MixedArrayOffset',
|
2022-11-05 22:34:42 +01:00
|
|
|
'ignored_issues' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nullArrayAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
$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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-02-06 17:27:01 +01:00
|
|
|
$params = ["key" => "value"];
|
|
|
|
echo $params["fieldName"];',
|
2019-02-27 22:00:44 +01:00
|
|
|
'error_message' => 'InvalidArrayOffset - src' . DIRECTORY_SEPARATOR . 'somefile.php:3:26 - 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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-05-03 01:39:11 +02:00
|
|
|
$x = ["a" => "value", "b" => "value"];
|
|
|
|
unset($x["a"]);
|
|
|
|
echo $x["a"];',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2018-08-21 17:40:29 +02:00
|
|
|
'noImpossibleStringAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-08-21 17:40:29 +02:00
|
|
|
function foo(string $s) : void {
|
|
|
|
echo $s[0][1];
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2018-09-01 02:02:36 +02:00
|
|
|
'mixedKeyStdClassOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-09-01 02:02:36 +02:00
|
|
|
function example(array $y) : void {
|
|
|
|
echo $y[new stdClass()];
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2018-09-01 02:24:50 +02:00
|
|
|
'toStringOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-09-01 02:24:50 +02:00
|
|
|
class Foo {
|
|
|
|
public function __toString() {
|
|
|
|
return "Foo";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = ["Foo" => "bar"];
|
|
|
|
echo $a[new Foo];',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2024-01-12 23:37:45 +01:00
|
|
|
'possiblyUndefinedIntArrayOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{0?:string} */
|
2018-11-30 19:21:08 +01:00
|
|
|
$entry = ["a"];
|
|
|
|
|
|
|
|
[$elt] = $entry;',
|
|
|
|
'error_message' => 'PossiblyUndefinedArrayOffset',
|
|
|
|
],
|
2024-01-12 23:37:45 +01:00
|
|
|
'possiblyUndefinedStringArrayOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-11-12 02:14:21 +01:00
|
|
|
/** @var array{a?:string} */
|
2018-11-30 19:21:08 +01:00
|
|
|
$entry = ["a"];
|
|
|
|
|
|
|
|
["a" => $elt] = $entry;',
|
|
|
|
'error_message' => 'PossiblyUndefinedArrayOffset',
|
|
|
|
],
|
2018-12-14 21:10:10 +01:00
|
|
|
'possiblyInvalidMixedArrayOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-14 21:10:10 +01:00
|
|
|
/**
|
|
|
|
* @param string|array $key
|
|
|
|
*/
|
|
|
|
function foo(array $a, $key) : void {
|
|
|
|
echo $a[$key];
|
|
|
|
}',
|
|
|
|
'error_message' => 'PossiblyInvalidArrayOffset',
|
|
|
|
],
|
2019-01-04 20:54:40 +01:00
|
|
|
'arrayAccessOnIterable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-04 20:54:40 +01:00
|
|
|
function foo(iterable $i) {
|
|
|
|
echo $i[0];
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidArrayAccess',
|
|
|
|
],
|
2019-01-05 06:15:53 +01:00
|
|
|
'arrayKeyCannotBeBool' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-05 06:15:53 +01:00
|
|
|
/**
|
|
|
|
* @param string[] $arr
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (!$arr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($arr as $i => $_) {}
|
|
|
|
|
|
|
|
if ($i === false) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
|
|
|
'arrayKeyCannotBeFloat' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-05 06:15:53 +01:00
|
|
|
/**
|
|
|
|
* @param string[] $arr
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (!$arr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($arr as $i => $_) {}
|
|
|
|
|
|
|
|
if ($i === 4.0) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
|
|
|
'arrayKeyCannotBeObject' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-05 06:15:53 +01:00
|
|
|
/**
|
|
|
|
* @param string[] $arr
|
|
|
|
*/
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
if (!$arr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($arr as $i => $_) {}
|
|
|
|
|
|
|
|
if ($i === new stdClass) {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2019-03-23 19:27:54 +01:00
|
|
|
'forbidNegativeStringOffsetOutOfRange' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-01-25 16:23:50 +01:00
|
|
|
$a = "hello";
|
|
|
|
echo $a[-6];',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2019-02-25 01:41:53 +01:00
|
|
|
'emptyStringAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-02-25 01:41:53 +01:00
|
|
|
$a = "";
|
|
|
|
echo $a[0];',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2019-03-31 21:27:52 +02:00
|
|
|
'recogniseBadVar' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-31 21:27:52 +02:00
|
|
|
/** @psalm-suppress MixedAssignment */
|
|
|
|
$array = $_GET["foo"] ?? [];
|
|
|
|
|
|
|
|
$array[$a] = "b";',
|
|
|
|
'error_message' => 'UndefinedGlobalVariable',
|
|
|
|
],
|
2019-10-09 18:49:31 +02:00
|
|
|
'unsetListElementShouldChangeToArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-10-09 18:49:31 +02:00
|
|
|
/**
|
|
|
|
* @param list<string> $arr
|
|
|
|
* @return list<string>
|
|
|
|
*/
|
|
|
|
function takesList(array $arr) : array {
|
|
|
|
unset($arr[0]);
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}',
|
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
|
|
|
],
|
2020-02-18 18:53:54 +01:00
|
|
|
'simpleXmlArrayFetchResultCannotEqualString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-18 18:53:54 +01:00
|
|
|
function foo(SimpleXMLElement $s) : void {
|
|
|
|
$b = $s["a"];
|
|
|
|
|
|
|
|
if ($b === "hello" || $b === "1") {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'TypeDoesNotContainType',
|
|
|
|
],
|
2020-08-30 17:44:14 +02:00
|
|
|
'undefinedTKeyedArrayOffset' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-02-22 06:29:59 +01:00
|
|
|
class Example {
|
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @param array{a: string, b: int} $context
|
2020-02-22 06:29:59 +01:00
|
|
|
*/
|
|
|
|
function foo(array $context): void {
|
|
|
|
$context["c"];
|
|
|
|
}
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidArrayOffset',
|
2020-02-22 06:29:59 +01:00
|
|
|
],
|
2020-05-03 02:36:41 +02:00
|
|
|
'destructureNullable' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-05-03 02:36:41 +02:00
|
|
|
/**
|
|
|
|
* @return null|array
|
|
|
|
*/
|
|
|
|
function maybeReturnArray(): ?array {
|
|
|
|
return rand(0, 1) ? null : ["key" => 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
["key" => $a] = maybeReturnArray();',
|
|
|
|
'error_message' => 'PossiblyNullArrayAccess',
|
|
|
|
],
|
2020-05-08 20:36:06 +02:00
|
|
|
'destructureTuple' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-05-08 20:36:06 +02:00
|
|
|
/**
|
2022-11-12 02:14:21 +01:00
|
|
|
* @return array{int, int}
|
2020-05-08 20:36:06 +02:00
|
|
|
*/
|
|
|
|
function size(): array {
|
|
|
|
return [10, 20];
|
|
|
|
}
|
|
|
|
|
|
|
|
[$width, $height, $depth] = size();',
|
|
|
|
'error_message' => 'InvalidArrayOffset',
|
|
|
|
],
|
2020-11-16 02:26:50 +01:00
|
|
|
'negativeListAccess' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-16 02:26:50 +01:00
|
|
|
class HelloWorld
|
|
|
|
{
|
|
|
|
public function sayHello(): void
|
|
|
|
{
|
|
|
|
$a = explode("/", "a/b/c");
|
|
|
|
$x = $a[-3];
|
|
|
|
echo $x;
|
|
|
|
}
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidArrayOffset',
|
2022-11-10 15:18:27 +01:00
|
|
|
],
|
|
|
|
'arrayOpenByDefault' => [
|
|
|
|
'code' => '<?php
|
|
|
|
/**
|
|
|
|
* @param array{a: float, b: float} $params
|
|
|
|
*/
|
|
|
|
function avg(array $params): void {
|
|
|
|
takesArrayOfFloats($params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<array-key, float> $arr
|
|
|
|
*/
|
|
|
|
function takesArrayOfFloats(array $arr): void {
|
|
|
|
foreach ($arr as $a) {
|
|
|
|
echo $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
avg(["a" => 0.5, "b" => 1.5, "c" => new Exception()]);',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2024-01-12 23:37:45 +01:00
|
|
|
'possiblyUndefinedArrayOffsetKeyedArray' => [
|
|
|
|
'code' => '<?php
|
|
|
|
$d = [];
|
|
|
|
if (!rand(0,1)) {
|
|
|
|
$d[0] = "a";
|
|
|
|
}
|
|
|
|
|
|
|
|
$x = $d[0];
|
|
|
|
|
|
|
|
// should not report TypeDoesNotContainNull
|
|
|
|
if ($x === null) {}',
|
|
|
|
'error_message' => 'PossiblyUndefinedArrayOffset',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-12 23:13:03 +01:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|