2016-10-19 04:02:38 +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:02:38 +02:00
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class Php70Test 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-19 04:02:38 +02:00
|
|
|
|
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-19 04:02:38 +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:02:38 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-19 04:02:38 +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(new TestConfig());
|
2016-10-19 04:02:38 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-19 04:02:38 +02:00
|
|
|
public function testFunctionTypeHints()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-19 04:02:38 +02:00
|
|
|
function indexof(string $haystack, string $needle) : int
|
|
|
|
{
|
|
|
|
$pos = strpos($haystack, $needle);
|
|
|
|
|
|
|
|
if ($pos === false) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = indexof("arr", "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-19 04:02:38 +02:00
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-19 04:02:38 +02:00
|
|
|
public function testMethodTypeHints()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-19 04:02:38 +02:00
|
|
|
class Foo {
|
|
|
|
public static function indexof(string $haystack, string $needle) : int
|
|
|
|
{
|
|
|
|
$pos = strpos($haystack, $needle);
|
|
|
|
|
|
|
|
if ($pos === false) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = Foo::indexof("arr", "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-19 04:02:38 +02:00
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
2016-10-20 06:47:10 +02:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 06:47:10 +02:00
|
|
|
public function testNullCoalesce()
|
|
|
|
{
|
2016-12-30 02:07:42 +01:00
|
|
|
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 06:47:10 +02:00
|
|
|
$a = $_GET["bar"] ?? "nobody";
|
|
|
|
');
|
|
|
|
|
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:47:10 +02:00
|
|
|
$this->assertEquals('mixed', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 06:47:10 +02:00
|
|
|
public function testSpaceship()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 06:47:10 +02:00
|
|
|
$a = 1 <=> 1;
|
|
|
|
');
|
|
|
|
|
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:47:10 +02:00
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 06:47:10 +02:00
|
|
|
public function testDefineArray()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 06:47:10 +02:00
|
|
|
define("ANIMALS", [
|
|
|
|
"dog",
|
|
|
|
"cat",
|
|
|
|
"bird"
|
|
|
|
]);
|
|
|
|
|
|
|
|
$a = ANIMALS[1];
|
|
|
|
');
|
|
|
|
|
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:47:10 +02:00
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-29 19:20:12 +01:00
|
|
|
public function testAnonymousClassLogger()
|
2016-10-20 06:47:10 +02:00
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 06:47:10 +02:00
|
|
|
interface Logger {
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-10-20 06:47:10 +02:00
|
|
|
public function log(string $msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Application {
|
2016-11-01 19:32:19 +01:00
|
|
|
/** @var Logger|null */
|
2016-10-20 06:47:10 +02:00
|
|
|
private $logger;
|
|
|
|
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-10-20 06:47:10 +02:00
|
|
|
public function setLogger(Logger $logger) {
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$app = new Application;
|
|
|
|
$app->setLogger(new class implements Logger {
|
|
|
|
public function log(string $msg) {
|
|
|
|
echo $msg;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
');
|
|
|
|
|
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:47:10 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 19:20:12 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage UndefinedClass
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAnonymousClassWithBadStatement()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$foo = new class {
|
|
|
|
public function a() {
|
|
|
|
new B();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidReturnType
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAnonymousClassWithInvalidFunctionReturnType()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$foo = new class {
|
|
|
|
public function a() : string {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-13 16:44:04 +01:00
|
|
|
public function testAnonymousClassFunctionReturnType()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$class = new class {
|
|
|
|
public function f() : int {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function g(int $i) : int {
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
$x = g($class->f());
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-13 16:44:04 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 20:26:03 +02:00
|
|
|
public function testGeneratorWithReturn()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 20:26:03 +02:00
|
|
|
/**
|
2016-10-21 00:05:28 +02:00
|
|
|
* @return Generator<int,int>
|
|
|
|
* @psalm-generator-return string
|
2016-10-20 20:26:03 +02:00
|
|
|
*/
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(int $i) : Generator {
|
2016-10-20 20:26:03 +02:00
|
|
|
if ($i === 1) {
|
|
|
|
return "bash";
|
|
|
|
}
|
|
|
|
|
|
|
|
yield 1;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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:47:10 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 06:47:10 +02:00
|
|
|
public function testGeneratorDelegation()
|
|
|
|
{
|
2016-12-30 02:07:42 +01:00
|
|
|
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-20 17:31:19 +02:00
|
|
|
/**
|
2016-10-20 20:26:03 +02:00
|
|
|
* @return Generator<int,int>
|
2016-10-21 00:05:28 +02:00
|
|
|
* @psalm-generator-return int
|
2016-10-20 17:31:19 +02:00
|
|
|
*/
|
2016-10-20 20:26:03 +02:00
|
|
|
function count_to_ten() : Generator {
|
2016-10-20 06:47:10 +02:00
|
|
|
yield 1;
|
|
|
|
yield 2;
|
2016-10-20 17:31:19 +02:00
|
|
|
yield from [3, 4];
|
|
|
|
yield from new ArrayIterator([5, 6]);
|
|
|
|
yield from seven_eight();
|
|
|
|
return yield from nine_ten();
|
2016-10-20 06:47:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-20 17:31:19 +02:00
|
|
|
/**
|
2016-10-20 20:26:03 +02:00
|
|
|
* @return Generator<int,int>
|
2016-10-20 17:31:19 +02:00
|
|
|
*/
|
2016-10-20 20:26:03 +02:00
|
|
|
function seven_eight() : Generator {
|
2016-10-20 17:31:19 +02:00
|
|
|
yield 7;
|
|
|
|
yield from eight();
|
2016-10-20 06:47:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-20 17:31:19 +02:00
|
|
|
/**
|
2016-10-20 20:26:03 +02:00
|
|
|
* @return Generator<int,int>
|
2016-10-20 17:31:19 +02:00
|
|
|
*/
|
2016-10-20 20:26:03 +02:00
|
|
|
function eight() : Generator {
|
2016-10-20 17:31:19 +02:00
|
|
|
yield 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-20 20:26:03 +02:00
|
|
|
* @return Generator<int,int>
|
2016-10-21 00:05:28 +02:00
|
|
|
* @psalm-generator-return int
|
2016-10-20 17:31:19 +02:00
|
|
|
*/
|
2016-10-20 20:26:03 +02:00
|
|
|
function nine_ten() : Generator {
|
2016-10-20 17:31:19 +02:00
|
|
|
yield 9;
|
|
|
|
return 10;
|
2016-10-20 06:47:10 +02:00
|
|
|
}
|
2016-10-20 17:31:19 +02:00
|
|
|
|
|
|
|
$gen = count_to_ten();
|
|
|
|
foreach ($gen as $num) {
|
|
|
|
echo "$num ";
|
|
|
|
}
|
|
|
|
$gen2 = $gen->getReturn();
|
2016-10-20 06:47:10 +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-11-13 17:24:25 +01:00
|
|
|
$this->assertEquals('Generator<int, int>', (string) $context->vars_in_scope['$gen']);
|
2016-10-20 20:26:03 +02:00
|
|
|
$this->assertEquals('mixed', (string) $context->vars_in_scope['$gen2']);
|
2016-10-20 06:47:10 +02:00
|
|
|
}
|
2016-11-21 04:40:19 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-21 04:40:19 +01:00
|
|
|
public function testMultipleUse()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
namespace Name\Space {
|
|
|
|
class A {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Noom\Spice {
|
|
|
|
use Name\Space\{
|
|
|
|
A,
|
|
|
|
B
|
|
|
|
};
|
|
|
|
|
|
|
|
new A();
|
|
|
|
new 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-11-21 04:40:19 +01:00
|
|
|
}
|
2016-10-19 04:02:38 +02:00
|
|
|
}
|