2016-12-12 05:41:11 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class FunctionCallTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-12-14 18:55:23 +01:00
|
|
|
/** @var \PhpParser\Parser */
|
2016-12-12 05:41:11 +01:00
|
|
|
protected static $parser;
|
|
|
|
|
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-12-12 05:41:11 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-17 00:56:23 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-12-12 05:41:11 +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-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-12 05:41:11 +01:00
|
|
|
* @expectedExceptionMessage InvalidScalarArgument
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-12 05:41:11 +01:00
|
|
|
*/
|
|
|
|
public function testInvalidScalarArgument()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(int $a) : void {}
|
|
|
|
fooFoo("string");
|
2016-12-12 05:41:11 +01: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-12-12 05:41:11 +01:00
|
|
|
}
|
2016-12-15 01:24:33 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-15 01:24:33 +01:00
|
|
|
* @expectedExceptionMessage MixedArgument
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-15 01:24:33 +01:00
|
|
|
*/
|
|
|
|
public function testMixedArgument()
|
|
|
|
{
|
2016-12-30 02:07:42 +01:00
|
|
|
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
2016-12-15 01:24:33 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(int $a) : void {}
|
2016-12-15 01:24:33 +01:00
|
|
|
/** @var mixed */
|
|
|
|
$a = "hello";
|
2016-12-30 19:09:00 +01:00
|
|
|
fooFoo($a);
|
2016-12-15 01:24:33 +01: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-12-15 01:24:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-15 01:24:33 +01:00
|
|
|
* @expectedExceptionMessage NullArgument
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-15 01:24:33 +01:00
|
|
|
*/
|
|
|
|
public function testNullArgument()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(int $a) : void {}
|
|
|
|
fooFoo(null);
|
2016-12-15 01:24:33 +01: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-12-15 01:24:33 +01:00
|
|
|
}
|
2016-12-17 04:16:29 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-17 04:16:29 +01:00
|
|
|
* @expectedExceptionMessage TooFewArguments
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-17 04:16:29 +01:00
|
|
|
*/
|
|
|
|
public function testTooFewArguments()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(int $a) : void {}
|
|
|
|
fooFoo();
|
2016-12-17 04:16:29 +01: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-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-17 04:16:29 +01:00
|
|
|
* @expectedExceptionMessage TooManyArguments
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-17 04:16:29 +01:00
|
|
|
*/
|
|
|
|
public function testTooManyArguments()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(int $a) : void {}
|
|
|
|
fooFoo(5, "dfd");
|
2016-12-17 04:16:29 +01: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-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2016-12-17 04:16:29 +01:00
|
|
|
* @expectedExceptionMessage TypeCoercion
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-17 04:16:29 +01:00
|
|
|
*/
|
|
|
|
public function testTypeCoercion()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {}
|
|
|
|
class B extends A{}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(B $b) : void {}
|
|
|
|
fooFoo(new A());
|
2016-12-17 04:16:29 +01: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-12-17 04:16:29 +01:00
|
|
|
}
|
2017-01-02 07:07:44 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-02 07:07:44 +01:00
|
|
|
public function testTypedArrayWithDefault()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {}
|
|
|
|
|
|
|
|
/** @param array<A> $a */
|
|
|
|
function fooFoo(array $a = []) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
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);
|
2017-01-02 07:07:44 +01:00
|
|
|
}
|
2017-01-02 07:20:47 +01:00
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-02 07:20:47 +01:00
|
|
|
* @expectedExceptionMessage DuplicateParam
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2017-01-02 07:20:47 +01:00
|
|
|
*/
|
|
|
|
public function testDuplicateParam()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function f($p, $p) {}
|
|
|
|
');
|
|
|
|
|
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);
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 07:12:19 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidParamDefault
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidParamDefault()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function f(int $p = false) {}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidParamDefault
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidDocblockParamDefault()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param int $p
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function f($p = false) {}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testValidDocblockParamDefault()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
/**
|
|
|
|
* @param int|false $p
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function f($p = false) {}
|
|
|
|
');
|
|
|
|
|
|
|
|
$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-02 21:31:18 +01:00
|
|
|
public function testByRef()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function fooFoo(string &$v) : void {}
|
|
|
|
fooFoo($a);
|
|
|
|
');
|
|
|
|
|
|
|
|
$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);
|
2017-01-02 21:31:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-02 21:31:18 +01:00
|
|
|
* @expectedExceptionMessage InvalidPassByReference
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2017-01-02 21:31:18 +01:00
|
|
|
*/
|
|
|
|
public function testBadByRef()
|
|
|
|
{
|
2017-02-02 00:11:00 +01:00
|
|
|
$this->markTestSkipped('Does not throw an error');
|
2017-01-02 21:31:18 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
function fooFoo(string &$v) : void {}
|
|
|
|
fooFoo("a");
|
|
|
|
');
|
|
|
|
|
|
|
|
$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);
|
2017-01-02 07:20:47 +01:00
|
|
|
}
|
2017-01-12 15:42:24 +01:00
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-01-12 15:42:24 +01:00
|
|
|
public function testNamespaced()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
namespace A;
|
|
|
|
|
2017-01-15 18:34:23 +01:00
|
|
|
/** @return void */
|
|
|
|
function f(int $p) {}
|
2017-01-12 15:42:24 +01:00
|
|
|
f(5);
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-12 15:42:24 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
2017-01-15 18:34:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNamespacedRootFunctionCall()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
namespace {
|
|
|
|
/** @return void */
|
|
|
|
function foo() { }
|
|
|
|
}
|
|
|
|
namespace A\B\C {
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-15 18:34:23 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
2017-01-16 06:49:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNamespacedAliasedFunctionCall()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
namespace Aye {
|
|
|
|
/** @return void */
|
|
|
|
function foo() { }
|
|
|
|
}
|
|
|
|
namespace Bee {
|
|
|
|
use Aye as A;
|
|
|
|
|
|
|
|
A\foo();
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2017-01-17 00:33:04 +01:00
|
|
|
$context = new Context();
|
2017-01-16 06:49:12 +01:00
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
}
|
2017-01-19 07:32:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-02-11 00:12:59 +01:00
|
|
|
public function testArrayKeys()
|
2017-01-19 07:32:35 +01:00
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = array_keys(["a" => 1, "b" => 2]);
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
|
|
|
$this->assertEquals('array<int, string>', (string) $context->vars_in_scope['$a']);
|
2017-02-11 00:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayValues()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$b = array_values(["a" => 1, "b" => 2]);
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2017-01-19 07:32:35 +01:00
|
|
|
$this->assertEquals('array<int, int>', (string) $context->vars_in_scope['$b']);
|
2017-02-11 00:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayCombine()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$c = array_combine(["a", "b", "c"], [1, 2, 3]);
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2017-01-19 07:32:35 +01:00
|
|
|
$this->assertEquals('array<string, int>', (string) $context->vars_in_scope['$c']);
|
2017-02-11 00:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testArrayMerge()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$d = array_merge(["a", "b", "c"], [1, 2, 3]);
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
|
|
|
$context = new Context();
|
|
|
|
$file_checker->visitAndAnalyzeMethods($context);
|
2017-01-19 07:32:35 +01:00
|
|
|
$this->assertEquals('array<int, int|string>', (string) $context->vars_in_scope['$d']);
|
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|