2016-10-20 06:13:35 +02: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-20 06:13:35 +02:00
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class Php55Test 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-10-20 06:13:35 +02: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-20 06:13:35 +02: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-20 06:13:35 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 06:13:35 +02: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-20 06:13:35 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 06:13:35 +02:00
|
|
|
public function testGenerator()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 17:31:39 +02:00
|
|
|
/**
|
|
|
|
* @param int $start
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $step
|
2016-10-20 20:26:03 +02:00
|
|
|
* @return Generator<int>
|
2016-10-20 17:31:39 +02:00
|
|
|
*/
|
2016-10-20 06:13:35 +02:00
|
|
|
function xrange($start, $limit, $step = 1) {
|
|
|
|
for ($i = $start; $i <= $limit; $i += $step) {
|
|
|
|
yield $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 20:26:03 +02:00
|
|
|
$a = null;
|
|
|
|
|
2016-10-20 06:13:35 +02:00
|
|
|
/*
|
|
|
|
* Note that an array is never created or returned,
|
|
|
|
* which saves memory.
|
|
|
|
*/
|
|
|
|
foreach (xrange(1, 9, 2) as $number) {
|
2016-10-20 20:26:03 +02:00
|
|
|
$a = $number;
|
2016-10-20 06:13:35 +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-10-20 20:26:03 +02:00
|
|
|
$this->assertEquals('null|int', (string) $context->vars_in_scope['$a']);
|
2016-10-20 06:13:35 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 06:13:35 +02:00
|
|
|
public function testFinally()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 06:13:35 +02:00
|
|
|
try {
|
|
|
|
}
|
|
|
|
catch (\Exception $e) {
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-20 06:13:35 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 06:13:35 +02:00
|
|
|
public function testForeachList()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 06:13:35 +02:00
|
|
|
$array = [
|
|
|
|
[1, 2],
|
|
|
|
[3, 4],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($array as list($a, $b)) {
|
|
|
|
echo "A: $a; B: $b\n";
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-20 06:13:35 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 06:13:35 +02:00
|
|
|
public function testArrayStringDereferencing()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 06:13:35 +02:00
|
|
|
$a = [1, 2, 3][0];
|
|
|
|
$b = "PHP"[0];
|
|
|
|
');
|
|
|
|
|
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-20 06:13:35 +02:00
|
|
|
$this->assertEquals('int', (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-20 06:13:35 +02:00
|
|
|
public function testClassString()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 06:13:35 +02:00
|
|
|
class ClassName {}
|
|
|
|
|
|
|
|
$a = ClassName::class;
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
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-20 06:13:35 +02:00
|
|
|
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
}
|