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 MethodCallTest 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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-12-17 00:56:23 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-12-14 18:28:38 +01:00
|
|
|
$config = new TestConfig();
|
2016-12-12 05:41:11 +01:00
|
|
|
FileChecker::clearCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidStaticInvocation
|
|
|
|
*/
|
|
|
|
public function testInvalidStaticInvocation()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class Foo {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() : void {}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
Foo::barBar();
|
2016-12-12 05:41:11 +01:00
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidNonStaticInvocation()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class Foo {
|
2016-12-30 19:09:00 +01:00
|
|
|
public static function barBar() : void {}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
(new Foo())->barBar();
|
2016-12-12 05:41:11 +01:00
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-12-17 00:56:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage MixedMethodCall
|
|
|
|
*/
|
|
|
|
public function testMixedMethodCall()
|
|
|
|
{
|
2016-12-30 02:07:42 +01:00
|
|
|
Config::getInstance()->setCustomErrorLevel('MissingPropertyType', Config::REPORT_SUPPRESS);
|
|
|
|
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
|
2016-12-17 00:56:23 +01:00
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class Foo {
|
2016-12-30 19:09:00 +01:00
|
|
|
public static function barBar() : void {}
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @var mixed */
|
|
|
|
$a = (new Foo());
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
$a->barBar();
|
2016-12-17 00:56:23 +01:00
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidStaticInvocation()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public static function fooFoo() : void {}
|
2016-12-17 00:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
B::fooFoo();
|
2016-12-17 00:56:23 +01:00
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-12-17 04:16:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage ParentNotFound
|
|
|
|
*/
|
|
|
|
public function testNoParent()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class Foo {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar() : void {
|
|
|
|
parent::barBar();
|
2016-12-17 04:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-12-12 05:41:11 +01:00
|
|
|
}
|