2016-10-19 04:23:09 +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-19 04:23:09 +02:00
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class Php56Test extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
protected static $parser;
|
2016-10-19 04:23:09 +02:00
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-10-19 04:23:09 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$config = Config::getInstance();
|
2016-10-19 04:23:09 +02:00
|
|
|
$config->throw_exception = true;
|
|
|
|
$config->use_docblock_types = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
FileChecker::clearCache();
|
2016-10-19 04:23:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstArray()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-19 04:23:09 +02:00
|
|
|
const ARR = ["a", "b"];
|
|
|
|
$a = ARR[0];
|
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-10-19 04:23:09 +02:00
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstFeatures()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-19 04:23:09 +02: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 06:00:49 +02:00
|
|
|
$f = TWO;
|
|
|
|
$g = C::ONE_THIRD;
|
2016-10-19 04:23:09 +02:00
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-10-19 04:23:09 +02:00
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$d']);
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$e']);
|
2016-10-19 06:00:49 +02:00
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$f']);
|
|
|
|
$this->assertEquals('float', (string) $context->vars_in_scope['$g']);
|
2016-10-19 04:23:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testVariadic()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-19 04:23:09 +02:00
|
|
|
function f($req, $opt = null, ...$params) {
|
|
|
|
}
|
|
|
|
|
|
|
|
f(1);
|
|
|
|
f(1, 2);
|
|
|
|
f(1, 2, 3);
|
|
|
|
f(1, 2, 3, 4);
|
|
|
|
f(1, 2, 3, 4, 5);
|
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-10-19 04:23:09 +02:00
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
2016-10-30 02:50:24 +02:00
|
|
|
public function testVariadicArray()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return array<int> */
|
2016-10-30 02:50:24 +02:00
|
|
|
function f(int ...$a_list) {
|
|
|
|
return array_map(function (int $a) {
|
|
|
|
return $a + 1;
|
|
|
|
}, $a_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
f(1);
|
|
|
|
f(1, 2);
|
|
|
|
f(1, 2, 3);
|
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-10-30 02:50:24 +02:00
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
2016-10-19 04:23:09 +02:00
|
|
|
public function testArgumentUnpacking()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-11-11 23:13:24 +01:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
* @param int $a
|
|
|
|
* @param int $b
|
|
|
|
* @param int $c
|
|
|
|
*/
|
2016-10-19 04:23:09 +02:00
|
|
|
function add($a, $b, $c) {
|
|
|
|
return $a + $b + $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
$operators = [2, 3];
|
|
|
|
echo add(1, ...$operators);
|
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-10-19 04:23:09 +02:00
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExponentiation()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-19 04:23:09 +02:00
|
|
|
$a = 2;
|
|
|
|
$a **= 3;
|
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-10-19 04:23:09 +02:00
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
2016-11-21 03:49:29 +01:00
|
|
|
public function testConstantAliasInNamespace()
|
2016-10-19 04:23:09 +02:00
|
|
|
{
|
2016-11-21 03:49:29 +01: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;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-10-19 06:31:32 +02:00
|
|
|
|
2016-11-21 03:49:29 +01:00
|
|
|
public function testConstantAliasInClass()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-19 04:23:09 +02:00
|
|
|
namespace Name\Space {
|
|
|
|
const FOO = 42;
|
|
|
|
}
|
|
|
|
|
2016-11-21 03:49:29 +01:00
|
|
|
namespace Noom\Spice {
|
2016-10-19 04:23:09 +02:00
|
|
|
use const Name\Space\FOO;
|
2016-11-21 03:49:29 +01:00
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function foo() {
|
|
|
|
echo FOO . "\n";
|
|
|
|
echo \Name\Space\FOO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFunctionAliasInNamespace()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
namespace Name\Space {
|
|
|
|
function f() { echo __FUNCTION__."\n"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Noom\Spice {
|
2016-10-19 04:23:09 +02:00
|
|
|
use function Name\Space\f;
|
|
|
|
|
|
|
|
f();
|
2016-11-21 03:49:29 +01:00
|
|
|
\Name\Space\f();
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFunctionAliasInClass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
namespace Name\Space {
|
|
|
|
function f() { echo __FUNCTION__."\n"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Noom\Spice {
|
|
|
|
use function Name\Space\f;
|
|
|
|
|
|
|
|
class A {
|
|
|
|
/** @return void */
|
|
|
|
public function foo() {
|
|
|
|
f();
|
|
|
|
\Name\Space\f();
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 04:23:09 +02:00
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-10-19 04:23:09 +02:00
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
}
|