2016-09-12 06:02:50 +02:00
|
|
|
|
<?php
|
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
|
use PHPUnit_Framework_TestCase;
|
2017-01-02 21:31:18 +01:00
|
|
|
|
use Psalm\Checker\FileChecker;
|
2016-12-17 04:16:29 +01:00
|
|
|
|
use Psalm\Config;
|
2016-11-02 07:29:00 +01:00
|
|
|
|
use Psalm\Context;
|
2016-12-11 00:24:28 +01:00
|
|
|
|
use Psalm\Type;
|
2016-09-12 06:02:50 +02:00
|
|
|
|
|
|
|
|
|
class ArrayAssignmentTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
{
|
2016-12-14 18:55:23 +01:00
|
|
|
|
/** @var \PhpParser\Parser */
|
2016-11-02 07:29:00 +01:00
|
|
|
|
protected static $parser;
|
2016-09-12 06:02:50 +02:00
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
|
protected $project_checker;
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public function setUp()
|
|
|
|
|
{
|
|
|
|
|
\Psalm\Checker\FileChecker::clearCache();
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
2017-02-01 01:22:05 +01:00
|
|
|
|
$this->project_checker->setConfig(new TestConfig());
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-12-17 00:56:23 +01:00
|
|
|
|
public function testGenericArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
|
|
foreach ([1, 2, 3, 4, 5] as $value) {
|
|
|
|
|
$out[] = 4;
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
$this->assertEquals('array<int, int>', (string) $context->vars_in_scope['$out']);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-12-17 00:56:23 +01:00
|
|
|
|
public function testGeneric2DArrayCreation()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
|
|
foreach ([1, 2, 3, 4, 5] as $value) {
|
|
|
|
|
$out[] = [4];
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
$this->assertEquals('array<int, array<int, int>>', (string) $context->vars_in_scope['$out']);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-12-17 00:56:23 +01:00
|
|
|
|
public function testGeneric2DArrayCreationAddedInIf()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
|
|
$bits = [];
|
|
|
|
|
|
|
|
|
|
foreach ([1, 2, 3, 4, 5] as $value) {
|
|
|
|
|
if (rand(0,100) > 50) {
|
|
|
|
|
$out[] = $bits;
|
|
|
|
|
$bits = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bits[] = 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($bits) {
|
|
|
|
|
$out[] = $bits;
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
$this->assertEquals('array<int, array<int, int>>', (string) $context->vars_in_scope['$out']);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-12-17 00:56:23 +01:00
|
|
|
|
public function testGenericArrayCreationWithObjectAddedInIf()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
class B {}
|
|
|
|
|
|
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
|
|
if (rand(0,10) === 10) {
|
|
|
|
|
$out[] = new B();
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
$this->assertEquals('array<int, B>', (string) $context->vars_in_scope['$out']);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-12-17 00:56:23 +01:00
|
|
|
|
public function testGenericArrayCreationWithElementAddedInSwitch()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
|
|
switch (rand(0,10)) {
|
|
|
|
|
case 5:
|
|
|
|
|
$out[] = 4;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
$this->assertEquals('array<int, int>', (string) $context->vars_in_scope['$out']);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-12-17 00:56:23 +01:00
|
|
|
|
public function testGenericArrayCreationWithElementsAddedInSwitch()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
|
|
switch (rand(0,10)) {
|
|
|
|
|
case 5:
|
|
|
|
|
$out[] = 4;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
|
$out[] = "hello";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
$this->assertEquals('array<int, int|string>', (string) $context->vars_in_scope['$out']);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-12-17 00:56:23 +01:00
|
|
|
|
public function testGenericArrayCreationWithElementsAddedInSwitchWithNothing()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
$out = [];
|
|
|
|
|
|
|
|
|
|
switch (rand(0,10)) {
|
|
|
|
|
case 5:
|
|
|
|
|
$out[] = 4;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
|
$out[] = "hello";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
$this->assertEquals('array<int, int|string>', (string) $context->vars_in_scope['$out']);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public function testImplicitIntArrayCreation()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-12 06:02:50 +02:00
|
|
|
|
$foo = [];
|
|
|
|
|
$foo[] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array<int, string>', (string) $context->vars_in_scope['$foo']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public function testImplicit2DIntArrayCreation()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-12 06:02:50 +02:00
|
|
|
|
$foo = [];
|
|
|
|
|
$foo[][] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array<int, array<int, string>>', (string) $context->vars_in_scope['$foo']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public function testImplicit3DIntArrayCreation()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-12 06:02:50 +02:00
|
|
|
|
$foo = [];
|
|
|
|
|
$foo[][][] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array<int, array<int, array<int, string>>>', (string) $context->vars_in_scope['$foo']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public function testImplicit4DIntArrayCreation()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-12 06:02:50 +02:00
|
|
|
|
$foo = [];
|
|
|
|
|
$foo[][][][] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$this->assertEquals(
|
2016-11-13 17:24:25 +01:00
|
|
|
|
'array<int, array<int, array<int, array<int, string>>>>',
|
2016-11-02 07:29:00 +01:00
|
|
|
|
(string) $context->vars_in_scope['$foo']
|
|
|
|
|
);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-15 20:21:51 +02:00
|
|
|
|
public function testImplicitIndexedIntArrayCreation()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-15 20:21:51 +02:00
|
|
|
|
$foo = [];
|
|
|
|
|
$foo[0] = "hello";
|
|
|
|
|
$foo[1] = "hello";
|
|
|
|
|
$foo[2] = "hello";
|
|
|
|
|
|
|
|
|
|
$bar = [0, 1, 2];
|
|
|
|
|
|
|
|
|
|
$bat = [];
|
|
|
|
|
|
|
|
|
|
foreach ($foo as $i => $text) {
|
|
|
|
|
$bat[$text] = $bar[$i];
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01: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
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public function testImplicitStringArrayCreation()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-12 06:02:50 +02:00
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["bar"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-28 19:24:06 +02:00
|
|
|
|
$this->assertEquals('array{bar:string}', (string) $context->vars_in_scope['$foo']);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'bar\']']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public function testImplicit2DStringArrayCreation()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-12 06:02:50 +02:00
|
|
|
|
$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"],
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-28 19:24:06 +02:00
|
|
|
|
$this->assertEquals('array{bar:array{baz:string}}', (string) $context->vars_in_scope['$foo']);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'bar\'][\'baz\']']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public function testImplicit3DStringArrayCreation()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-12 06:02:50 +02:00
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["bar"]["baz"]["bat"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-28 19:24:06 +02:00
|
|
|
|
$this->assertEquals('array{bar:array{baz:array{bat:string}}}', (string) $context->vars_in_scope['$foo']);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'bar\'][\'baz\'][\'bat\']']);
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-12 06:02:50 +02:00
|
|
|
|
public function testImplicit4DStringArrayCreation()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-12 06:02:50 +02:00
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["bar"]["baz"]["bat"]["bap"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'array{bar:array{baz:array{bat:array{bap:string}}}}',
|
|
|
|
|
(string) $context->vars_in_scope['$foo']
|
|
|
|
|
);
|
|
|
|
|
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$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
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-10-03 04:01:01 +02:00
|
|
|
|
public function test2Step2DStringArrayCreation()
|
2016-09-13 01:31:16 +02:00
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-13 01:31:16 +02:00
|
|
|
|
$foo = ["bar" => []];
|
|
|
|
|
$foo["bar"]["baz"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-28 19:24:06 +02:00
|
|
|
|
$this->assertEquals('array{bar:array{baz:string}}', (string) $context->vars_in_scope['$foo']);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'bar\'][\'baz\']']);
|
2016-09-13 01:31:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-10-03 04:01:01 +02:00
|
|
|
|
public function test2StepImplicit3DStringArrayCreation()
|
2016-09-13 01:31:16 +02:00
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-13 01:31:16 +02:00
|
|
|
|
$foo = ["bar" => []];
|
|
|
|
|
$foo["bar"]["baz"]["bat"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-28 19:24:06 +02:00
|
|
|
|
$this->assertEquals('array{bar:array{baz:array{bat:string}}}', (string) $context->vars_in_scope['$foo']);
|
2016-09-13 01:31:16 +02:00
|
|
|
|
}
|
2016-09-13 01:44:46 +02:00
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-13 01:44:46 +02:00
|
|
|
|
public function testConflictingTypes()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-13 01:44:46 +02:00
|
|
|
|
$foo = [
|
|
|
|
|
"bar" => ["a" => "b"],
|
|
|
|
|
"baz" => [1]
|
|
|
|
|
];
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array{bar:array{a:string}, baz:array<int, int>}', (string) $context->vars_in_scope['$foo']);
|
2016-10-03 04:01:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-10-03 04:01:01 +02:00
|
|
|
|
public function testImplicitObjectLikeCreation()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-03 04:01:01 +02:00
|
|
|
|
$foo = [
|
|
|
|
|
"bar" => 1,
|
|
|
|
|
];
|
|
|
|
|
$foo["baz"] = "a";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array{bar:int, baz:string}', (string) $context->vars_in_scope['$foo']);
|
2016-09-13 01:44:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-13 01:44:46 +02:00
|
|
|
|
public function testConflictingTypesWithAssignment()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-13 01:44:46 +02:00
|
|
|
|
$foo = [
|
|
|
|
|
"bar" => ["a" => "b"],
|
|
|
|
|
"baz" => [1]
|
|
|
|
|
];
|
|
|
|
|
$foo["bar"]["bam"]["baz"] = "hello";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$this->assertEquals(
|
2016-11-13 17:24:25 +01:00
|
|
|
|
'array{bar:array{a:string, bam:array{baz:string}}, baz:array<int, int>}',
|
2016-11-02 07:29:00 +01:00
|
|
|
|
(string) $context->vars_in_scope['$foo']
|
|
|
|
|
);
|
2016-09-13 01:44:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-13 06:18:41 +02:00
|
|
|
|
public function testConflictingTypesWithAssignment2()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-13 06:18:41 +02:00
|
|
|
|
$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
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array{a:string, b:array<int, string>}', (string) $context->vars_in_scope['$foo']);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$foo[\'a\']']);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array<int, string>', (string) $context->vars_in_scope['$foo[\'b\']']);
|
2016-10-15 19:10:05 +02:00
|
|
|
|
$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
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-09-13 06:18:41 +02:00
|
|
|
|
public function testConflictingTypesWithAssignment3()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-09-13 06:18:41 +02:00
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["a"] = "hello";
|
|
|
|
|
$foo["b"]["c"]["d"] = "goodbye";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array{a:string, b:array{c:array{d:string}}}', (string) $context->vars_in_scope['$foo']);
|
2016-09-13 06:18:41 +02:00
|
|
|
|
}
|
2016-09-22 01:15:09 +02:00
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-10-03 06:44:05 +02:00
|
|
|
|
public function testNestedObjectLikeAssignment()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-03 06:44:05 +02:00
|
|
|
|
$foo = [];
|
|
|
|
|
$foo["a"]["b"] = "hello";
|
|
|
|
|
$foo["a"]["c"] = 1;
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array{a:array{b:string, c:int}}', (string) $context->vars_in_scope['$foo']);
|
2016-10-03 06:44:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-10-03 06:44:05 +02:00
|
|
|
|
public function testConditionalObjectLikeAssignment()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-03 06:44:05 +02:00
|
|
|
|
$foo = ["a" => "hello"];
|
|
|
|
|
if (rand(0, 10) === 5) {
|
|
|
|
|
$foo["b"] = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$foo["b"] = 2;
|
|
|
|
|
}
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array{a:string, b:int}', (string) $context->vars_in_scope['$foo']);
|
2016-10-03 06:44:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 02:50:47 +01:00
|
|
|
|
|
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-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker(
|
2016-09-22 18:26:24 +02:00
|
|
|
|
'somefile.php',
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$this->project_checker,
|
2016-11-02 07:29:00 +01:00
|
|
|
|
self::$parser->parse('<?php
|
2016-09-22 18:26:24 +02:00
|
|
|
|
if ($b) {
|
|
|
|
|
$foo["a"] = "hello";
|
|
|
|
|
}
|
|
|
|
|
')
|
|
|
|
|
);
|
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();
|
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()
|
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2017-01-02 01:09:17 +01:00
|
|
|
|
class A implements \ArrayAccess {
|
|
|
|
|
public function offsetSet($offset, $value) : void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function offsetExists($offset) : bool {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function offsetUnset($offset) : void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function offsetGet($offset) : int {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-22 19:45:47 +02:00
|
|
|
|
|
|
|
|
|
$a = new A();
|
|
|
|
|
$a["bar"] = "cool";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($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
|
|
|
|
|
2017-01-20 06:23:58 +01:00
|
|
|
|
/**
|
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
|
* @expectedExceptionMessage InvalidArrayAssignment
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function testObjectAssignment()
|
|
|
|
|
{
|
|
|
|
|
$context = new Context();
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
class A {}
|
|
|
|
|
(new A)["b"] = 1;
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-10-15 19:11:08 +02:00
|
|
|
|
public function testConditionalCheck()
|
|
|
|
|
{
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker(
|
2016-10-15 19:11:08 +02:00
|
|
|
|
'somefile.php',
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$this->project_checker,
|
2016-11-02 07:29:00 +01:00
|
|
|
|
self::$parser->parse('<?php
|
2016-10-15 19:11:08 +02:00
|
|
|
|
/**
|
2016-10-28 19:24:06 +02:00
|
|
|
|
* @param array{b:string} $a
|
2016-10-15 19:11:08 +02:00
|
|
|
|
* @return null|string
|
|
|
|
|
*/
|
2016-12-30 19:09:00 +01:00
|
|
|
|
function fooFoo($a) {
|
2016-10-15 19:11:08 +02:00
|
|
|
|
if ($a["b"]) {
|
|
|
|
|
return $a["b"];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
')
|
|
|
|
|
);
|
2016-11-02 07:29:00 +01:00
|
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-15 19:11:08 +02:00
|
|
|
|
}
|
2016-11-05 22:53:30 +01:00
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-11-05 22:53:30 +01:00
|
|
|
|
public function testArrayKey()
|
|
|
|
|
{
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker(
|
2016-11-05 22:53:30 +01:00
|
|
|
|
'somefile.php',
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$this->project_checker,
|
2016-11-05 22:53:30 +01:00
|
|
|
|
self::$parser->parse('<?php
|
|
|
|
|
$a = ["foo", "bar"];
|
|
|
|
|
$b = $a[0];
|
|
|
|
|
|
|
|
|
|
$c = ["a" => "foo", "b"=> "bar"];
|
|
|
|
|
$d = "a";
|
|
|
|
|
$e = $a[$d];
|
|
|
|
|
')
|
|
|
|
|
);
|
|
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-05 22:53:30 +01:00
|
|
|
|
$this->assertEquals('string', (string)$context->vars_in_scope['$b']);
|
|
|
|
|
$this->assertEquals('string', (string)$context->vars_in_scope['$e']);
|
|
|
|
|
}
|
2016-11-06 01:17:22 +01:00
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-11-06 01:17:22 +01:00
|
|
|
|
public function testVariableKeyArrayCreate()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
$a = [];
|
|
|
|
|
$b = "boop";
|
|
|
|
|
$a[$b][] = "bam";
|
|
|
|
|
|
|
|
|
|
$c = [];
|
|
|
|
|
$c[$b][$b][] = "bam";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:24:25 +01:00
|
|
|
|
$this->assertEquals('array<string, array<int, string>>', (string) $context->vars_in_scope['$a']);
|
|
|
|
|
$this->assertEquals('array<string, array<string, array<int, string>>>', (string) $context->vars_in_scope['$c']);
|
2016-11-06 01:17:22 +01:00
|
|
|
|
}
|
2016-11-13 17:54:40 +01:00
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-11-13 17:54:40 +01:00
|
|
|
|
public function testAssignExplicitValueToGeneric()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
/** @var array<string, array<string, string>> */
|
|
|
|
|
$a = [];
|
|
|
|
|
$a["foo"] = ["bar" => "baz"];
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 17:54:40 +01:00
|
|
|
|
$this->assertEquals('array<string, array<string, string>>', (string) $context->vars_in_scope['$a']);
|
|
|
|
|
}
|
2016-11-13 20:36:29 +01:00
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-11-13 20:36:29 +01:00
|
|
|
|
public function testAdditionWithEmpty()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
$a = [];
|
|
|
|
|
$a += ["bar"];
|
|
|
|
|
|
|
|
|
|
$b = [] + ["bar"];
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 20:36:29 +01:00
|
|
|
|
$this->assertEquals('array<int, string>', (string) $context->vars_in_scope['$a']);
|
|
|
|
|
$this->assertEquals('array<int, string>', (string) $context->vars_in_scope['$b']);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-11-13 20:36:29 +01:00
|
|
|
|
public function testAdditionDifferentType()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
$a = ["bar"];
|
|
|
|
|
$a += [1];
|
|
|
|
|
|
|
|
|
|
$b = ["bar"] + [1];
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-11-13 20:36:29 +01:00
|
|
|
|
$this->assertEquals('array<int, string|int>', (string) $context->vars_in_scope['$a']);
|
|
|
|
|
$this->assertEquals('array<int, string|int>', (string) $context->vars_in_scope['$b']);
|
|
|
|
|
}
|
2016-12-05 01:33:42 +01:00
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-12-05 01:33:42 +01:00
|
|
|
|
public function testPreset1DArrayTypeWithVarKeys()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
/** @var array<string, array<int, string>> */
|
|
|
|
|
$a = [];
|
|
|
|
|
|
|
|
|
|
$foo = "foo";
|
|
|
|
|
|
|
|
|
|
$a[$foo][] = "bat";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-05 01:33:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2016-12-05 01:33:42 +01:00
|
|
|
|
public function testPreset2DArrayTypeWithVarKeys()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
/** @var array<string, array<string, array<int, string>>> */
|
|
|
|
|
$b = [];
|
|
|
|
|
|
|
|
|
|
$foo = "foo";
|
|
|
|
|
$bar = "bar";
|
|
|
|
|
|
|
|
|
|
$b[$foo][$bar][] = "bat";
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-05 01:33:42 +01:00
|
|
|
|
}
|
2016-12-11 00:24:28 +01:00
|
|
|
|
|
2016-12-12 05:41:11 +01:00
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-12 05:41:11 +01:00
|
|
|
|
* @expectedExceptionMessage InvalidArrayAssignment
|
2017-01-13 20:07:23 +01:00
|
|
|
|
* @return void
|
2016-12-12 05:41:11 +01:00
|
|
|
|
*/
|
|
|
|
|
public function testInvalidArrayAccess()
|
2016-12-11 00:24:28 +01:00
|
|
|
|
{
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2016-12-11 00:24:28 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-12-12 05:41:11 +01:00
|
|
|
|
$a = 5;
|
|
|
|
|
$a[0] = 5;
|
2016-12-11 00:24:28 +01:00
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-11 00:24:28 +01:00
|
|
|
|
}
|
2016-12-17 00:56:23 +01:00
|
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-17 00:56:23 +01:00
|
|
|
|
* @expectedExceptionMessage MixedStringOffsetAssignment
|
2017-01-13 20:07:23 +01:00
|
|
|
|
* @return void
|
2016-12-17 00:56:23 +01:00
|
|
|
|
*/
|
|
|
|
|
public function testMixedStringOffsetAssignment()
|
|
|
|
|
{
|
2016-12-30 02:07:42 +01:00
|
|
|
|
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
|
2016-12-17 04:16:29 +01:00
|
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2016-12-17 00:56:23 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
/** @var mixed */
|
|
|
|
|
$a = 5;
|
|
|
|
|
"hello"[0] = $a;
|
|
|
|
|
');
|
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-07 21:09:47 +01:00
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
}
|
2017-01-09 07:27:35 +01:00
|
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-09 07:27:35 +01:00
|
|
|
|
* @expectedExceptionMessage TypeCoercion
|
2017-01-13 20:07:23 +01:00
|
|
|
|
* @return void
|
2017-01-09 07:27:35 +01:00
|
|
|
|
*/
|
|
|
|
|
public function testMixedArrayArgument()
|
|
|
|
|
{
|
|
|
|
|
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
|
|
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
|
$context = new Context();
|
2017-01-09 07:27:35 +01:00
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
/** @param array<mixed, int|string> $foo */
|
|
|
|
|
function fooFoo(array $foo) : void { }
|
|
|
|
|
|
|
|
|
|
function barBar(array $bar) : void {
|
|
|
|
|
fooFoo($bar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
barBar([1, "2"]);
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
|
}
|
2017-01-25 08:11:24 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function testObjectLikeWithIntegerKeys()
|
|
|
|
|
{
|
|
|
|
|
$stmts = self::$parser->parse('<?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
|
|
|
|
|
');
|
|
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
|
$context = new Context();
|
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$b']);
|
|
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$c']);
|
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$d']);
|
|
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$e']);
|
|
|
|
|
}
|
2016-09-12 06:02:50 +02:00
|
|
|
|
}
|