2016-09-12 06:02:50 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Context;
|
2016-09-12 06:02:50 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class ArrayAssignmentTest extends TestCase
|
2016-09-12 06:02:50 +02:00
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2016-09-22 18:26:24 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-22 18:26:24 +02:00
|
|
|
public function testConditionalAssignment()
|
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
2016-09-22 18:26:24 +02:00
|
|
|
'somefile.php',
|
2017-07-25 22:11:02 +02:00
|
|
|
'<?php
|
2016-09-22 18:26:24 +02:00
|
|
|
if ($b) {
|
|
|
|
$foo["a"] = "hello";
|
2017-07-25 22:11:02 +02:00
|
|
|
}'
|
2016-09-22 18:26:24 +02:00
|
|
|
);
|
2016-11-02 07:29:00 +01:00
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-10-15 19:10:05 +02:00
|
|
|
$context->vars_in_scope['$b'] = \Psalm\Type::getBool();
|
|
|
|
$context->vars_in_scope['$foo'] = \Psalm\Type::getArray();
|
2018-01-21 16:22:04 +01:00
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
|
2016-10-15 19:10:05 +02:00
|
|
|
$this->assertFalse(isset($context->vars_in_scope['$foo[\'a\']']));
|
2016-09-22 19:45:47 +02:00
|
|
|
}
|
|
|
|
|
2017-01-20 06:23:58 +01:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerValidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
2018-05-18 17:02:50 +02:00
|
|
|
'genericArrayCreationWithSingleIntValue' => [
|
|
|
|
'<?php
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
$out[] = 4;',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$out' => 'non-empty-list<int>',
|
2018-05-18 17:02:50 +02:00
|
|
|
],
|
|
|
|
],
|
2017-11-19 18:33:43 +01:00
|
|
|
'genericArrayCreationWithInt' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
$out = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1, 2, 3, 4, 5] as $value) {
|
|
|
|
$out[] = 4;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$out' => 'non-empty-list<int>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'generic2dArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$out = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1, 2, 3, 4, 5] as $value) {
|
|
|
|
$out[] = [4];
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$out' => 'non-empty-list<array{0: int}>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'generic2dArrayCreationAddedInIf' => [
|
|
|
|
'<?php
|
|
|
|
$out = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bits = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ([1, 2, 3, 4, 5] as $value) {
|
|
|
|
if (rand(0,100) > 50) {
|
|
|
|
$out[] = $bits;
|
|
|
|
$bits = [];
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bits[] = 4;
|
2016-10-15 19:11:08 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2018-12-19 22:15:19 +01:00
|
|
|
$out[] = $bits;',
|
2017-04-25 05:45:02 +02:00
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$out' => 'non-empty-list<non-empty-list<int>>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'genericArrayCreationWithObjectAddedInIf' => [
|
|
|
|
'<?php
|
|
|
|
class B {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$out = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if (rand(0,10) === 10) {
|
|
|
|
$out[] = new B();
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$out' => 'list<B>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'genericArrayCreationWithElementAddedInSwitch' => [
|
|
|
|
'<?php
|
|
|
|
$out = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
switch (rand(0,10)) {
|
|
|
|
case 5:
|
|
|
|
$out[] = 4;
|
|
|
|
break;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
case 6:
|
|
|
|
// do nothing
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$out' => 'list<int>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'genericArrayCreationWithElementsAddedInSwitch' => [
|
|
|
|
'<?php
|
|
|
|
$out = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
switch (rand(0,10)) {
|
|
|
|
case 5:
|
|
|
|
$out[] = 4;
|
|
|
|
break;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
case 6:
|
|
|
|
$out[] = "hello";
|
|
|
|
break;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$out' => 'list<int|string>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'genericArrayCreationWithElementsAddedInSwitchWithNothing' => [
|
|
|
|
'<?php
|
|
|
|
$out = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
switch (rand(0,10)) {
|
|
|
|
case 5:
|
|
|
|
$out[] = 4;
|
|
|
|
break;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
case 6:
|
|
|
|
$out[] = "hello";
|
|
|
|
break;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
case 7:
|
|
|
|
// do nothing
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$out' => 'list<int|string>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicit2dIntArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo[][] = "hello";',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$foo' => 'non-empty-list<array<int, string>>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicit3dIntArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo[][][] = "hello";',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$foo' => 'non-empty-list<list<array<int, string>>>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicit4dIntArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo[][][][] = "hello";',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$foo' => 'non-empty-list<list<list<array<int, string>>>>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicitIndexedIntArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
2019-05-24 00:06:22 +02:00
|
|
|
$foo[0] = "a";
|
|
|
|
$foo[1] = "b";
|
|
|
|
$foo[2] = "c";
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bar = [0, 1, 2];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$bat = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
foreach ($foo as $i => $text) {
|
|
|
|
$bat[$text] = $bar[$i];
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{0: string, 1: string, 2: string}',
|
|
|
|
'$bar' => 'array{0: int, 1: int, 2: int}',
|
2019-09-08 16:23:12 +02:00
|
|
|
'$bat' => 'non-empty-array<string, int>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicitStringArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["bar"] = "hello";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{bar: string}',
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo[\'bar\']' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicit2dStringArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["bar"]["baz"] = "hello";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{bar: array{baz: string}}',
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo[\'bar\'][\'baz\']' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicit3dStringArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["bar"]["baz"]["bat"] = "hello";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{bar: array{baz: array{bat: string}}}',
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo[\'bar\'][\'baz\'][\'bat\']' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicit4dStringArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["bar"]["baz"]["bat"]["bap"] = "hello";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{bar: array{baz: array{bat: array{bap: string}}}}',
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo[\'bar\'][\'baz\'][\'bat\'][\'bap\']' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'2Step2dStringArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = ["bar" => []];
|
|
|
|
$foo["bar"]["baz"] = "hello";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{bar: array{baz: string}}',
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo[\'bar\'][\'baz\']' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'2StepImplicit3dStringArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = ["bar" => []];
|
|
|
|
$foo["bar"]["baz"]["bat"] = "hello";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{bar: array{baz: array{bat: string}}}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-03-17 21:53:11 +01:00
|
|
|
'conflictingTypesWithNoAssignment' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
$foo = [
|
|
|
|
"bar" => ["a" => "b"],
|
|
|
|
"baz" => [1]
|
|
|
|
];',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{bar: array{a: string}, baz: array{0: int}}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicitObjectLikeCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [
|
|
|
|
"bar" => 1,
|
|
|
|
];
|
|
|
|
$foo["baz"] = "a";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{bar: int, baz: string}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'conflictingTypesWithAssignment' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [
|
|
|
|
"bar" => ["a" => "b"],
|
|
|
|
"baz" => [1]
|
|
|
|
];
|
|
|
|
$foo["bar"]["bam"]["baz"] = "hello";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{bar: array{a: string, bam: array{baz: string}}, baz: array{0: int}}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'conflictingTypesWithAssignment2' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["a"] = "hello";
|
|
|
|
$foo["b"][] = "goodbye";
|
|
|
|
$bar = $foo["a"];',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$foo' => 'array{a: string, b: non-empty-list<string>}',
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo[\'a\']' => 'string',
|
2019-10-09 00:44:46 +02:00
|
|
|
'$foo[\'b\']' => 'non-empty-list<string>',
|
2017-06-29 16:22:49 +02:00
|
|
|
'$bar' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'conflictingTypesWithAssignment3' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["a"] = "hello";
|
|
|
|
$foo["b"]["c"]["d"] = "goodbye";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{a: string, b: array{c: array{d: string}}}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'nestedObjectLikeAssignment' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["a"]["b"] = "hello";
|
|
|
|
$foo["a"]["c"] = 1;',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{a: array{b: string, c: int}}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'conditionalObjectLikeAssignment' => [
|
|
|
|
'<?php
|
|
|
|
$foo = ["a" => "hello"];
|
|
|
|
if (rand(0, 10) === 5) {
|
|
|
|
$foo["b"] = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$foo["b"] = 2;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{a: string, b: int}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayKey' => [
|
|
|
|
'<?php
|
|
|
|
$a = ["foo", "bar"];
|
|
|
|
$b = $a[0];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$c = ["a" => "foo", "b"=> "bar"];
|
|
|
|
$d = "a";
|
2017-11-16 02:45:53 +01:00
|
|
|
$e = $c[$d];',
|
2017-04-25 05:45:02 +02:00
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$b' => 'string',
|
|
|
|
'$e' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'conditionalCheck' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{b:string} $a
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
function fooFoo($a) {
|
|
|
|
if ($a["b"]) {
|
|
|
|
return $a["b"];
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'assertions' => [],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'variableKeyArrayCreate' => [
|
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
$b = "boop";
|
|
|
|
$a[$b][] = "bam";
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$c = [];
|
|
|
|
$c[$b][$b][] = "bam";',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$a' => 'array{boop: non-empty-list<string>}',
|
|
|
|
'$c' => 'array{boop: non-empty-array<string, non-empty-list<string>>}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'assignExplicitValueToGeneric' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, array<string, string>> */
|
|
|
|
$a = [];
|
|
|
|
$a["foo"] = ["bar" => "baz"];',
|
|
|
|
'assertions' => [
|
2018-12-19 22:15:19 +01:00
|
|
|
'$a' => 'non-empty-array<string, non-empty-array<string, string>>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'additionWithEmpty' => [
|
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
$a += ["bar"];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = [] + ["bar"];',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$a' => 'array{0: string}',
|
|
|
|
'$b' => 'array{0: string}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'additionDifferentType' => [
|
|
|
|
'<?php
|
|
|
|
$a = ["bar"];
|
|
|
|
$a += [1];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b = ["bar"] + [1];',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$a' => 'array{0: string}',
|
|
|
|
'$b' => 'array{0: string}',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'present1dArrayTypeWithVarKeys' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, array<int, string>> */
|
|
|
|
$a = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$foo = "foo";
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$a[$foo][] = "bat";',
|
2017-05-27 02:05:57 +02:00
|
|
|
'assertions' => [],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'present2dArrayTypeWithVarKeys' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<string, array<string, array<int, string>>> */
|
|
|
|
$b = [];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$foo = "foo";
|
|
|
|
$bar = "bar";
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$b[$foo][$bar][] = "bat";',
|
2017-05-27 02:05:57 +02:00
|
|
|
'assertions' => [],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'objectLikeWithIntegerKeys' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array{0: string, 1: int} **/
|
|
|
|
$a = ["hello", 5];
|
|
|
|
$b = $a[0]; // string
|
|
|
|
$c = $a[1]; // int
|
|
|
|
list($d, $e) = $a; // $d is string, $e is int',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$b' => 'string',
|
|
|
|
'$c' => 'int',
|
|
|
|
'$d' => 'string',
|
|
|
|
'$e' => 'int',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
|
|
|
],
|
2019-09-08 16:23:12 +02:00
|
|
|
'objectLikeArrayAdditionNotNested' => [
|
2017-11-11 20:19:45 +01:00
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["a"] = 1;
|
|
|
|
$foo += ["b" => [2, 3]];',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{a: int, b: array{0: int, 1: int}}',
|
2017-11-11 20:19:45 +01:00
|
|
|
],
|
|
|
|
],
|
2017-11-19 18:33:43 +01:00
|
|
|
'nestedObjectLikeArrayAddition' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["root"]["a"] = 1;
|
|
|
|
$foo["root"] += ["b" => [2, 3]];',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$foo' => 'array{root: array{a: int, b: array{0: int, 1: int}}}',
|
2017-11-19 18:33:43 +01:00
|
|
|
],
|
|
|
|
],
|
2018-12-19 22:15:19 +01:00
|
|
|
'updateStringIntKey1' => [
|
2017-11-19 18:33:43 +01:00
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
$a["a"] = 5;
|
2018-12-19 22:15:19 +01:00
|
|
|
$a[0] = 3;',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a' => 'array{0: int, a: int}',
|
2018-12-19 22:15:19 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKey2' => [
|
|
|
|
'<?php
|
|
|
|
$string = "c";
|
2017-11-19 18:33:43 +01:00
|
|
|
|
|
|
|
$b = [];
|
|
|
|
|
|
|
|
$b[$string] = 5;
|
2018-12-19 22:15:19 +01:00
|
|
|
$b[0] = 3;',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$b' => 'array{0: int, c: int}',
|
2018-12-19 22:15:19 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKey3' => [
|
|
|
|
'<?php
|
|
|
|
$string = "c";
|
2017-11-19 18:33:43 +01:00
|
|
|
|
|
|
|
$c = [];
|
|
|
|
|
|
|
|
$c[0] = 3;
|
2018-12-19 22:15:19 +01:00
|
|
|
$c[$string] = 5;',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$c' => 'array{0: int, c: int}',
|
2018-12-19 22:15:19 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKey4' => [
|
|
|
|
'<?php
|
|
|
|
$int = 5;
|
2017-11-19 18:33:43 +01:00
|
|
|
|
|
|
|
$d = [];
|
|
|
|
|
|
|
|
$d[$int] = 3;
|
2018-12-19 22:15:19 +01:00
|
|
|
$d["a"] = 5;',
|
|
|
|
'assertions' => [
|
|
|
|
'$d' => 'non-empty-array<int|string, int>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKey5' => [
|
|
|
|
'<?php
|
|
|
|
$string = "c";
|
|
|
|
$int = 5;
|
2017-11-19 18:33:43 +01:00
|
|
|
|
|
|
|
$e = [];
|
|
|
|
|
|
|
|
$e[$int] = 3;
|
|
|
|
$e[$string] = 5;',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$e' => 'non-empty-array<int|string, int>',
|
2017-11-19 18:33:43 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKeyWithIntRootAndNumberOffset' => [
|
|
|
|
'<?php
|
|
|
|
$string = "c";
|
|
|
|
$int = 5;
|
|
|
|
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
$a[0]["a"] = 5;
|
|
|
|
$a[0][0] = 3;',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a' => 'array{0: array{0: int, a: int}}',
|
2017-11-19 18:33:43 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKeyWithIntRoot' => [
|
|
|
|
'<?php
|
|
|
|
$string = "c";
|
|
|
|
$int = 5;
|
|
|
|
|
|
|
|
$b = [];
|
|
|
|
|
|
|
|
$b[0][$string] = 5;
|
|
|
|
$b[0][0] = 3;
|
|
|
|
|
|
|
|
$c = [];
|
|
|
|
|
|
|
|
$c[0][0] = 3;
|
|
|
|
$c[0][$string] = 5;
|
|
|
|
|
|
|
|
$d = [];
|
|
|
|
|
|
|
|
$d[0][$int] = 3;
|
|
|
|
$d[0]["a"] = 5;
|
|
|
|
|
|
|
|
$e = [];
|
|
|
|
|
|
|
|
$e[0][$int] = 3;
|
|
|
|
$e[0][$string] = 5;',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$b' => 'array{0: non-empty-array<int|string, int>}',
|
2019-09-08 16:23:12 +02:00
|
|
|
'$c' => 'array{0: non-empty-array<int|string, int>}',
|
|
|
|
'$d' => 'array{0: non-empty-array<int|string, int>}',
|
2019-10-17 07:14:33 +02:00
|
|
|
'$e' => 'array{0: non-empty-array<int|string, int>}',
|
2017-11-19 18:33:43 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKeyWithObjectLikeRootAndNumberOffset' => [
|
|
|
|
'<?php
|
|
|
|
$string = "c";
|
|
|
|
$int = 5;
|
|
|
|
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
$a["root"]["a"] = 5;
|
|
|
|
$a["root"][0] = 3;',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a' => 'array{root: array{0: int, a: int}}',
|
2017-11-19 18:33:43 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKeyWithObjectLikeRoot' => [
|
|
|
|
'<?php
|
|
|
|
$string = "c";
|
|
|
|
$int = 5;
|
|
|
|
|
|
|
|
$b = [];
|
|
|
|
|
|
|
|
$b["root"][$string] = 5;
|
|
|
|
$b["root"][0] = 3;
|
|
|
|
|
|
|
|
$c = [];
|
|
|
|
|
|
|
|
$c["root"][0] = 3;
|
|
|
|
$c["root"][$string] = 5;
|
|
|
|
|
|
|
|
$d = [];
|
|
|
|
|
|
|
|
$d["root"][$int] = 3;
|
|
|
|
$d["root"]["a"] = 5;
|
|
|
|
|
|
|
|
$e = [];
|
|
|
|
|
|
|
|
$e["root"][$int] = 3;
|
|
|
|
$e["root"][$string] = 5;',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$b' => 'array{root: non-empty-array<int|string, int>}',
|
2019-09-08 16:23:12 +02:00
|
|
|
'$c' => 'array{root: non-empty-array<int|string, int>}',
|
|
|
|
'$d' => 'array{root: non-empty-array<int|string, int>}',
|
2019-10-17 07:14:33 +02:00
|
|
|
'$e' => 'array{root: non-empty-array<int|string, int>}',
|
2017-11-19 18:33:43 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'mixedArrayAssignmentWithStringKeys' => [
|
|
|
|
'<?php
|
2018-12-08 19:18:55 +01:00
|
|
|
function foo(array $a) : array {
|
|
|
|
$a["b"]["c"] = 5;
|
|
|
|
echo $a["b"]["d"];
|
|
|
|
echo $a["a"];
|
|
|
|
return $a;
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2017-11-19 18:33:43 +01:00
|
|
|
'error_levels' => ['MixedArrayAssignment', 'MixedArrayAccess', 'MixedArgument'],
|
|
|
|
],
|
2017-11-19 19:42:48 +01:00
|
|
|
'mixedArrayCoercion' => [
|
|
|
|
'<?php
|
|
|
|
/** @param int[] $arg */
|
2018-01-11 21:50:45 +01:00
|
|
|
function expect_int_array($arg): void { }
|
2017-11-19 19:42:48 +01:00
|
|
|
/** @return array */
|
|
|
|
function generic_array() { return []; }
|
|
|
|
|
|
|
|
expect_int_array(generic_array());
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function expect_int(int $arg): void {}
|
2017-11-19 19:42:48 +01:00
|
|
|
/** @return mixed */
|
|
|
|
function return_mixed() { return 2; }
|
|
|
|
expect_int(return_mixed());',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedTypeCoercion', 'MixedArgument'],
|
|
|
|
],
|
2017-11-19 20:58:48 +01:00
|
|
|
'suppressMixedObjectOffset' => [
|
|
|
|
'<?php
|
2018-01-11 21:50:45 +01:00
|
|
|
function getThings(): array {
|
2017-11-19 20:58:48 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr = [];
|
|
|
|
|
|
|
|
foreach (getThings() as $a) {
|
|
|
|
$arr[$a->id] = $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $arr[0];',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedAssignment', 'MixedPropertyFetch', 'MixedArrayOffset', 'MixedArgument'],
|
|
|
|
],
|
2017-11-20 03:24:29 +01:00
|
|
|
'changeObjectLikeType' => [
|
|
|
|
'<?php
|
2017-11-20 04:28:52 +01:00
|
|
|
$a = ["b" => "c"];
|
|
|
|
$a["d"] = ["e" => "f"];
|
2017-11-20 03:24:29 +01:00
|
|
|
$a["b"] = 4;
|
2017-11-20 04:28:52 +01:00
|
|
|
$a["d"]["e"] = 5;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a[\'b\']' => 'int',
|
2019-06-16 15:42:34 +02:00
|
|
|
'$a[\'d\']' => 'array{e: int}',
|
2017-11-20 04:28:52 +01:00
|
|
|
'$a[\'d\'][\'e\']' => 'int',
|
2019-06-16 15:42:34 +02:00
|
|
|
'$a' => 'array{b: int, d: array{e: int}}',
|
2017-11-20 04:28:52 +01:00
|
|
|
],
|
2017-11-20 03:24:29 +01:00
|
|
|
],
|
2017-11-20 05:25:14 +01:00
|
|
|
'changeObjectLikeTypeInIf' => [
|
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (rand(0, 5) > 3) {
|
|
|
|
$a["b"] = new stdClass;
|
|
|
|
} else {
|
|
|
|
$a["b"] = ["e" => "f"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a["b"] instanceof stdClass) {
|
|
|
|
$a["b"] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$a["b"]["e"] = "d";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$a' => 'array{b: array{e: string}}',
|
|
|
|
'$a[\'b\']' => 'array{e: string}',
|
2017-11-20 05:25:14 +01:00
|
|
|
'$a[\'b\'][\'e\']' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2017-11-20 17:49:26 +01:00
|
|
|
'implementsArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
class A implements \ArrayAccess {
|
|
|
|
/**
|
|
|
|
* @param string|int $offset
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2018-01-11 21:50:45 +01:00
|
|
|
public function offsetSet($offset, $value): void {}
|
2017-11-20 17:49:26 +01:00
|
|
|
|
|
|
|
/** @param string|int $offset */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function offsetExists($offset): bool {
|
2017-11-20 17:49:26 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param string|int $offset */
|
2018-01-11 21:50:45 +01:00
|
|
|
public function offsetUnset($offset): void {}
|
2017-11-20 17:49:26 +01:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @param string $offset
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-11-20 17:49:26 +01:00
|
|
|
public function offsetGet($offset) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
$a["bar"] = "cool";
|
|
|
|
$a["bar"]->foo();',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'A',
|
|
|
|
],
|
|
|
|
'error_levels' => ['MixedMethodCall'],
|
|
|
|
],
|
2019-01-04 14:37:54 +01:00
|
|
|
'implementsArrayAccessInheritingDocblock' => [
|
|
|
|
'<?php
|
|
|
|
class A implements \ArrayAccess
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<string, mixed>
|
|
|
|
*/
|
|
|
|
protected $data = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, mixed> $data
|
|
|
|
*/
|
|
|
|
public function __construct(array $data = [])
|
|
|
|
{
|
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $offset
|
|
|
|
*/
|
|
|
|
public function offsetExists($offset): bool
|
|
|
|
{
|
|
|
|
return isset($this->data[$offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $offset
|
|
|
|
*/
|
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
|
|
|
return $this->data[$offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $offset
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value): void
|
|
|
|
{
|
|
|
|
$this->data[$offset] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $offset
|
|
|
|
*/
|
|
|
|
public function offsetUnset($offset): void
|
|
|
|
{
|
|
|
|
unset($this->data[$offset]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value): void
|
|
|
|
{
|
|
|
|
echo "some log";
|
|
|
|
$this->data[$offset] = $value;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2020-03-24 22:59:48 +01:00
|
|
|
'error_levels' => ['MixedAssignment', 'MixedReturnStatement'],
|
2019-01-04 14:37:54 +01:00
|
|
|
],
|
2017-11-20 22:19:40 +01:00
|
|
|
'assignToNullDontDie' => [
|
|
|
|
'<?php
|
|
|
|
$a = null;
|
|
|
|
$a[0][] = 1;',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$a' => 'array{0: non-empty-list<int>}',
|
2017-11-20 22:19:40 +01:00
|
|
|
],
|
|
|
|
'error_levels' => ['PossiblyNullArrayAssignment'],
|
|
|
|
],
|
2017-11-20 23:10:05 +01:00
|
|
|
'stringAssignment' => [
|
|
|
|
'<?php
|
|
|
|
$str = "hello";
|
|
|
|
$str[0] = "i";',
|
|
|
|
'assertions' => [
|
|
|
|
'$str' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2017-11-27 18:01:23 +01:00
|
|
|
'ignoreInvalidArrayOffset' => [
|
|
|
|
'<?php
|
|
|
|
$a = [
|
|
|
|
"b" => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
$a["b"]["c"] = 0;
|
|
|
|
|
|
|
|
foreach ([1, 2, 3] as $i) {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress InvalidArrayOffset
|
|
|
|
* @psalm-suppress MixedOperand
|
2018-05-05 22:17:54 +02:00
|
|
|
* @psalm-suppress PossiblyUndefinedArrayOffset
|
2017-11-27 18:01:23 +01:00
|
|
|
*/
|
|
|
|
$a["b"]["d"] += $a["b"][$i];
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
],
|
2017-12-19 00:47:17 +01:00
|
|
|
'keyedIntOffsetArrayValues' => [
|
|
|
|
'<?php
|
|
|
|
$a = ["hello", 5];
|
|
|
|
$a_values = array_values($a);
|
|
|
|
$a_keys = array_keys($a);',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$a' => 'array{0: string, 1: int}',
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a_values' => 'non-empty-list<int|string>',
|
2019-10-09 00:44:46 +02:00
|
|
|
'$a_keys' => 'list<int>',
|
2017-12-19 00:47:17 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'changeIntOffsetKeyValuesWithDirectAssignment' => [
|
|
|
|
'<?php
|
|
|
|
$b = ["hello", 5];
|
|
|
|
$b[0] = 3;',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$b' => 'array{0: int, 1: int}',
|
2017-12-19 00:47:17 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'changeIntOffsetKeyValuesAfterCopy' => [
|
|
|
|
'<?php
|
|
|
|
$b = ["hello", 5];
|
|
|
|
$c = $b;
|
|
|
|
$c[0] = 3;',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$b' => 'array{0: string, 1: int}',
|
|
|
|
'$c' => 'array{0: int, 1: int}',
|
2017-12-19 00:47:17 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'mergeIntOffsetValues' => [
|
|
|
|
'<?php
|
|
|
|
$d = array_merge(["hello", 5], []);
|
|
|
|
$e = array_merge(["hello", 5], ["hello again"]);',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$d' => 'array{0: string, 1: int}',
|
|
|
|
'$e' => 'array{0: string, 1: int, 2: string}',
|
2017-12-19 00:47:17 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'addIntOffsetToEmptyArray' => [
|
|
|
|
'<?php
|
|
|
|
$f = [];
|
|
|
|
$f[0] = "hello";',
|
|
|
|
'assertions' => [
|
2019-06-16 15:42:34 +02:00
|
|
|
'$f' => 'array{0: string}',
|
2017-12-19 00:47:17 +01:00
|
|
|
],
|
|
|
|
],
|
2018-01-20 17:48:16 +01:00
|
|
|
'assignArrayOrSetNull' => [
|
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a[] = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$a) {
|
|
|
|
$a = null;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$a' => 'non-empty-list<int>|null',
|
2018-01-20 17:48:16 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'assignArrayOrSetNullInElseIf' => [
|
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a[] = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {
|
|
|
|
} elseif (rand(0, 1)) {
|
|
|
|
$a = null;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$a' => 'list<int>|null',
|
2018-01-20 17:48:16 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'assignArrayOrSetNullInElse' => [
|
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$a[] = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($a) {
|
|
|
|
} else {
|
|
|
|
$a = null;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2019-10-09 00:44:46 +02:00
|
|
|
'$a' => 'non-empty-list<int>|null',
|
2018-01-20 17:48:16 +01:00
|
|
|
],
|
|
|
|
],
|
2018-02-09 05:22:29 +01:00
|
|
|
'mixedMethodCallArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
function foo(object $obj) : array {
|
|
|
|
$ret = [];
|
|
|
|
$ret["a"][$obj->foo()] = 1;
|
|
|
|
return $ret["a"];
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2019-01-05 06:15:53 +01:00
|
|
|
'error_levels' => ['MixedMethodCall', 'MixedArrayOffset', 'MixedTypeCoercion'],
|
2018-02-09 05:22:29 +01:00
|
|
|
],
|
2018-02-17 18:32:19 +01:00
|
|
|
'mixedAccessNestedKeys' => [
|
|
|
|
'<?php
|
|
|
|
function takesString(string $s) : void {}
|
|
|
|
function updateArray(array $arr) : array {
|
|
|
|
foreach ($arr as $i => $item) {
|
|
|
|
$arr[$i]["a"]["b"] = 5;
|
|
|
|
$arr[$i]["a"]["c"] = takesString($arr[$i]["a"]["c"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedArrayAccess', 'MixedAssignment', 'MixedArrayOffset', 'MixedArrayAssignment', 'MixedArgument',
|
|
|
|
],
|
|
|
|
],
|
2018-03-17 22:35:36 +01:00
|
|
|
'possiblyUndefinedArrayAccessWithIsset' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0,1)) {
|
|
|
|
$a = ["a" => 1];
|
|
|
|
} else {
|
|
|
|
$a = [2, 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($a[0])) {
|
|
|
|
echo $a[0];
|
|
|
|
}',
|
|
|
|
],
|
2018-03-18 00:03:46 +01:00
|
|
|
'possiblyUndefinedArrayAccessWithArrayKeyExists' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0,1)) {
|
|
|
|
$a = ["a" => 1];
|
|
|
|
} else {
|
|
|
|
$a = [2, 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists(0, $a)) {
|
|
|
|
echo $a[0];
|
|
|
|
}',
|
|
|
|
],
|
2018-03-20 00:14:55 +01:00
|
|
|
'noCrashOnArrayKeyExistsBracket' => [
|
|
|
|
'<?php
|
|
|
|
class MyCollection {
|
|
|
|
/**
|
|
|
|
* @param int $commenter
|
|
|
|
* @param int $numToGet
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getPosters($commenter, $numToGet=10) {
|
|
|
|
$posters = array();
|
|
|
|
$count = 0;
|
|
|
|
$a = new ArrayObject([[1234]]);
|
|
|
|
$iter = $a->getIterator();
|
|
|
|
while ($iter->valid() && $count < $numToGet) {
|
|
|
|
$value = $iter->current();
|
|
|
|
if ($value[0] != $commenter) {
|
|
|
|
if (!array_key_exists($value[0], $posters)) {
|
|
|
|
$posters[$value[0]] = 1;
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$iter->next();
|
|
|
|
}
|
|
|
|
return array_keys($posters);
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => [
|
|
|
|
'MixedArrayAccess', 'MixedAssignment', 'MixedArrayOffset',
|
2018-07-05 16:11:04 +02:00
|
|
|
'MixedArgument', 'MixedTypeCoercion',
|
2018-03-20 00:14:55 +01:00
|
|
|
],
|
|
|
|
],
|
2018-05-03 02:10:08 +02:00
|
|
|
'accessArrayAfterSuppressingBugs' => [
|
|
|
|
'<?php
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
foreach (["one", "two", "three"] as $key) {
|
|
|
|
/**
|
2018-12-20 02:01:27 +01:00
|
|
|
* @psalm-suppress MixedOperand
|
2018-05-03 02:10:08 +02:00
|
|
|
*/
|
2019-12-11 23:08:02 +01:00
|
|
|
$a[$key] += rand(0, 10);
|
2018-05-03 02:10:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$a["four"] = true;
|
|
|
|
|
|
|
|
if ($a["one"]) {}',
|
|
|
|
],
|
2018-05-30 13:42:00 +02:00
|
|
|
'noDuplicateImplicitIntArrayKey' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [1 => 0, 1, 2, 3];
|
2018-09-17 18:15:45 +02:00
|
|
|
$arr = [1 => "one", 2 => "two", "three"];',
|
2019-01-08 15:56:54 +01:00
|
|
|
],
|
|
|
|
'noDuplicateImplicitIntArrayKeyLargeOffset' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [
|
|
|
|
48 => "A",
|
|
|
|
95 => "a", "b",
|
|
|
|
];',
|
2018-05-30 13:42:00 +02:00
|
|
|
],
|
2018-05-31 22:49:01 +02:00
|
|
|
'constArrayAssignment' => [
|
|
|
|
'<?php
|
|
|
|
const BAR = 2;
|
|
|
|
$arr = [1 => 2];
|
|
|
|
$arr[BAR] = [6];
|
|
|
|
$bar = $arr[BAR][0];',
|
|
|
|
],
|
2018-08-21 04:11:01 +02:00
|
|
|
'castToArray' => [
|
|
|
|
'<?php
|
|
|
|
$a = (array) (rand(0, 1) ? [1 => "one"] : 0);
|
|
|
|
$b = (array) null;',
|
|
|
|
'assertions' => [
|
2019-10-17 07:14:33 +02:00
|
|
|
'$a' => 'array{0?: int, 1?: string}',
|
2018-08-21 04:11:01 +02:00
|
|
|
'$b' => 'array<empty, empty>',
|
|
|
|
],
|
2018-08-21 06:28:39 +02:00
|
|
|
],
|
2020-01-11 17:42:09 +01:00
|
|
|
'coerceListToArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param list<int> $_bar
|
|
|
|
*/
|
|
|
|
function foo(array $_bar) : void {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param list<int> $bar
|
|
|
|
*/
|
|
|
|
function baz(array $bar) : void { foo((array) $bar); }',
|
|
|
|
],
|
2018-08-21 06:28:39 +02:00
|
|
|
'getOnCoercedArray' => [
|
|
|
|
'<?php
|
|
|
|
function getArray() : array {
|
|
|
|
return rand(0, 1) ? ["attr" => []] : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$out = getArray();
|
|
|
|
$out["attr"] = (array) ($out["attr"] ?? []);
|
|
|
|
$out["attr"]["bar"] = 1;',
|
|
|
|
'assertions' => [
|
2019-03-23 19:27:54 +01:00
|
|
|
'$out[\'attr\'][\'bar\']' => 'int',
|
2018-08-21 06:28:39 +02:00
|
|
|
],
|
|
|
|
],
|
2018-12-08 19:18:55 +01:00
|
|
|
'arrayAssignmentOnMixedArray' => [
|
|
|
|
'<?php
|
|
|
|
function foo(array $arr) : void {
|
|
|
|
$arr["a"] = 1;
|
|
|
|
|
|
|
|
foreach ($arr["b"] as $b) {}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'error_levels' => ['MixedAssignment'],
|
|
|
|
],
|
2019-03-16 16:15:25 +01:00
|
|
|
'implementsArrayAccessAllowNullOffset' => [
|
2019-01-31 18:45:47 +01:00
|
|
|
'<?php
|
|
|
|
/**
|
2019-03-16 16:15:25 +01:00
|
|
|
* @template-implements ArrayAccess<?int, string>
|
2019-01-31 18:45:47 +01:00
|
|
|
*/
|
|
|
|
class C implements ArrayAccess {
|
|
|
|
public function offsetExists(int $offset) : bool { return true; }
|
|
|
|
|
|
|
|
public function offsetGet($offset) : string { return "";}
|
|
|
|
|
|
|
|
public function offsetSet(?int $offset, string $value) : void {}
|
|
|
|
|
|
|
|
public function offsetUnset(int $offset) : void { }
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = new C();
|
|
|
|
$c[] = "hello";',
|
|
|
|
],
|
2019-08-27 16:18:58 +02:00
|
|
|
'addToMixedArray' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param array{key: string} $a
|
|
|
|
*/
|
|
|
|
function foo(array $a): void {
|
|
|
|
echo $a["key"];
|
|
|
|
}
|
|
|
|
|
|
|
|
function bar(array $arr) : void {
|
|
|
|
$arr["key"] = "qqq";
|
|
|
|
foo($arr);
|
|
|
|
}'
|
|
|
|
],
|
2019-08-27 23:00:00 +02:00
|
|
|
'checkEmptinessAfterConditionalArrayAdjustment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public array $arr = [];
|
|
|
|
|
|
|
|
public function foo() : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$this->arr["a"] = "hello";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->arr) {}
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2019-09-08 16:23:12 +02:00
|
|
|
'arrayAssignmentAddsTypePossibilities' => [
|
|
|
|
'<?php
|
|
|
|
function bar(array $value): void {
|
|
|
|
$value["b"] = "hello";
|
|
|
|
$value = $value + ["a" => 0];
|
|
|
|
if (is_int($value["a"])) {}
|
|
|
|
}'
|
|
|
|
],
|
2019-09-20 21:21:38 +02:00
|
|
|
'falseArrayAssignment' => [
|
|
|
|
'<?php
|
|
|
|
function foo(): array {
|
|
|
|
$array = [];
|
|
|
|
$array[false] = "";
|
|
|
|
echo $array[0];
|
|
|
|
return $array;
|
|
|
|
}',
|
|
|
|
],
|
2019-09-25 19:12:29 +02:00
|
|
|
'coercePossiblyNullKeyToZero' => [
|
2019-09-25 18:17:37 +02:00
|
|
|
'<?php
|
|
|
|
function int_or_null(): ?int {
|
|
|
|
return rand(0, 1) !== 0 ? 42 : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<array-key, null>
|
|
|
|
*/
|
|
|
|
function foo(): array {
|
|
|
|
$array = [];
|
|
|
|
/** @psalm-suppress PossiblyNullArrayOffset */
|
|
|
|
$array[int_or_null()] = null;
|
|
|
|
return $array;
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'coerceNullKeyToZero' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @return array<int, null>
|
|
|
|
*/
|
|
|
|
function foo(): array {
|
|
|
|
$array = [];
|
|
|
|
/** @psalm-suppress NullArrayOffset */
|
|
|
|
$array[null] = null;
|
|
|
|
return $array;
|
|
|
|
}'
|
|
|
|
],
|
2019-10-09 00:44:46 +02:00
|
|
|
'listUsedAsArray' => [
|
|
|
|
'<?php
|
|
|
|
function takesArray(array $arr) : void {}
|
|
|
|
|
|
|
|
$a = [];
|
|
|
|
$a[] = 1;
|
|
|
|
$a[] = 2;
|
|
|
|
|
|
|
|
takesArray($a);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'non-empty-list<int>'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'listTakesEmptyArray' => [
|
|
|
|
'<?php
|
|
|
|
/** @param list<int> $arr */
|
|
|
|
function takesList(array $arr) : void {}
|
|
|
|
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
takesList($a);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array<empty, empty>'
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'listCreatedInSingleStatementUsedAsArray' => [
|
|
|
|
'<?php
|
|
|
|
function takesArray(array $arr) : void {}
|
|
|
|
|
|
|
|
/** @param list<int> $arr */
|
|
|
|
function takesList(array $arr) : void {}
|
|
|
|
|
|
|
|
$a = [1, 2];
|
|
|
|
|
|
|
|
takesArray($a);
|
|
|
|
takesList($a);
|
|
|
|
|
|
|
|
$a[] = 3;
|
|
|
|
|
|
|
|
takesArray($a);
|
|
|
|
takesList($a);
|
|
|
|
|
|
|
|
$b = $a;
|
|
|
|
|
|
|
|
$b[] = rand(0, 10);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array{0: int, 1: int, 2: int}',
|
|
|
|
'$b' => 'array{0: int, 1: int, 2: int, 3: int}',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'listMergedWithObjectLikeList' => [
|
|
|
|
'<?php
|
|
|
|
/** @param list<int> $arr */
|
|
|
|
function takesAnotherList(array $arr) : void {}
|
|
|
|
|
|
|
|
/** @param list<int> $arr */
|
|
|
|
function takesList(array $arr) : void {
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
$arr = [1, 2, 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
takesAnotherList($arr);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'listMergedWithObjectLikeListAfterAssertion' => [
|
|
|
|
'<?php
|
|
|
|
/** @param list<int> $arr */
|
|
|
|
function takesAnotherList(array $arr) : void {}
|
|
|
|
|
|
|
|
/** @param list<int> $arr */
|
|
|
|
function takesList(array $arr) : void {
|
|
|
|
if ($arr) {
|
|
|
|
$arr = [4, 5, 6];
|
|
|
|
}
|
|
|
|
|
|
|
|
takesAnotherList($arr);
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'nonEmptyAssertionOnListElement' => [
|
|
|
|
'<?php
|
|
|
|
/** @param list<array<string, string>> $arr */
|
|
|
|
function takesList(array $arr) : void {
|
|
|
|
if (!empty($arr[0])) {
|
|
|
|
foreach ($arr[0] as $k => $v) {}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-10-09 16:04:34 +02:00
|
|
|
'nonEmptyAssignmentToListElement' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param non-empty-list<string> $arr
|
|
|
|
* @return non-empty-list<string>
|
|
|
|
*/
|
|
|
|
function takesList(array $arr) : array {
|
|
|
|
$arr[0] = "food";
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}',
|
|
|
|
],
|
2019-10-10 23:46:20 +02:00
|
|
|
'unpackedArgIsList' => [
|
|
|
|
'<?php
|
|
|
|
final class Values
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @psalm-var list<int>
|
|
|
|
*/
|
|
|
|
private $ints = [];
|
|
|
|
|
|
|
|
public function set(int ...$ints): void {
|
|
|
|
$this->ints = $ints;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2019-11-06 01:03:59 +01:00
|
|
|
'assignStringFirstChar' => [
|
|
|
|
'<?php
|
|
|
|
/** @param non-empty-list<string> $arr */
|
|
|
|
function foo(array $arr) : string {
|
|
|
|
$arr[0][0] = "a";
|
|
|
|
return $arr[0];
|
|
|
|
}'
|
|
|
|
],
|
2019-11-27 08:07:17 +01:00
|
|
|
'arraySpread' => [
|
|
|
|
'<?php
|
|
|
|
$arrayA = [1, 2, 3];
|
|
|
|
$arrayB = [4, 5];
|
|
|
|
$result = [0, ...$arrayA, ...$arrayB, 6 ,7];
|
|
|
|
|
|
|
|
$arr1 = [3 => 1, 1 => 2, 3];
|
|
|
|
$arr2 = [...$arr1];
|
|
|
|
$arr3 = [1 => 0, ...$arr1];',
|
|
|
|
[
|
|
|
|
'$result' => 'array{0: int, 1: int, 2: int, 3: int, 4: int, 5: int, 6: int, 7: int}',
|
|
|
|
'$arr2' => 'array{0: int, 1: int, 2: int}',
|
|
|
|
'$arr3' => 'array{1: int, 2: int, 3: int, 4: int}',
|
|
|
|
]
|
|
|
|
],
|
2019-12-19 00:48:25 +01:00
|
|
|
'listPropertyAssignmentAfterIsset' => [
|
|
|
|
'<?php
|
|
|
|
class Collection {
|
|
|
|
/** @var list<string> */
|
|
|
|
private $list = [];
|
|
|
|
|
|
|
|
public function override(int $offset): void {
|
|
|
|
if (isset($this->list[$offset])) {
|
|
|
|
$this->list[$offset] = "a";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-01-14 16:40:16 +01:00
|
|
|
'propertyAssignmentToObjectLikeIntKeys' => [
|
|
|
|
'<?php
|
|
|
|
class Bar {
|
|
|
|
/** @var array{0: string, 1:string} */
|
|
|
|
private array $baz = ["a", "b"];
|
|
|
|
|
|
|
|
public function append(string $str) : void {
|
|
|
|
$this->baz[rand(0, 1) ? 0 : 1] = $str;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
|
|
|
'propertyAssignmentToObjectLikeStringKeys' => [
|
|
|
|
'<?php
|
|
|
|
class Bar {
|
|
|
|
/** @var array{a: string, b:string} */
|
|
|
|
private array $baz = ["a" => "c", "b" => "d"];
|
|
|
|
|
|
|
|
public function append(string $str) : void {
|
|
|
|
$this->baz[rand(0, 1) ? "a" : "b"] = $str;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2020-02-22 18:12:40 +01:00
|
|
|
'arrayMixedMixedNotAllowedFromObject' => [
|
|
|
|
'<?php
|
|
|
|
function foo(ArrayObject $a) : array {
|
|
|
|
$arr = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
* @psalm-suppress MixedArrayOffset
|
|
|
|
*/
|
|
|
|
foreach ($a as $k => $v) {
|
|
|
|
$arr[$k] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'arrayMixedMixedNotAllowedFromMixed' => [
|
|
|
|
'<?php
|
|
|
|
/** @psalm-suppress MissingParamType */
|
|
|
|
function foo($a) : array {
|
|
|
|
$arr = ["a" => "foo"];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
* @psalm-suppress MixedArrayOffset
|
|
|
|
*/
|
|
|
|
foreach ($a as $k => $v) {
|
|
|
|
$arr[$k] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}',
|
|
|
|
],
|
2020-02-23 02:52:39 +01:00
|
|
|
'assignNestedKey' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedAssignment
|
|
|
|
* @psalm-suppress MixedArrayOffset
|
|
|
|
*
|
|
|
|
* @psalm-return array<true>
|
|
|
|
*/
|
|
|
|
function getAutoComplete(array $data): array {
|
|
|
|
$response = ["s" => []];
|
|
|
|
|
|
|
|
foreach ($data as $suggestion) {
|
|
|
|
$response["s"][$suggestion] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response["s"];
|
|
|
|
}'
|
|
|
|
],
|
2020-02-23 03:13:14 +01:00
|
|
|
'assignArrayUnion' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedArrayOffset
|
|
|
|
*/
|
|
|
|
function foo(array $out) : array {
|
|
|
|
$key = 1;
|
|
|
|
|
|
|
|
if (rand(0, 1)) {
|
|
|
|
/** @var mixed */
|
|
|
|
$key = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out[$key] = 5;
|
|
|
|
return $out;
|
|
|
|
}'
|
2020-04-08 20:07:09 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-24 01:36:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2017-04-25 05:45:02 +02:00
|
|
|
*/
|
2018-11-06 03:57:36 +01:00
|
|
|
public function providerInvalidCodeParse()
|
2017-04-25 05:45:02 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'objectAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
(new A)["b"] = 1;',
|
2019-02-27 03:24:30 +01:00
|
|
|
'error_message' => 'UndefinedMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'invalidArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
$a = 5;
|
|
|
|
$a[0] = 5;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidArrayAssignment',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2018-03-17 22:35:36 +01:00
|
|
|
'possiblyUndefinedArrayAccess' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0,1)) {
|
|
|
|
$a = ["a" => 1];
|
|
|
|
} else {
|
|
|
|
$a = [2, 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $a[0];',
|
2018-04-17 20:06:27 +02:00
|
|
|
'error_message' => 'PossiblyUndefinedArrayOffset',
|
2018-03-17 22:35:36 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'mixedStringOffsetAssignment' => [
|
|
|
|
'<?php
|
|
|
|
/** @var mixed */
|
|
|
|
$a = 5;
|
|
|
|
"hello"[0] = $a;',
|
|
|
|
'error_message' => 'MixedStringOffsetAssignment',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_level' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'mixedArrayArgument' => [
|
|
|
|
'<?php
|
|
|
|
/** @param array<mixed, int|string> $foo */
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(array $foo): void { }
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function barBar(array $bar): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
fooFoo($bar);
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
barBar([1, "2"]);',
|
2019-04-26 00:02:19 +02:00
|
|
|
'error_message' => 'MixedArgumentTypeCoercion',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_level' => ['MixedAssignment'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'arrayPropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string[] */
|
|
|
|
public $strs = ["a", "b", "c"];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function bar() {
|
|
|
|
$this->strs = [new stdClass()]; // no issue emitted
|
|
|
|
}
|
|
|
|
}',
|
2018-01-11 23:38:24 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'incrementalArrayPropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var string[] */
|
|
|
|
public $strs = ["a", "b", "c"];
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
public function bar() {
|
|
|
|
$this->strs[] = new stdClass(); // no issue emitted
|
|
|
|
}
|
|
|
|
}',
|
2018-01-11 23:38:24 +01:00
|
|
|
'error_message' => 'InvalidPropertyAssignmentValue',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2018-03-18 00:03:46 +01:00
|
|
|
'possiblyUndefinedArrayAccessWithArrayKeyExistsOnWrongKey' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0,1)) {
|
|
|
|
$a = ["a" => 1];
|
|
|
|
} else {
|
|
|
|
$a = [2, 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists("a", $a)) {
|
|
|
|
echo $a[0];
|
|
|
|
}',
|
2018-04-17 20:06:27 +02:00
|
|
|
'error_message' => 'PossiblyUndefinedArrayOffset',
|
2018-03-18 00:03:46 +01:00
|
|
|
],
|
|
|
|
'possiblyUndefinedArrayAccessWithArrayKeyExistsOnMissingKey' => [
|
|
|
|
'<?php
|
|
|
|
if (rand(0,1)) {
|
|
|
|
$a = ["a" => 1];
|
|
|
|
} else {
|
|
|
|
$a = [2, 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists("b", $a)) {
|
|
|
|
echo $a[0];
|
|
|
|
}',
|
2018-04-17 20:06:27 +02:00
|
|
|
'error_message' => 'PossiblyUndefinedArrayOffset',
|
2018-03-18 00:03:46 +01:00
|
|
|
],
|
2018-05-30 13:42:00 +02:00
|
|
|
'duplicateStringArrayKey' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [
|
|
|
|
"a" => 1,
|
|
|
|
"b" => 2,
|
|
|
|
"c" => 3,
|
|
|
|
"c" => 4,
|
|
|
|
];',
|
|
|
|
'error_message' => 'DuplicateArrayKey',
|
|
|
|
],
|
|
|
|
'duplicateIntArrayKey' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [
|
|
|
|
0 => 1,
|
|
|
|
1 => 2,
|
|
|
|
2 => 3,
|
|
|
|
2 => 4,
|
|
|
|
];',
|
|
|
|
'error_message' => 'DuplicateArrayKey',
|
|
|
|
],
|
|
|
|
'duplicateImplicitIntArrayKey' => [
|
|
|
|
'<?php
|
|
|
|
$arr = [
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
2 => 4,
|
|
|
|
];',
|
|
|
|
'error_message' => 'DuplicateArrayKey',
|
|
|
|
],
|
2019-08-27 16:18:58 +02:00
|
|
|
'mixedArrayAssignmentOnVariable' => [
|
2018-08-21 06:28:39 +02:00
|
|
|
'<?php
|
2019-08-27 16:18:58 +02:00
|
|
|
function foo(array $arr) : void {
|
|
|
|
$arr["foo"][0] = "5";
|
|
|
|
}',
|
2018-08-21 06:28:39 +02:00
|
|
|
'error_message' => 'MixedArrayAssignment',
|
|
|
|
],
|
2019-03-16 05:31:17 +01:00
|
|
|
'implementsArrayAccessPreventNullOffset' => [
|
2019-01-31 18:45:47 +01:00
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @template-implements ArrayAccess<int, string>
|
|
|
|
*/
|
|
|
|
class C implements ArrayAccess {
|
|
|
|
public function offsetExists(int $offset) : bool { return true; }
|
|
|
|
|
|
|
|
public function offsetGet($offset) : string { return "";}
|
|
|
|
|
|
|
|
public function offsetSet(int $offset, string $value) : void {}
|
|
|
|
|
|
|
|
public function offsetUnset(int $offset) : void { }
|
|
|
|
}
|
|
|
|
|
|
|
|
$c = new C();
|
|
|
|
$c[] = "hello";',
|
2019-02-07 21:21:34 +01:00
|
|
|
'error_message' => 'NullArgument',
|
|
|
|
],
|
|
|
|
'storageKeyMustBeObject' => [
|
|
|
|
'<?php
|
|
|
|
$key = [1,2,3];
|
|
|
|
$storage = new \SplObjectStorage();
|
|
|
|
$storage[$key] = "test";',
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_message' => 'InvalidArgument',
|
2019-01-31 18:45:47 +01:00
|
|
|
],
|
2019-10-09 00:44:46 +02:00
|
|
|
'listUsedAsArrayWrongType' => [
|
|
|
|
'<?php
|
|
|
|
/** @param string[] $arr */
|
|
|
|
function takesArray(array $arr) : void {}
|
|
|
|
|
|
|
|
$a = [];
|
|
|
|
$a[] = 1;
|
|
|
|
$a[] = 2;
|
|
|
|
|
|
|
|
takesArray($a);',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'listUsedAsArrayWrongListType' => [
|
|
|
|
'<?php
|
|
|
|
/** @param list<string> $arr */
|
|
|
|
function takesArray(array $arr) : void {}
|
|
|
|
|
|
|
|
$a = [];
|
|
|
|
$a[] = 1;
|
|
|
|
$a[] = 2;
|
|
|
|
|
|
|
|
takesArray($a);',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
2019-10-09 16:04:34 +02:00
|
|
|
'nonEmptyAssignmentToListElementChangeType' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @param non-empty-list<string> $arr
|
|
|
|
* @return non-empty-list<string>
|
|
|
|
*/
|
|
|
|
function takesList(array $arr) : array {
|
|
|
|
$arr[0] = 5;
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
2019-11-08 17:56:33 +01:00
|
|
|
'preventArrayAssignmentOnReturnValue' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function foo() : array {
|
|
|
|
return [1, 2, 3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new A)->foo()[3] = 5;',
|
|
|
|
'error_message' => 'InvalidArrayAssignment',
|
|
|
|
],
|
2020-04-08 20:07:09 +02:00
|
|
|
'mergeIntWithMixed' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private static array $cache = [];
|
|
|
|
|
|
|
|
public function getCachedMixed(array $cache, string $locale) : string {
|
|
|
|
if (!isset(self::$cache[$locale])) {
|
|
|
|
$cache[$locale] = 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cache[$locale];
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnStatement',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-24 01:36:51 +01:00
|
|
|
}
|
2016-09-12 06:02:50 +02:00
|
|
|
}
|