2016-09-12 06:02:50 +02:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
|
use Psalm\Context;
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
|
|
|
|
|
|
class ArrayAssignmentTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
{
|
|
|
|
|
protected static $_parser;
|
|
|
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
|
{
|
|
|
|
|
self::$_parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
|
|
|
|
|
|
$config = \Psalm\Config::getInstance();
|
|
|
|
|
$config->throw_exception = true;
|
|
|
|
|
$config->use_docblock_types = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
|
{
|
|
|
|
|
\Psalm\Checker\FileChecker::clearCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImplicitIntArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo[] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('array<int,string>', (string) $context->vars_in_scope['$foo']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImplicit2DIntArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo[][] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('array<int,array<int,string>>', (string) $context->vars_in_scope['$foo']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImplicit3DIntArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo[][][] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('array<int,array<int,array<int,string>>>', (string) $context->vars_in_scope['$foo']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImplicit4DIntArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo[][][][] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('array<int,array<int,array<int,array<int,string>>>>', (string) $context->vars_in_scope['$foo']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 20:21:51 +02:00
|
|
|
|
public function testImplicitIndexedIntArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo[0] = "hello";
|
|
|
|
|
$foo[1] = "hello";
|
|
|
|
|
$foo[2] = "hello";
|
|
|
|
|
|
|
|
|
|
$bar = [0, 1, 2];
|
|
|
|
|
|
|
|
|
|
$bat = [];
|
|
|
|
|
|
|
|
|
|
foreach ($foo as $i => $text) {
|
|
|
|
|
$bat[$text] = $bar[$i];
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('array<int,string>', (string) $context->vars_in_scope['$foo']);
|
|
|
|
|
$this->assertEquals('array<int,int>', (string) $context->vars_in_scope['$bar']);
|
|
|
|
|
$this->assertEquals('array<string,int>', (string) $context->vars_in_scope['$bat']);
|
2016-09-15 20:21:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public function testImplicitStringArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["bar"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{bar:string}', (string) $context->vars_in_scope['$foo']);
|
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'bar\']']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImplicit2DStringArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["bar"]["baz"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2016-09-21 03:45:49 +02:00
|
|
|
|
// check array access of baz on foo
|
|
|
|
|
// with some extra data – if we need to create an array for type $foo["bar"],
|
|
|
|
|
|
2016-09-12 06:02:50 +02:00
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{bar:object-like{baz:string}}', (string) $context->vars_in_scope['$foo']);
|
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'bar\'][\'baz\']']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImplicit3DStringArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["bar"]["baz"]["bat"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{bar:object-like{baz:object-like{bat:string}}}', (string) $context->vars_in_scope['$foo']);
|
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'bar\'][\'baz\'][\'bat\']']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImplicit4DStringArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["bar"]["baz"]["bat"]["bap"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{bar:object-like{baz:object-like{bat:object-like{bap:string}}}}', (string) $context->vars_in_scope['$foo']);
|
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'bar\'][\'baz\'][\'bat\'][\'bap\']']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
2016-09-13 01:31:16 +02:00
|
|
|
|
|
2016-10-03 04:01:01 +02:00
|
|
|
|
public function test2Step2DStringArrayCreation()
|
2016-09-13 01:31:16 +02:00
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = ["bar" => []];
|
|
|
|
|
$foo["bar"]["baz"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{bar:object-like{baz:string}}', (string) $context->vars_in_scope['$foo']);
|
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'bar\'][\'baz\']']);
|
2016-09-13 01:31:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-03 04:01:01 +02:00
|
|
|
|
public function test2StepImplicit3DStringArrayCreation()
|
2016-09-13 01:31:16 +02:00
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = ["bar" => []];
|
|
|
|
|
$foo["bar"]["baz"]["bat"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{bar:object-like{baz:object-like{bat:string}}}', (string) $context->vars_in_scope['$foo']);
|
2016-09-13 01:31:16 +02:00
|
|
|
|
}
|
2016-09-13 01:44:46 +02:00
|
|
|
|
|
|
|
|
|
public function testConflictingTypes()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [
|
|
|
|
|
"bar" => ["a" => "b"],
|
|
|
|
|
"baz" => [1]
|
|
|
|
|
];
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{bar:object-like{a:string},baz:array<int,int>}', (string) $context->vars_in_scope['$foo']);
|
2016-10-03 04:01:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImplicitObjectLikeCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [
|
|
|
|
|
"bar" => 1,
|
|
|
|
|
];
|
|
|
|
|
$foo["baz"] = "a";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{bar:int,baz:string}', (string) $context->vars_in_scope['$foo']);
|
2016-09-13 01:44:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConflictingTypesWithAssignment()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [
|
|
|
|
|
"bar" => ["a" => "b"],
|
|
|
|
|
"baz" => [1]
|
|
|
|
|
];
|
|
|
|
|
$foo["bar"]["bam"]["baz"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{bar:object-like{a:string,bam:object-like{baz:string}},baz:array<int,int>}', (string) $context->vars_in_scope['$foo']);
|
2016-09-13 01:44:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 06:18:41 +02:00
|
|
|
|
public function testConflictingTypesWithAssignment2()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["a"] = "hello";
|
|
|
|
|
$foo["b"][] = "goodbye";
|
2016-09-22 01:15:09 +02:00
|
|
|
|
$bar = $foo["a"];
|
2016-09-13 06:18:41 +02:00
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{a:string,b:array<int,string>}', (string) $context->vars_in_scope['$foo']);
|
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'a\']']);
|
|
|
|
|
$this->assertEquals('array<int,string>', (string) $context->vars_in_scope['$foo[\'b\']']);
|
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$bar']);
|
2016-09-13 06:18:41 +02:00
|
|
|
|
}
|
2016-09-13 01:44:46 +02:00
|
|
|
|
|
2016-09-13 06:18:41 +02:00
|
|
|
|
public function testConflictingTypesWithAssignment3()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["a"] = "hello";
|
|
|
|
|
$foo["b"]["c"]["d"] = "goodbye";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{a:string,b:object-like{c:object-like{d:string}}}', (string) $context->vars_in_scope['$foo']);
|
2016-09-13 06:18:41 +02:00
|
|
|
|
}
|
2016-09-22 01:15:09 +02:00
|
|
|
|
|
2016-10-03 06:44:05 +02:00
|
|
|
|
public function testNestedObjectLikeAssignment()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["a"]["b"] = "hello";
|
|
|
|
|
$foo["a"]["c"] = 1;
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{a:object-like{b:string,c:int}}', (string) $context->vars_in_scope['$foo']);
|
2016-10-03 06:44:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConditionalObjectLikeAssignment()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
$foo = ["a" => "hello"];
|
|
|
|
|
if (rand(0, 10) === 5) {
|
|
|
|
|
$foo["b"] = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$foo["b"] = 2;
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('object-like{a:string,b:int}', (string) $context->vars_in_scope['$foo']);
|
2016-10-03 06:44:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-22 01:15:09 +02:00
|
|
|
|
public function testIssetKeyedOffset()
|
|
|
|
|
{
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker(
|
|
|
|
|
'somefile.php',
|
|
|
|
|
self::$_parser->parse('<?php
|
|
|
|
|
if (!isset($foo["a"])) {
|
|
|
|
|
$foo["a"] = "hello";
|
|
|
|
|
}
|
|
|
|
|
')
|
|
|
|
|
);
|
|
|
|
|
$context = new Context('somefile.php');
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$context->vars_in_scope['$foo'] = \Psalm\Type::getArray();
|
2016-09-22 01:15:09 +02:00
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('mixed', (string) $context->vars_in_scope['$foo[\'a\']']);
|
2016-09-22 18:26:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConditionalAssignment()
|
|
|
|
|
{
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker(
|
|
|
|
|
'somefile.php',
|
|
|
|
|
self::$_parser->parse('<?php
|
|
|
|
|
if ($b) {
|
|
|
|
|
$foo["a"] = "hello";
|
|
|
|
|
}
|
|
|
|
|
')
|
|
|
|
|
);
|
|
|
|
|
$context = new Context('somefile.php');
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$context->vars_in_scope['$b'] = \Psalm\Type::getBool();
|
|
|
|
|
$context->vars_in_scope['$foo'] = \Psalm\Type::getArray();
|
2016-09-22 18:26:24 +02:00
|
|
|
|
$file_checker->check(true, true, $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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testImplementsArrayAccess()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$_parser->parse('<?php
|
|
|
|
|
class A implements \ArrayAccess { }
|
|
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
$a["bar"] = "cool";
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('A', (string) $context->vars_in_scope['$a']);
|
|
|
|
|
$this->assertFalse(isset($context->vars_in_scope['$a[\'bar\']']));
|
2016-09-22 01:15:09 +02:00
|
|
|
|
}
|
2016-10-15 19:11:08 +02:00
|
|
|
|
|
|
|
|
|
public function testConditionalCheck()
|
|
|
|
|
{
|
|
|
|
|
$file_checker = new \Psalm\Checker\FileChecker(
|
|
|
|
|
'somefile.php',
|
|
|
|
|
self::$_parser->parse('<?php
|
|
|
|
|
/**
|
|
|
|
|
* @param object-like{b:string} $a
|
|
|
|
|
* @return null|string
|
|
|
|
|
*/
|
|
|
|
|
function foo($a) {
|
|
|
|
|
if ($a["b"]) {
|
|
|
|
|
return $a["b"];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
')
|
|
|
|
|
);
|
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
|
}
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|