2016-09-12 06:02:50 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
use Psalm\Checker\FileChecker;
|
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
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
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-07-25 22:11:02 +02:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
|
|
|
|
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();
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($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-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-22 19:45:47 +02:00
|
|
|
public function testImplementsArrayAccess()
|
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A implements \ArrayAccess {
|
|
|
|
public function offsetSet($offset, $value) : void {
|
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
public function offsetExists($offset) : bool {
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
public function offsetUnset($offset) : void {
|
|
|
|
}
|
2017-01-02 01:09:17 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
public function offsetGet($offset) : int {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 19:45:47 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$a = new A();
|
|
|
|
$a["bar"] = "cool";'
|
|
|
|
);
|
2016-09-22 19:45:47 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker);
|
|
|
|
$file_checker->visitAndAnalyzeMethods();
|
2016-09-22 01:15:09 +02:00
|
|
|
}
|
2016-10-15 19:11:08 +02:00
|
|
|
|
2017-01-20 06:23:58 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerValidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$out' => 'array<int, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$out' => 'array<int, array<int, 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
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
if ($bits) {
|
|
|
|
$out[] = $bits;
|
|
|
|
}',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$out' => 'array<int, array<int, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$out' => 'array<int, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$out' => 'array<int, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$out' => 'array<int, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$out' => 'array<int, int|string>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicitIntArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo[] = "hello";',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array<int, string>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicit2dIntArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo[][] = "hello";',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array<int, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array<int, array<int, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array<int, array<int, array<int, array<int, string>>>>',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implicitIndexedIntArrayCreation' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo[0] = "hello";
|
|
|
|
$foo[1] = "hello";
|
|
|
|
$foo[2] = "hello";
|
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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array<int, string>',
|
|
|
|
'$bar' => 'array<int, int>',
|
|
|
|
'$bat' => '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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array{bar:string}',
|
|
|
|
'$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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array{bar:array{baz:string}}',
|
|
|
|
'$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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array{bar:array{baz:array{bat:string}}}',
|
|
|
|
'$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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array{bar:array{baz:array{bat:array{bap:string}}}}',
|
|
|
|
'$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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array{bar:array{baz:string}}',
|
|
|
|
'$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' => [
|
2017-06-29 16:22:49 +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
|
|
|
],
|
|
|
|
'conflictingTypes' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [
|
|
|
|
"bar" => ["a" => "b"],
|
|
|
|
"baz" => [1]
|
|
|
|
];',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array{bar:array{a:string}, baz:array<int, 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' => [
|
2017-06-29 16:22:49 +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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array{bar:array{a:string, bam:array{baz:string}}, baz:array<int, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$foo' => 'array{a:string, b:array<int, string>}',
|
|
|
|
'$foo[\'a\']' => 'string',
|
|
|
|
'$foo[\'b\']' => 'array<int, string>',
|
|
|
|
'$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' => [
|
2017-06-29 16:22:49 +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' => [
|
2017-06-29 16:22:49 +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' => [
|
2017-06-29 16:22:49 +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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'array<string, array<int, string>>',
|
|
|
|
'$c' => 'array<string, array<string, array<int, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'array<string, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'array<int, string>',
|
|
|
|
'$b' => 'array<int, 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' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$a' => 'array<int, string|int>',
|
|
|
|
'$b' => 'array<int, string|int>',
|
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
|
|
|
],
|
|
|
|
],
|
2017-11-11 20:19:45 +01:00
|
|
|
'objectLikeArrayAddition' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["a"] = 1;
|
|
|
|
$foo += ["b" => [2, 3]];',
|
|
|
|
'assertions' => [
|
|
|
|
'$foo' => 'array{a:int, b:array<int, int>}',
|
|
|
|
],
|
|
|
|
],
|
2017-11-19 18:33:43 +01:00
|
|
|
'nestedObjectLikeArrayAddition' => [
|
|
|
|
'<?php
|
|
|
|
$foo = [];
|
|
|
|
$foo["root"]["a"] = 1;
|
|
|
|
$foo["root"] += ["b" => [2, 3]];',
|
|
|
|
'assertions' => [
|
|
|
|
'$foo' => 'array{root:array{a:int, b:array<int, int>}}',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKey' => [
|
|
|
|
'<?php
|
|
|
|
$string = "c";
|
|
|
|
$int = 5;
|
|
|
|
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
$a["a"] = 5;
|
|
|
|
$a[0] = 3;
|
|
|
|
|
|
|
|
$b = [];
|
|
|
|
|
|
|
|
$b[$string] = 5;
|
|
|
|
$b[0] = 3;
|
|
|
|
|
|
|
|
$c = [];
|
|
|
|
|
|
|
|
$c[0] = 3;
|
|
|
|
$c[$string] = 5;
|
|
|
|
|
|
|
|
$d = [];
|
|
|
|
|
|
|
|
$d[$int] = 3;
|
|
|
|
$d["a"] = 5;
|
|
|
|
|
|
|
|
$e = [];
|
|
|
|
|
|
|
|
$e[$int] = 3;
|
|
|
|
$e[$string] = 5;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array<int|string, int>',
|
|
|
|
'$b' => 'array<string|int, int>',
|
|
|
|
'$c' => 'array<int|string, int>',
|
|
|
|
'$d' => 'array<int|string, int>',
|
|
|
|
'$e' => 'array<int|string, int>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKeyWithIntRootAndNumberOffset' => [
|
|
|
|
'<?php
|
|
|
|
$string = "c";
|
|
|
|
$int = 5;
|
|
|
|
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
$a[0]["a"] = 5;
|
|
|
|
$a[0][0] = 3;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array<int, array<int|string, int>>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'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' => [
|
|
|
|
'$b' => 'array<int, array<string|int, int>>',
|
|
|
|
'$c' => 'array<int, array<int|string, int>>',
|
|
|
|
'$d' => 'array<int, array<int|string, int>>',
|
|
|
|
'$e' => 'array<int, array<int|string, int>>',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'updateStringIntKeyWithObjectLikeRootAndNumberOffset' => [
|
|
|
|
'<?php
|
|
|
|
$string = "c";
|
|
|
|
$int = 5;
|
|
|
|
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
$a["root"]["a"] = 5;
|
|
|
|
$a["root"][0] = 3;',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array{root:array<int|string, int>}',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'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' => [
|
|
|
|
'$b' => 'array{root:array<string|int, int>}',
|
|
|
|
'$c' => 'array{root:array<int|string, int>}',
|
|
|
|
'$d' => 'array{root:array<int|string, int>}',
|
|
|
|
'$e' => 'array{root:array<int|string, int>}',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'mixedArrayAssignmentWithStringKeys' => [
|
|
|
|
'<?php
|
|
|
|
/** @var array<mixed, mixed> */
|
|
|
|
$a = [];
|
|
|
|
$a["b"]["c"] = 5;
|
|
|
|
echo $a["b"]["d"];',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'array<mixed, mixed>',
|
|
|
|
],
|
|
|
|
'error_levels' => ['MixedArrayAssignment', 'MixedArrayAccess', 'MixedArgument'],
|
|
|
|
],
|
2017-11-19 19:42:48 +01:00
|
|
|
'mixedArrayCoercion' => [
|
|
|
|
'<?php
|
|
|
|
/** @param int[] $arg */
|
|
|
|
function expect_int_array($arg) : void { }
|
|
|
|
/** @return array */
|
|
|
|
function generic_array() { return []; }
|
|
|
|
|
|
|
|
expect_int_array(generic_array());
|
|
|
|
|
|
|
|
function expect_int(int $arg) : void {}
|
|
|
|
/** @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
|
|
|
|
function getThings() : array {
|
|
|
|
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',
|
|
|
|
'$a[\'d\']' => 'array{e:int}',
|
|
|
|
'$a[\'d\'][\'e\']' => 'int',
|
|
|
|
'$a' => 'array{b:int, d:array{e:int}}',
|
|
|
|
],
|
2017-11-20 03:24:29 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-24 01:36:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'objectAssignment' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
(new A)["b"] = 1;',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidArrayAssignment',
|
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
|
|
|
],
|
|
|
|
'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 */
|
|
|
|
function fooFoo(array $foo) : void { }
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function barBar(array $bar) : void {
|
|
|
|
fooFoo($bar);
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
barBar([1, "2"]);',
|
2017-11-19 19:42:48 +01:00
|
|
|
'error_message' => 'MixedTypeCoercion',
|
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
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidPropertyAssignment',
|
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
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'InvalidPropertyAssignment',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-02-24 01:36:51 +01:00
|
|
|
}
|
2016-09-12 06:02:50 +02:00
|
|
|
}
|