2016-10-18 22:23:09 -04:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
2016-11-02 02:29:00 -04:00
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
2016-10-18 22:23:09 -04:00
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class Php56Test extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-12-14 12:55:23 -05:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-11-02 02:29:00 -04:00
|
|
|
protected static $parser;
|
2016-10-18 22:23:09 -04:00
|
|
|
|
2017-01-31 19:22:05 -05:00
|
|
|
/** @var TestConfig */
|
|
|
|
protected static $config;
|
|
|
|
|
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-10-18 22:23:09 -04:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 02:29:00 -04:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2017-01-31 19:22:05 -05:00
|
|
|
self::$config = new TestConfig();
|
2016-10-18 22:23:09 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-18 22:23:09 -04:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-11-02 02:29:00 -04: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(self::$config);
|
2016-10-18 22:23:09 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-18 22:23:09 -04:00
|
|
|
public function testConstArray()
|
|
|
|
{
|
2016-11-02 02:29:00 -04:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-18 22:23:09 -04:00
|
|
|
const ARR = ["a", "b"];
|
|
|
|
$a = ARR[0];
|
|
|
|
');
|
|
|
|
|
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-10-18 22:23:09 -04:00
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-18 22:23:09 -04:00
|
|
|
public function testConstFeatures()
|
|
|
|
{
|
2016-11-02 02:29:00 -04:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-18 22:23:09 -04:00
|
|
|
const ONE = 1;
|
|
|
|
const TWO = ONE * 2;
|
|
|
|
|
|
|
|
class C {
|
|
|
|
const THREE = TWO + 1;
|
|
|
|
const ONE_THIRD = ONE / self::THREE;
|
|
|
|
const SENTENCE = "The value of THREE is " . self::THREE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $a
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function f($a = ONE + self::THREE) {
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$d = (new C)->f();
|
|
|
|
$e = C::SENTENCE;
|
2016-10-19 00:00:49 -04:00
|
|
|
$f = TWO;
|
|
|
|
$g = C::ONE_THIRD;
|
2016-10-18 22:23:09 -04: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-10-18 22:23:09 -04:00
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$d']);
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$e']);
|
2016-10-19 00:00:49 -04:00
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$f']);
|
2016-12-08 16:37:14 -05:00
|
|
|
$this->assertEquals('float|int', (string) $context->vars_in_scope['$g']);
|
2016-10-18 22:23:09 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-18 22:23:09 -04:00
|
|
|
public function testArgumentUnpacking()
|
|
|
|
{
|
2016-11-02 02:29:00 -04:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-11-11 17:13:24 -05:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
* @param int $a
|
|
|
|
* @param int $b
|
|
|
|
* @param int $c
|
|
|
|
*/
|
2016-10-18 22:23:09 -04:00
|
|
|
function add($a, $b, $c) {
|
|
|
|
return $a + $b + $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
$operators = [2, 3];
|
|
|
|
echo add(1, ...$operators);
|
|
|
|
');
|
|
|
|
|
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-10-18 22:23:09 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-18 22:23:09 -04:00
|
|
|
public function testExponentiation()
|
|
|
|
{
|
2016-11-02 02:29:00 -04:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-18 22:23:09 -04:00
|
|
|
$a = 2;
|
|
|
|
$a **= 3;
|
|
|
|
');
|
|
|
|
|
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-10-18 22:23:09 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-20 21:49:29 -05:00
|
|
|
public function testConstantAliasInNamespace()
|
2016-10-18 22:23:09 -04:00
|
|
|
{
|
2016-11-20 21:49:29 -05:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
namespace Name\Space {
|
|
|
|
const FOO = 42;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Noom\Spice {
|
|
|
|
use const Name\Space\FOO;
|
|
|
|
|
|
|
|
echo FOO . "\n";
|
|
|
|
echo \Name\Space\FOO;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-11-20 21:49:29 -05:00
|
|
|
}
|
2016-10-19 00:31:32 -04:00
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-20 21:49:29 -05:00
|
|
|
public function testConstantAliasInClass()
|
|
|
|
{
|
2016-11-02 02:29:00 -04:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-18 22:23:09 -04:00
|
|
|
namespace Name\Space {
|
|
|
|
const FOO = 42;
|
|
|
|
}
|
|
|
|
|
2016-11-20 21:49:29 -05:00
|
|
|
namespace Noom\Spice {
|
2016-10-18 22:23:09 -04:00
|
|
|
use const Name\Space\FOO;
|
2016-11-20 21:49:29 -05:00
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @return void */
|
2016-12-30 13:09:00 -05:00
|
|
|
public function fooFoo() {
|
2016-11-20 21:49:29 -05:00
|
|
|
echo FOO . "\n";
|
|
|
|
echo \Name\Space\FOO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-11-20 21:49:29 -05:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-20 21:49:29 -05:00
|
|
|
public function testFunctionAliasInNamespace()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
namespace Name\Space {
|
2016-12-28 19:57:18 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-20 21:49:29 -05:00
|
|
|
function f() { echo __FUNCTION__."\n"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Noom\Spice {
|
2016-10-18 22:23:09 -04:00
|
|
|
use function Name\Space\f;
|
|
|
|
|
|
|
|
f();
|
2016-11-20 21:49:29 -05:00
|
|
|
\Name\Space\f();
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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-11-20 21:49:29 -05:00
|
|
|
}
|
|
|
|
|
2017-01-13 14:07:23 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-20 21:49:29 -05:00
|
|
|
public function testFunctionAliasInClass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
namespace Name\Space {
|
2016-12-28 19:57:18 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-20 21:49:29 -05:00
|
|
|
function f() { echo __FUNCTION__."\n"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Noom\Spice {
|
|
|
|
use function Name\Space\f;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @return void */
|
2016-12-30 13:09:00 -05:00
|
|
|
public function fooFoo() {
|
2016-11-20 21:49:29 -05:00
|
|
|
f();
|
|
|
|
\Name\Space\f();
|
|
|
|
}
|
|
|
|
}
|
2016-10-18 22:23:09 -04: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-10-18 22:23:09 -04:00
|
|
|
}
|
|
|
|
}
|