2016-10-31 00:52:35 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
2016-10-31 00:52:35 +01:00
|
|
|
use Psalm\Context;
|
|
|
|
|
2016-11-01 19:32:19 +01:00
|
|
|
class ListTest extends PHPUnit_Framework_TestCase
|
2016-10-31 00:52:35 +01:00
|
|
|
{
|
2016-12-14 18:55:23 +01:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-11-02 07:29:00 +01:00
|
|
|
protected static $parser;
|
2016-10-31 00:52:35 +01:00
|
|
|
|
2017-02-01 01:22:05 +01:00
|
|
|
/** @var TestConfig */
|
|
|
|
protected static $config;
|
|
|
|
|
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-10-31 00:52:35 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2017-02-01 01:22:05 +01:00
|
|
|
self::$config = new TestConfig();
|
2016-10-31 00:52:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-31 00:52:35 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
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(self::$config);
|
2016-10-31 00:52:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-31 00:52:35 +01:00
|
|
|
public function testSimpleVars()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-31 00:52:35 +01:00
|
|
|
list($a, $b) = ["a", "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-10-31 00:52:35 +01:00
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$b']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-31 00:52:35 +01:00
|
|
|
public function testSimpleVarsWithSeparateTypes()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-31 00:52:35 +01:00
|
|
|
list($a, $b) = ["a", 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-10-31 00:52:35 +01:00
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
|
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$b']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-31 00:52:35 +01:00
|
|
|
public function testSimpleVarsWithSeparateTypesInVar()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-31 00:52:35 +01:00
|
|
|
$bar = ["a", 2];
|
|
|
|
list($a, $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-10-31 00:52:35 +01:00
|
|
|
$this->assertEquals('int|string', (string) $context->vars_in_scope['$a']);
|
|
|
|
$this->assertEquals('int|string', (string) $context->vars_in_scope['$b']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-31 00:52:35 +01:00
|
|
|
public function testThisVar()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-31 00:52:35 +01:00
|
|
|
class A {
|
2016-11-01 19:32:19 +01:00
|
|
|
/** @var string */
|
2017-01-27 07:23:12 +01:00
|
|
|
public $a = "";
|
2016-11-01 19:32:19 +01:00
|
|
|
|
|
|
|
/** @var string */
|
2017-01-27 07:23:12 +01:00
|
|
|
public $b = "";
|
2016-11-01 19:32:19 +01:00
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() : string
|
2016-11-01 19:32:19 +01:00
|
|
|
{
|
|
|
|
list($this->a, $this->b) = ["a", "b"];
|
|
|
|
|
|
|
|
return $this->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-01 19:32:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-11-01 19:32:19 +01:00
|
|
|
* @expectedExceptionMessage InvalidPropertyAssignment - somefile.php:11
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-11-01 19:32:19 +01:00
|
|
|
*/
|
|
|
|
public function testThisVarWithBadType()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-11-01 19:32:19 +01:00
|
|
|
class A {
|
|
|
|
/** @var int */
|
2017-01-27 07:23:12 +01:00
|
|
|
public $a = 0;
|
2016-11-01 19:32:19 +01:00
|
|
|
|
|
|
|
/** @var string */
|
2017-01-27 07:23:12 +01:00
|
|
|
public $b = "";
|
2016-10-31 00:52:35 +01:00
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo() : string
|
2016-10-31 00:52:35 +01:00
|
|
|
{
|
|
|
|
list($this->a, $this->b) = ["a", "b"];
|
|
|
|
|
|
|
|
return $this->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-10-31 00:52:35 +01:00
|
|
|
}
|
|
|
|
}
|