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
|
|
|
|
*/
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
|
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-11-20 17:51:19 +01:00
|
|
|
public function bar();
|
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
|
|
|
{
|
|
|
|
public function foo()
|
|
|
|
{
|
2016-12-07 20:13:39 +01:00
|
|
|
return "hello";
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function bar()
|
|
|
|
{
|
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();
|
|
|
|
$dee = (new D())->foo();
|
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
|
|
|
|
*/
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
interface B extends A
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function baz();
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements B
|
|
|
|
{
|
|
|
|
public function foo()
|
|
|
|
{
|
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
|
|
|
|
*/
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
interface B extends A
|
|
|
|
{
|
|
|
|
public function bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return void */
|
|
|
|
function mux(B $b) {
|
|
|
|
$b->foo();
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$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 { }
|
|
|
|
|
|
|
|
function foo(A $a) : void {
|
|
|
|
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 {
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
public function foo(int $a);
|
|
|
|
}
|
|
|
|
|
|
|
|
class B implements A {
|
|
|
|
public function foo(string $a) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
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 {
|
|
|
|
public function foo(int $a) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B implements A {
|
|
|
|
public function foo(int $a) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$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 {
|
|
|
|
public function foo(int $a) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public function foo(int $a) : void {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
public function foo(int $a, int $b) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
public function foo(int $a) : void {
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
public function foo(int $a, int $b) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
public function foo(int $a, int $b) : void {
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
public function foo(int $a, int $b) : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
trait T {
|
|
|
|
public function foo(int $a, int $b) : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B implements A {
|
|
|
|
use T;
|
|
|
|
|
|
|
|
public function foo(int $a) : void {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
');
|
|
|
|
|
|
|
|
$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
|
|
|
}
|