2016-10-30 03:17:46 +01: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-30 03:17:46 +01:00
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class ClosureTest 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-30 03:17:46 +01:00
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
2016-10-30 03:17:46 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-10-30 03:17:46 +01:00
|
|
|
|
2016-12-14 18:28:38 +01:00
|
|
|
$config = new TestConfig();
|
2016-10-30 03:17:46 +01: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();
|
2016-10-30 03:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testByRefUseVar()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-11-11 23:13:24 +01:00
|
|
|
/** @return void */
|
2016-10-30 03:17:46 +01:00
|
|
|
function run_function(\Closure $fnc) {
|
|
|
|
$fnc();
|
|
|
|
}
|
|
|
|
|
|
|
|
// here we have to make sure $data exists as a side-effect of calling `run_function`
|
|
|
|
// because it could exist depending on how run_function is implemented
|
2016-12-09 19:04:08 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @psalm-suppress MixedArgument
|
|
|
|
*/
|
2016-10-30 03:17:46 +01:00
|
|
|
function fn() {
|
|
|
|
run_function(
|
2016-12-07 20:13:39 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-30 03:17:46 +01:00
|
|
|
function() use(&$data) {
|
|
|
|
$data = 1;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
echo $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn();
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-10-30 03:17:46 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-30 03:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-02 07:29:00 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-10-30 03:17:46 +01:00
|
|
|
* @expectedExceptionMessage InvalidScalarArgument
|
|
|
|
*/
|
|
|
|
public function testWrongArg()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-10-30 03:17:46 +01:00
|
|
|
$bar = ["foo", "bar"];
|
|
|
|
|
|
|
|
$bam = array_map(
|
2016-12-07 20:13:39 +01:00
|
|
|
function(int $a) : int {
|
2016-10-30 03:17:46 +01:00
|
|
|
return $a + 1;
|
|
|
|
},
|
|
|
|
$bar
|
|
|
|
);
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-10-30 03:17:46 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-10-30 03:17:46 +01:00
|
|
|
}
|
2016-12-07 20:13:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidReturnType
|
|
|
|
*/
|
|
|
|
public function testNoReturn()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$bar = ["foo", "bar"];
|
|
|
|
|
|
|
|
$bam = array_map(
|
|
|
|
function(string $a) : string {
|
|
|
|
},
|
|
|
|
$bar
|
|
|
|
);
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-07 20:13:39 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInferredArg()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$bar = ["foo", "bar"];
|
|
|
|
|
|
|
|
$bam = array_map(
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingReturnType
|
|
|
|
*/
|
|
|
|
function(string $a) {
|
|
|
|
return $a . "blah";
|
|
|
|
},
|
|
|
|
$bar
|
|
|
|
);
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-07 20:13:39 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-07 20:13:39 +01:00
|
|
|
}
|
2016-12-31 16:51:42 +01:00
|
|
|
|
|
|
|
public function testVarReturnType()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
|
|
|
|
$add_one = function(int $a) : int {
|
|
|
|
return $a + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
$a = $add_one(1);
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-31 16:51:42 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-31 16:51:42 +01:00
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
|
|
|
|
2017-01-09 07:27:35 +01:00
|
|
|
public function testCallableToClosure()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
function foo() {
|
|
|
|
return function(string $a) : string {
|
|
|
|
return $a . "blah";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
2017-01-02 01:24:15 +01:00
|
|
|
public function testCallable()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function foo(Callable $c) : void {
|
|
|
|
echo (string)$c();
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-02 01:24:15 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2017-01-02 01:24:15 +01:00
|
|
|
}
|
|
|
|
|
2016-12-31 16:51:42 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidFunctionCall
|
|
|
|
*/
|
|
|
|
public function testStringFunctionCall()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$bad_one = "hello";
|
|
|
|
$a = $bad_one(1);
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-31 16:51:42 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-07 21:09:47 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2016-12-31 16:51:42 +01:00
|
|
|
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
|
|
|
|
}
|
2016-10-30 03:17:46 +01:00
|
|
|
}
|