2016-12-04 05:03:51 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class Php71Test extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-12-14 18:55:23 +01:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-12-04 05:03:51 +01:00
|
|
|
protected static $parser;
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
2016-12-04 05:03:51 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
2016-12-04 05:03:51 +01:00
|
|
|
|
2016-12-17 00:56:23 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-12-14 18:28:38 +01:00
|
|
|
$config = new TestConfig();
|
2016-12-04 05:03:51 +01:00
|
|
|
$config->use_docblock_types = true;
|
|
|
|
|
|
|
|
FileChecker::clearCache();
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
2016-12-04 05:03:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableReturnType()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function a(): ?string
|
|
|
|
{
|
|
|
|
return rand(0, 10) ? "elePHPant" : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = a();
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 05:03:51 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 05:03:51 +01:00
|
|
|
$this->assertEquals('string|null', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNullableArgument()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function test(?string $name) : ?string
|
|
|
|
{
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
test("elePHPant");
|
|
|
|
test(null);
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 05:03:51 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 05:03:51 +01:00
|
|
|
}
|
2016-12-04 07:44:33 +01:00
|
|
|
|
|
|
|
public function testPrivateClassConst()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
private const IS_PRIVATE = 1;
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo() : int {
|
2016-12-04 07:44:33 +01:00
|
|
|
return A::IS_PRIVATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 07:44:33 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 07:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InaccessibleClassConstant
|
|
|
|
*/
|
|
|
|
public function testInvalidPrivateClassConstFetch()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
private const IS_PRIVATE = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo A::IS_PRIVATE;
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 07:44:33 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 07:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InaccessibleClassConstant
|
|
|
|
*/
|
|
|
|
public function testInvalidPrivateClassConstFetchFromSubclass()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
private const IS_PRIVATE = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A
|
|
|
|
{
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo() : int {
|
2016-12-04 07:44:33 +01:00
|
|
|
return A::IS_PRIVATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 07:44:33 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 07:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testProtectedClassConst()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
protected const IS_PROTECTED = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A
|
|
|
|
{
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo() : int {
|
2016-12-04 07:44:33 +01:00
|
|
|
return A::IS_PROTECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 07:44:33 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 07:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InaccessibleClassConstant
|
|
|
|
*/
|
|
|
|
public function testInvalidProtectedClassConstFetch()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
protected const IS_PROTECTED = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo A::IS_PROTECTED;
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 07:44:33 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 07:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPublicClassConstFetch()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A
|
|
|
|
{
|
|
|
|
public const IS_PUBLIC = 1;
|
|
|
|
const IS_ALSO_PUBLIC = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A
|
|
|
|
{
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo() : int {
|
2016-12-04 07:44:33 +01:00
|
|
|
echo A::IS_PUBLIC;
|
|
|
|
return A::IS_ALSO_PUBLIC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo A::IS_PUBLIC;
|
|
|
|
echo A::IS_ALSO_PUBLIC;
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 07:44:33 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 07:44:33 +01:00
|
|
|
}
|
2016-12-04 19:35:38 +01:00
|
|
|
|
|
|
|
public function testArrayDestructuring()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$data = [
|
|
|
|
[1, "Tom"],
|
|
|
|
[2, "Fred"],
|
|
|
|
];
|
|
|
|
|
|
|
|
// list() style
|
|
|
|
list($id1, $name1) = $data[0];
|
|
|
|
|
|
|
|
// [] style
|
|
|
|
[$id2, $name2] = $data[1];
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 19:35:38 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 19:35:38 +01:00
|
|
|
$this->assertEquals('string|int', (string) $context->vars_in_scope['$id1']);
|
|
|
|
$this->assertEquals('string|int', (string) $context->vars_in_scope['$name1']);
|
|
|
|
$this->assertEquals('string|int', (string) $context->vars_in_scope['$id2']);
|
|
|
|
$this->assertEquals('string|int', (string) $context->vars_in_scope['$name2']);
|
|
|
|
}
|
|
|
|
|
2016-12-04 20:14:00 +01:00
|
|
|
public function testArrayDestructuringInForeach()
|
2016-12-04 19:35:38 +01:00
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$data = [
|
|
|
|
[1, "Tom"],
|
|
|
|
[2, "Fred"],
|
|
|
|
];
|
|
|
|
|
2016-12-04 20:14:00 +01:00
|
|
|
// [] style
|
|
|
|
foreach ($data as [$id, $name]) {
|
2016-12-04 19:35:38 +01:00
|
|
|
echo $id;
|
|
|
|
echo $name;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 19:35:38 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 20:14:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayDestructuringWithKeys()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$data = [
|
|
|
|
["id" => 1, "name" => "Tom"],
|
|
|
|
["id" => 2, "name" => "Fred"],
|
|
|
|
];
|
2016-12-04 19:35:38 +01:00
|
|
|
|
2016-12-04 20:14:00 +01:00
|
|
|
// list() style
|
|
|
|
list("id" => $id1, "name" => $name1) = $data[0];
|
|
|
|
|
|
|
|
// [] style
|
|
|
|
["id" => $id2, "name" => $name2] = $data[1];
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 20:14:00 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 20:14:00 +01:00
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$id1']);
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$name1']);
|
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$id2']);
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$name2']);
|
2016-12-04 19:35:38 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 20:14:00 +01:00
|
|
|
public function testArrayListDestructuringInForeachWithKeys()
|
2016-12-04 19:35:38 +01:00
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$data = [
|
2016-12-04 20:14:00 +01:00
|
|
|
["id" => 1, "name" => "Tom"],
|
|
|
|
["id" => 2, "name" => "Fred"],
|
|
|
|
];
|
|
|
|
|
|
|
|
$last_id = null;
|
|
|
|
$last_name = null;
|
|
|
|
|
|
|
|
// list() style
|
|
|
|
foreach ($data as list("id" => $id, "name" => $name)) {
|
|
|
|
$last_id = $id;
|
|
|
|
$last_name = $name;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 20:14:00 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 20:14:00 +01:00
|
|
|
$this->assertEquals('null|int', (string) $context->vars_in_scope['$last_id']);
|
|
|
|
$this->assertEquals('null|string', (string) $context->vars_in_scope['$last_name']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayDestructuringInForeachWithKeys()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$data = [
|
|
|
|
["id" => 1, "name" => "Tom"],
|
|
|
|
["id" => 2, "name" => "Fred"],
|
2016-12-04 19:35:38 +01:00
|
|
|
];
|
|
|
|
|
2016-12-04 20:14:00 +01:00
|
|
|
$last_id = null;
|
|
|
|
$last_name = null;
|
|
|
|
|
2016-12-04 19:35:38 +01:00
|
|
|
// [] style
|
2016-12-04 20:14:00 +01:00
|
|
|
foreach ($data as ["id" => $id, "name" => $name]) {
|
|
|
|
$last_id = $id;
|
|
|
|
$last_name = $name;
|
2016-12-04 19:35:38 +01:00
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-04 19:35:38 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-04 20:14:00 +01:00
|
|
|
$this->assertEquals('null|int', (string) $context->vars_in_scope['$last_id']);
|
|
|
|
$this->assertEquals('null|string', (string) $context->vars_in_scope['$last_name']);
|
2016-12-04 19:35:38 +01:00
|
|
|
}
|
2016-12-05 03:04:25 +01:00
|
|
|
|
|
|
|
public function testIterableArg()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-12-17 00:56:23 +01:00
|
|
|
/**
|
|
|
|
* @param iterable<string> $iter
|
|
|
|
*/
|
2016-12-05 03:04:25 +01:00
|
|
|
function iterator(iterable $iter) : void
|
|
|
|
{
|
|
|
|
foreach ($iter as $val) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator([1, 2, 3, 4]);
|
|
|
|
iterator(new SplFixedArray(5));
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-05 03:04:25 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-05 03:04:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidArgument
|
|
|
|
*/
|
|
|
|
public function testInvalidIterableArg()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-12-17 00:56:23 +01:00
|
|
|
/**
|
|
|
|
* @param iterable<string> $iter
|
|
|
|
*/
|
2016-12-05 03:04:25 +01:00
|
|
|
function iterator(iterable $iter) : void
|
|
|
|
{
|
|
|
|
foreach ($iter as $val) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator(new A());
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-05 03:04:25 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-05 03:04:25 +01:00
|
|
|
}
|
2016-12-04 05:03:51 +01:00
|
|
|
}
|