2016-10-25 01:20:28 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class InterfaceTest extends TestCase
|
2016-10-25 01:20:28 +02:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
use Traits\FileCheckerInvalidCodeParseTestTrait;
|
|
|
|
use Traits\FileCheckerValidCodeParseTestTrait;
|
2017-01-12 17:16:00 +01:00
|
|
|
|
2017-01-31 00:38:23 +01:00
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-31 00:38:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerValidCodeParse()
|
2017-01-31 00:38:23 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
|
|
|
'extendsAndImplements' => [
|
|
|
|
'<?php
|
|
|
|
interface A
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function fooFoo();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
interface B
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function barBar();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
interface C extends A, B
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function baz();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class D implements C
|
|
|
|
{
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function fooFoo()
|
|
|
|
{
|
|
|
|
return "hello";
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function barBar()
|
|
|
|
{
|
|
|
|
return "goodbye";
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-11-26 22:03:17 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function baz()
|
|
|
|
{
|
|
|
|
return "hello again";
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
$cee = (new D())->baz();
|
|
|
|
$dee = (new D())->fooFoo();',
|
|
|
|
'assertions' => [
|
2017-06-29 16:22:49 +02:00
|
|
|
'$cee' => 'string',
|
|
|
|
'$dee' => 'string',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'isExtendedInterface' => [
|
|
|
|
'<?php
|
2017-07-25 22:11:02 +02:00
|
|
|
interface A {}
|
|
|
|
class B implements A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
/**
|
|
|
|
* @param A $a
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function qux(A $a) { }
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
qux(new B());',
|
|
|
|
],
|
|
|
|
'isDoubleExtendedInterface' => [
|
|
|
|
'<?php
|
|
|
|
interface A {}
|
|
|
|
interface B extends A {}
|
|
|
|
class C implements B {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/**
|
|
|
|
* @param A $a
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function qux(A $a) {
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
qux(new C());',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'extendsWithMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface A
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function fooFoo();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
interface B extends A
|
|
|
|
{
|
|
|
|
public function barBar();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @return void */
|
|
|
|
function mux(B $b) {
|
|
|
|
$b->fooFoo();
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'correctInterfaceMethodSignature' => [
|
|
|
|
'<?php
|
|
|
|
interface A {
|
|
|
|
public function fooFoo(int $a) : void;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B implements A {
|
|
|
|
public function fooFoo(int $a) : void {
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'interfaceMethodImplementedInParent' => [
|
|
|
|
'<?php
|
|
|
|
interface MyInterface {
|
|
|
|
public function fooFoo(int $a) : void;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
|
|
|
public function fooFoo(int $a) : void {
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
class C extends B implements MyInterface { }',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'interfaceMethodSignatureInTrait' => [
|
|
|
|
'<?php
|
|
|
|
interface A {
|
|
|
|
public function fooFoo(int $a, int $b) : void;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
trait T {
|
|
|
|
public function fooFoo(int $a, int $b) : void {
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B implements A {
|
|
|
|
use T;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'delayedInterface' => [
|
|
|
|
'<?php
|
|
|
|
// fails in PHP, whatcha gonna do
|
|
|
|
$c = new C;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A { }
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
interface B { }
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
class C extends A implements B { }',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'typeDoesNotContainType' => [
|
|
|
|
'<?php
|
|
|
|
interface A { }
|
|
|
|
interface B {
|
|
|
|
function foo();
|
|
|
|
}
|
|
|
|
function bar(A $a) : void {
|
|
|
|
if ($a instanceof B) {
|
|
|
|
$a->foo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'abstractInterfaceImplements' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public function fnc();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
abstract class A implements I {}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'abstractInterfaceImplementsButCallMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public function foo();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
abstract class A implements I {
|
|
|
|
public function bar() : void {
|
|
|
|
$this->foo();
|
|
|
|
}
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'implementsPartialInterfaceMethods' => [
|
|
|
|
'<?php
|
|
|
|
namespace Bat;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
interface I {
|
|
|
|
public function foo();
|
|
|
|
public function bar();
|
|
|
|
}
|
|
|
|
abstract class A implements I {
|
|
|
|
public function foo() {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class B extends A {
|
|
|
|
public function bar() {
|
|
|
|
return "goodbye";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'assertions' => [],
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_levels' => ['MissingReturnType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'interfaceConstants' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {
|
|
|
|
const A = 5;
|
|
|
|
const B = "two";
|
|
|
|
const C = 3.0;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
interface I2 extends I1 {
|
|
|
|
const D = 5;
|
|
|
|
const E = "two";
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class A implements I2 {
|
|
|
|
/** @var int */
|
|
|
|
public $foo = I1::A;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var string */
|
|
|
|
public $bar = self::B;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var float */
|
|
|
|
public $bar2 = I2::C;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var int */
|
|
|
|
public $foo2 = I2::D;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
/** @var string */
|
|
|
|
public $bar3 = self::E;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'interfaceExtendsReturnType' => [
|
|
|
|
'<?php
|
|
|
|
interface A {}
|
|
|
|
interface B extends A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function foo(B $a) : A {
|
|
|
|
return $a;
|
2017-05-27 02:05:57 +02:00
|
|
|
}',
|
|
|
|
],
|
2017-11-28 00:07:38 +01:00
|
|
|
'interfaceInstanceofReturningInitial' => [
|
2017-07-10 16:34:45 +02:00
|
|
|
'<?php
|
|
|
|
interface A {}
|
|
|
|
interface B {}
|
|
|
|
|
2017-11-27 22:49:59 +01:00
|
|
|
class C implements A, B {}
|
|
|
|
|
2017-11-28 00:07:38 +01:00
|
|
|
function takesB(B $b) : void {}
|
|
|
|
|
2017-07-10 16:34:45 +02:00
|
|
|
function foo(A $i) : A {
|
2017-11-27 22:49:59 +01:00
|
|
|
if ($i instanceof B) {
|
2017-11-28 00:07:38 +01:00
|
|
|
takesB($i);
|
2017-11-27 22:49:59 +01:00
|
|
|
return $i;
|
|
|
|
}
|
2017-07-10 16:34:45 +02:00
|
|
|
return $i;
|
2017-11-27 22:49:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foo(new C);',
|
|
|
|
],
|
|
|
|
'interfaceInstanceofAndReturn' => [
|
|
|
|
'<?php
|
|
|
|
interface A {}
|
|
|
|
interface B {}
|
|
|
|
|
|
|
|
class C implements A, B {}
|
|
|
|
|
|
|
|
function foo(A $i) : B {
|
|
|
|
if ($i instanceof B) {
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
throw new \Exception("bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(new C);',
|
2017-07-10 16:34:45 +02:00
|
|
|
],
|
2017-11-27 17:43:06 +01:00
|
|
|
'extendIteratorIterator' => [
|
|
|
|
'<?php
|
|
|
|
class SomeIterator extends IteratorIterator {}',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-31 00:38:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 05:45:02 +02:00
|
|
|
* @return array
|
2017-01-31 00:38:23 +01:00
|
|
|
*/
|
2017-04-25 05:45:02 +02:00
|
|
|
public function providerFileCheckerInvalidCodeParse()
|
2017-01-31 00:38:23 +01:00
|
|
|
{
|
2017-04-25 05:45:02 +02:00
|
|
|
return [
|
2017-07-25 22:11:02 +02:00
|
|
|
'invalidInterface' => [
|
|
|
|
'<?php
|
|
|
|
class C2 implements A { }',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'noInterfaceProperties' => [
|
|
|
|
'<?php
|
|
|
|
interface A { }
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function fooFoo(A $a) : void {
|
|
|
|
if ($a->bar) {
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'NoInterfaceProperties',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'unimplementedInterfaceMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface A {
|
|
|
|
public function fooFoo();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B implements A { }',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UnimplementedInterfaceMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'mismatchingInterfaceMethodSignature' => [
|
|
|
|
'<?php
|
|
|
|
interface A {
|
|
|
|
public function fooFoo(int $a) : void;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B implements A {
|
|
|
|
public function fooFoo(string $a) : void {
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'mismatchingInterfaceMethodSignatureInTrait' => [
|
|
|
|
'<?php
|
|
|
|
interface A {
|
|
|
|
public function fooFoo(int $a, int $b) : void;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
trait T {
|
|
|
|
public function fooFoo(int $a) : void {
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B implements A {
|
|
|
|
use T;
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'mismatchingInterfaceMethodSignatureInImplementer' => [
|
|
|
|
'<?php
|
|
|
|
interface A {
|
|
|
|
public function fooFoo(int $a, int $b) : void;
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
trait T {
|
|
|
|
public function fooFoo(int $a, int $b) : void {
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B implements A {
|
|
|
|
use T;
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
public function fooFoo(int $a) : void {
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
2017-11-26 22:03:17 +01:00
|
|
|
'mismatchingReturnTypes' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {
|
|
|
|
public function foo() : string;
|
|
|
|
}
|
|
|
|
interface I2 {
|
|
|
|
public function foo() : int;
|
|
|
|
}
|
|
|
|
class A implements I1, I2 {
|
|
|
|
public function foo() : string {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
|
|
|
],
|
|
|
|
'mismatchingDocblockReturnTypes' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {
|
|
|
|
/** @return string */
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
interface I2 {
|
|
|
|
/** @return int */
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
class A implements I1, I2 {
|
|
|
|
/** @return string */
|
|
|
|
public function foo() {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'abstractInterfaceImplementsButCallUndefinedMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public function foo();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
abstract class A implements I {
|
|
|
|
public function bar() : void {
|
|
|
|
$this->foo2();
|
|
|
|
}
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UndefinedMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'abstractInterfaceImplementsWithSubclass' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public function fnc();
|
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
abstract class A implements I {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B extends A {}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'UnimplementedInterfaceMethod',
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'moreSpecificReturnType' => [
|
|
|
|
'<?php
|
|
|
|
interface A {}
|
|
|
|
interface B extends A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
function foo(A $a) : B {
|
|
|
|
return $a;
|
|
|
|
}',
|
2017-05-27 02:05:57 +02:00
|
|
|
'error_message' => 'MoreSpecificReturnType',
|
|
|
|
],
|
2017-11-26 22:03:17 +01:00
|
|
|
'interfaceReturnType' => [
|
|
|
|
'<?php
|
|
|
|
interface A {
|
|
|
|
/** @return string|null */
|
|
|
|
public function blah();
|
|
|
|
}
|
|
|
|
|
|
|
|
class B implements A {
|
|
|
|
public function blah() {
|
|
|
|
return rand(0, 10) === 4 ? "blah" : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$blah = (new B())->blah();',
|
|
|
|
'error_message' => 'MixedAssignment',
|
|
|
|
'error_levels' => [
|
|
|
|
'MissingReturnType',
|
|
|
|
],
|
|
|
|
],
|
2017-11-28 00:07:38 +01:00
|
|
|
'SKIPPED-interfaceInstanceofAndTwoReturns' => [
|
|
|
|
'<?php
|
|
|
|
interface A {}
|
|
|
|
interface B {}
|
|
|
|
|
|
|
|
class C implements A, B {}
|
|
|
|
|
|
|
|
function foo(A $i) : B {
|
|
|
|
if ($i instanceof B) {
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(new C);',
|
|
|
|
'error_message' => 'InvalidReturnTypa',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-31 00:38:23 +01:00
|
|
|
}
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|