2016-12-15 01:24:33 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class MethodSignatureTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/** @var \PhpParser\Parser */
|
|
|
|
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-15 01:24:33 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
|
|
|
|
$config = new TestConfig();
|
|
|
|
}
|
|
|
|
|
2017-01-13 20:07:23 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-12-15 01:24:33 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
2016-12-15 01:24:33 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 18:03:22 +01:00
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 18:03:22 +01:00
|
|
|
* @expectedExceptionMessage Method B::fooFoo has more arguments than parent method A::fooFoo
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2017-01-13 18:03:22 +01:00
|
|
|
*/
|
2016-12-23 21:14:13 +01:00
|
|
|
public function testMoreArguments()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a, bool $b) : void {
|
2016-12-23 21:14:13 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a, bool $b, array $c) : void {
|
2016-12-23 21:14:13 +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-23 21:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 01:24:33 +01:00
|
|
|
/**
|
2017-01-13 20:07:23 +01:00
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
2017-01-13 18:03:22 +01:00
|
|
|
* @expectedExceptionMessage Method B::fooFoo has fewer arguments than parent method A::fooFoo
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-15 01:24:33 +01:00
|
|
|
*/
|
|
|
|
public function testFewerArguments()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a, bool $b) : void {
|
2016-12-15 01:24:33 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a) : void {
|
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-30 19:14:17 +01:00
|
|
|
* @expectedExceptionMessage Argument 1 of B::fooFoo has wrong type 'bool', expecting 'int' as defined by A::foo
|
2017-01-13 20:07:23 +01:00
|
|
|
* @return void
|
2016-12-15 01:24:33 +01:00
|
|
|
*/
|
|
|
|
public function testDifferentArguments()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
class A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a, bool $b) : void {
|
2016-12-15 01:24:33 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(bool $b, int $a) : void {
|
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
|
|
|
}
|
|
|
|
}
|