2016-12-12 05:41:11 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class ArrayAccessTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-12-14 18:55:23 +01:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-12-12 05:41:11 +01:00
|
|
|
protected static $parser;
|
|
|
|
|
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-12-12 05:41:11 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-17 00:56:23 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-12-14 18:28:38 +01:00
|
|
|
$config = new TestConfig();
|
2016-12-12 05:41:11 +01:00
|
|
|
FileChecker::clearCache();
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-12 05:41:11 +01:00
|
|
|
public function testInstanceOfStringOffset()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() : void { }
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
function bar (array $a) : void {
|
|
|
|
if ($a["a"] instanceof A) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$a["a"]->fooFoo();
|
2016-12-12 05:41:11 +01: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-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-12 05:41:11 +01:00
|
|
|
public function testInstanceOfIntOffset()
|
|
|
|
{
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-12-12 05:41:11 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() : void { }
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
function bar (array $a) : void {
|
|
|
|
if ($a[0] instanceof A) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$a[0]->fooFoo();
|
2016-12-12 05:41:11 +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-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-12 05:41:11 +01:00
|
|
|
public function testNotEmptyStringOffset()
|
|
|
|
{
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-12-12 05:41:11 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param array<string> $a
|
|
|
|
*/
|
|
|
|
function bar (array $a) : string {
|
|
|
|
if ($a["bat"]) {
|
|
|
|
return $a["bat"];
|
|
|
|
}
|
|
|
|
|
|
|
|
return "blah";
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-12 05:41:11 +01:00
|
|
|
public function testNotEmptyIntOffset()
|
|
|
|
{
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-12-12 05:41:11 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param array<string> $a
|
|
|
|
*/
|
|
|
|
function bar (array $a) : string {
|
|
|
|
if ($a[0]) {
|
|
|
|
return $a[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return "blah";
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-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 InvalidArrayAccess
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-12 05:41:11 +01:00
|
|
|
*/
|
|
|
|
public function testInvalidArrayAccess()
|
|
|
|
{
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-12-12 05:41:11 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = 5;
|
|
|
|
echo $a[0];
|
|
|
|
');
|
|
|
|
|
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-12 05:41:11 +01:00
|
|
|
}
|
2016-12-15 07:28:36 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-15 07:28:36 +01:00
|
|
|
* @expectedExceptionMessage MixedArrayAccess
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-15 07:28:36 +01:00
|
|
|
*/
|
|
|
|
public function testMixedArrayAccess()
|
|
|
|
{
|
2016-12-30 02:07:42 +01:00
|
|
|
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-12-15 07:28:36 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/** @var mixed */
|
|
|
|
$a = [];
|
|
|
|
echo $a[0];
|
|
|
|
');
|
|
|
|
|
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-15 07:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-15 07:28:36 +01:00
|
|
|
* @expectedExceptionMessage MixedArrayOffset
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-15 07:28:36 +01:00
|
|
|
*/
|
|
|
|
public function testMixedArrayOffset()
|
|
|
|
{
|
2016-12-30 02:07:42 +01:00
|
|
|
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-12-15 07:28:36 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/** @var mixed */
|
|
|
|
$a = 5;
|
|
|
|
echo [1, 2, 3, 4][$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-15 07:28:36 +01:00
|
|
|
}
|
2016-12-17 04:16:29 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-17 04:16:29 +01:00
|
|
|
* @expectedExceptionMessage NullArrayAccess
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-17 04:16:29 +01:00
|
|
|
*/
|
|
|
|
public function testNullArrayAccess()
|
|
|
|
{
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2016-12-17 04:16:29 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = null;
|
|
|
|
echo $a[0];
|
|
|
|
');
|
|
|
|
|
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 04:16:29 +01:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|