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