2016-10-25 01:20:28 +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-25 01:20:28 +02:00
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class InterfaceTest 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-25 01:20:28 +02:00
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
2016-10-25 01:20:28 +02:00
|
|
|
|
2016-12-14 18:28:38 +01:00
|
|
|
$config = new TestConfig();
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
FileChecker::clearCache();
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 17:51:19 +01:00
|
|
|
public function testExtendsAndImplements()
|
2016-10-25 01:20:28 +02:00
|
|
|
{
|
2016-11-02 07:29:00 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
2016-11-02 17:14:21 +01:00
|
|
|
interface A
|
2016-10-25 01:20:28 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo();
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 17:14:21 +01:00
|
|
|
interface B
|
2016-10-25 01:20:28 +02:00
|
|
|
{
|
2016-12-07 20:13:39 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar();
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 17:14:21 +01:00
|
|
|
interface C extends A, B
|
2016-10-25 01:20:28 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-20 17:51:19 +01:00
|
|
|
public function baz();
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 17:14:21 +01:00
|
|
|
class D implements C
|
2016-10-25 01:20:28 +02:00
|
|
|
{
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo()
|
2016-10-25 01:20:28 +02:00
|
|
|
{
|
2016-12-07 20:13:39 +01:00
|
|
|
return "hello";
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar()
|
2016-10-25 01:20:28 +02:00
|
|
|
{
|
2016-12-07 20:13:39 +01:00
|
|
|
return "goodbye";
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function baz()
|
|
|
|
{
|
2016-12-07 20:13:39 +01:00
|
|
|
return "hello again";
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 17:14:21 +01:00
|
|
|
$cee = (new D())->baz();
|
2016-12-30 19:09:00 +01:00
|
|
|
$dee = (new D())->fooFoo();
|
2016-10-25 01:20:28 +02:00
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
2016-10-25 01:20:28 +02:00
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$cee']);
|
|
|
|
$this->assertEquals('string', (string) $context->vars_in_scope['$dee']);
|
|
|
|
}
|
2016-11-20 17:51:19 +01:00
|
|
|
|
|
|
|
public function testIsExtendedInterface()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
interface A
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo();
|
2016-11-20 17:51:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
interface B extends A
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function baz();
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements B
|
|
|
|
{
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo()
|
2016-11-20 17:51:19 +01:00
|
|
|
{
|
2016-12-07 20:13:39 +01:00
|
|
|
return "hello";
|
2016-11-20 17:51:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function baz()
|
|
|
|
{
|
2016-12-07 20:13:39 +01:00
|
|
|
return "goodbye";
|
2016-11-20 17:51:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:13:39 +01:00
|
|
|
/**
|
|
|
|
* @param A $a
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-20 17:51:19 +01:00
|
|
|
function qux(A $a) {
|
|
|
|
}
|
|
|
|
|
|
|
|
qux(new C());
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExtendsWithMethod()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
interface A
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo();
|
2016-11-20 17:51:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
interface B extends A
|
|
|
|
{
|
2016-12-30 19:09:00 +01:00
|
|
|
public function barBar();
|
2016-11-20 17:51:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return void */
|
|
|
|
function mux(B $b) {
|
2016-12-30 19:09:00 +01:00
|
|
|
$b->fooFoo();
|
2016-11-20 17:51:19 +01:00
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-12-17 04:15:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage NoInterfaceProperties
|
|
|
|
*/
|
|
|
|
public function testNoInterfaceProperties()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
interface A { }
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
function fooFoo(A $a) : void {
|
2016-12-17 04:15:31 +01:00
|
|
|
if ($a->bar) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage UnimplementedInterfaceMethod
|
|
|
|
*/
|
|
|
|
public function testUnimplementedInterfaceMethod()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
interface A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo();
|
2016-12-17 04:15:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B implements A { }
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-12-29 02:33:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage MethodSignatureMismatch
|
|
|
|
*/
|
|
|
|
public function testMismatchingInterfaceMethodSignature()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
interface A {
|
2016-12-30 21:53:35 +01:00
|
|
|
public function fooFoo(int $a) : void;
|
2016-12-29 02:33:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B implements A {
|
2016-12-30 21:53:35 +01:00
|
|
|
public function fooFoo(string $a) : void {
|
2016-12-29 02:33:26 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
2016-12-30 05:37:09 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCorrectInterfaceMethodSignature()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
interface A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a) : void;
|
2016-12-30 05:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class B implements A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a) : void {
|
2016-12-30 05:37:09 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInterfaceMethodImplementedInParentAndTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
interface MyInterface {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a) : void;
|
2016-12-30 05:37:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a) : void {
|
2016-12-30 05:37:09 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends B implements MyInterface {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
2016-12-29 02:33:26 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage MethodSignatureMismatch
|
|
|
|
*/
|
|
|
|
public function testMismatchingInterfaceMethodSignatureInTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
interface A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a, int $b) : void;
|
2016-12-29 02:33:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a) : void {
|
2016-12-29 02:33:26 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-29 04:11:50 +01:00
|
|
|
|
|
|
|
class B implements A {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInterfaceMethodSignatureInTrait()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
interface A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a, int $b) : void;
|
2016-12-29 04:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a, int $b) : void {
|
2016-12-29 04:11:50 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-29 02:33:26 +01:00
|
|
|
|
|
|
|
class B implements A {
|
|
|
|
use T;
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-12-29 03:37:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage MethodSignatureMismatch
|
|
|
|
*/
|
|
|
|
public function testMismatchingInterfaceMethodSignatureInImplementer()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
interface A {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a, int $b) : void;
|
2016-12-29 03:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a, int $b) : void {
|
2016-12-29 03:37:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B implements A {
|
|
|
|
use T;
|
|
|
|
|
2016-12-30 19:09:00 +01:00
|
|
|
public function fooFoo(int $a) : void {
|
2016-12-29 03:37:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$file_checker = new FileChecker('somefile.php', $stmts);
|
|
|
|
$context = new Context('somefile.php');
|
|
|
|
$file_checker->check(true, true, $context);
|
|
|
|
}
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|