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
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2017-01-12 17:16:00 +01:00
|
|
|
|
2017-01-31 00:38:23 +01:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2017-01-31 00:38:23 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
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
|
|
|
|
{
|
2019-07-29 01:03:12 +02:00
|
|
|
public function barBar() : void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
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 {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a): void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B implements A {
|
2018-01-11 21:50:45 +01:00
|
|
|
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 {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a): void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B {
|
2018-01-11 21:50:45 +01:00
|
|
|
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 {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a, int $b): void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
trait T {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a, int $b): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2019-07-29 01:03:12 +02:00
|
|
|
function foo() : void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2018-01-11 21:50:45 +01:00
|
|
|
function bar(A $a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
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 {
|
2019-07-29 01:03:12 +02:00
|
|
|
public function fnc() : void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
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 {
|
2019-07-29 01:03:12 +02:00
|
|
|
public function foo() : void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
abstract class A implements I {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function bar(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$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
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(B $a): A {
|
2017-04-25 05:45:02 +02:00
|
|
|
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 {}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function takesB(B $b): void {}
|
2017-11-28 00:07:38 +01:00
|
|
|
|
2018-01-11 21:50:45 +01: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 {}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(A $i): B {
|
2017-11-27 22:49:59 +01:00
|
|
|
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 {}',
|
|
|
|
],
|
2020-11-12 19:54:27 +01:00
|
|
|
'SKIPPED-suppressMismatch' => [
|
2017-11-30 06:07:36 +01:00
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
/**
|
2018-01-10 06:07:47 +01:00
|
|
|
* @return int
|
2017-11-30 06:07:36 +01:00
|
|
|
*/
|
|
|
|
public function check();
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @psalm-suppress ImplementedReturnTypeMismatch
|
|
|
|
*/
|
|
|
|
public function check(): bool
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-23 07:32:16 +01:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'implementStaticReturn' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
interface I {
|
|
|
|
/** @return A */
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A implements I {
|
|
|
|
/** @return static */
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
2018-03-22 22:55:36 +01:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'implementThisReturn' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
interface I {
|
|
|
|
/** @return A */
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A implements I {
|
|
|
|
/** @return $this */
|
|
|
|
public function foo() {
|
|
|
|
return $this;
|
|
|
|
}
|
2017-11-30 06:07:36 +01:00
|
|
|
}',
|
|
|
|
],
|
2018-01-26 17:50:29 +01:00
|
|
|
'inheritMultipleInterfacesWithDocblocks' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {
|
|
|
|
/** @return string */
|
|
|
|
public function foo();
|
|
|
|
}
|
|
|
|
interface I2 {
|
|
|
|
/** @return string */
|
|
|
|
public function bar();
|
|
|
|
}
|
|
|
|
class A implements I1, I2 {
|
|
|
|
public function foo() {
|
|
|
|
return "hello";
|
|
|
|
}
|
|
|
|
public function bar() {
|
|
|
|
return "goodbye";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
|
|
|
'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();',
|
|
|
|
],
|
2018-01-26 20:08:45 +01:00
|
|
|
'interfaceExtendsTraversible' => [
|
|
|
|
'<?php
|
|
|
|
interface Collection extends Countable, IteratorAggregate, ArrayAccess {}
|
|
|
|
|
|
|
|
function takesCollection(Collection $c): void {
|
|
|
|
takesIterable($c);
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesIterable(iterable $i): void {}',
|
|
|
|
],
|
2019-02-07 18:25:57 +01:00
|
|
|
'interfaceInstanceofInterfaceOrClass' => [
|
2018-04-18 16:59:46 +02:00
|
|
|
'<?php
|
|
|
|
interface A {}
|
|
|
|
class B extends Exception {}
|
|
|
|
|
|
|
|
function foo(Throwable $e): void {
|
|
|
|
if ($e instanceof A || $e instanceof B) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
class C extends Exception {}
|
|
|
|
interface D {}
|
|
|
|
|
|
|
|
function bar(Throwable $e): void {
|
|
|
|
if ($e instanceof C || $e instanceof D) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}',
|
|
|
|
],
|
2018-07-22 02:38:55 +02:00
|
|
|
'filterIteratorExtension' => [
|
|
|
|
'<?php
|
|
|
|
interface I2 extends Iterator {}
|
|
|
|
|
|
|
|
class DedupeIterator extends FilterIterator {
|
|
|
|
public function __construct(I2 $i) {
|
|
|
|
parent::__construct($i);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function accept() : bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-09-09 18:20:49 +02:00
|
|
|
'interfacInstanceMayContainOtherInterfaceInstance' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {}
|
|
|
|
interface I2 {}
|
|
|
|
class C implements I1,I2 {}
|
|
|
|
|
|
|
|
function f(I1 $a, I2 $b): bool {
|
|
|
|
return $a === $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<I1> $a
|
|
|
|
* @param array<I2> $b
|
|
|
|
*/
|
|
|
|
function g(array $a, array $b): bool {
|
|
|
|
return $a === $b;
|
|
|
|
}
|
|
|
|
|
|
|
|
$o = new C;
|
|
|
|
f($o, $o);',
|
|
|
|
],
|
2018-11-25 00:31:00 +01:00
|
|
|
'interfacePropertyIntersection' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?string */
|
|
|
|
public $a;
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A implements I {}
|
|
|
|
|
|
|
|
interface I {}
|
|
|
|
|
|
|
|
function takeI(I $i) : void {
|
|
|
|
if ($i instanceof A) {
|
|
|
|
echo $i->a;
|
|
|
|
$i->a = "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-11-25 17:11:33 +01:00
|
|
|
'interfacePropertyIntersectionMockPropertyAccess' => [
|
2018-11-25 00:31:00 +01:00
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/** @var ?string */
|
|
|
|
private $a;
|
|
|
|
}
|
|
|
|
|
2018-11-25 17:11:33 +01:00
|
|
|
/** @psalm-override-property-visibility */
|
2018-11-25 00:31:00 +01:00
|
|
|
interface I {}
|
|
|
|
|
|
|
|
function takeI(I $i) : void {
|
|
|
|
if ($i instanceof A) {
|
|
|
|
echo $i->a;
|
|
|
|
$i->a = "hello";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-11-25 17:11:33 +01:00
|
|
|
'interfacePropertyIntersectionMockMethodAccess' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
private function foo() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-override-method-visibility */
|
|
|
|
interface I {}
|
|
|
|
|
|
|
|
function takeI(I $i) : void {
|
|
|
|
if ($i instanceof A) {
|
|
|
|
$i->foo();
|
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function takeA(A $a) : void {
|
|
|
|
if ($a instanceof I) {
|
|
|
|
$a->foo();
|
|
|
|
}
|
2018-11-25 17:11:33 +01:00
|
|
|
}',
|
|
|
|
],
|
2018-12-21 17:01:24 +01:00
|
|
|
'docblockParamInheritance' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
/** @param string[] $f */
|
|
|
|
function foo(array $f) : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I {
|
|
|
|
/** @var string[] */
|
|
|
|
private $f = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function foo(array $f) : void {
|
|
|
|
$this->f = $f;
|
|
|
|
}
|
2019-03-08 16:16:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class C2 implements I {
|
|
|
|
/** @var string[] */
|
|
|
|
private $f = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function foo(array $f) : void {
|
|
|
|
$this->f = $f;
|
|
|
|
}
|
2018-12-21 17:01:24 +01:00
|
|
|
}',
|
|
|
|
],
|
2019-03-28 13:22:44 +01:00
|
|
|
'allowStaticCallOnInterfaceMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface IFoo {
|
2019-07-29 01:03:12 +02:00
|
|
|
public static function doFoo() : void;
|
2019-03-28 13:22:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function bar(IFoo $i) : void {
|
|
|
|
$i::doFoo();
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-03-28 13:22:44 +01:00
|
|
|
],
|
2020-12-04 19:16:05 +01:00
|
|
|
'SKIPPED-inheritSystemInterface' => [
|
2019-03-28 22:44:31 +01:00
|
|
|
'<?php
|
|
|
|
interface I extends \RecursiveIterator {}
|
|
|
|
|
|
|
|
function f(I $c): void {
|
|
|
|
$c->current();
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-03-28 22:44:31 +01:00
|
|
|
],
|
2019-05-24 18:48:37 +02:00
|
|
|
'intersectMixedTypes' => [
|
|
|
|
'<?php
|
|
|
|
interface IFoo {
|
2019-07-29 01:03:12 +02:00
|
|
|
function foo() : string;
|
2019-05-24 18:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IBar {
|
|
|
|
function foo() : string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param IFoo&IBar $i */
|
|
|
|
function iFooFirst($i) : string {
|
|
|
|
return $i->foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param IBar&IFoo $i */
|
|
|
|
function iBarFirst($i) : string {
|
|
|
|
return $i->foo();
|
|
|
|
}',
|
|
|
|
],
|
2019-05-26 19:11:43 +02:00
|
|
|
'intersectionObjectTypes' => [
|
|
|
|
'<?php
|
|
|
|
|
|
|
|
class C {}
|
|
|
|
|
|
|
|
interface IFoo {
|
|
|
|
function foo() : object;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IBar {
|
|
|
|
function foo() : C;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param IFoo&IBar $i */
|
|
|
|
function iFooFirst($i) : C {
|
|
|
|
return $i->foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param IBar&IFoo $i */
|
|
|
|
function iBarFirst($i) : C {
|
|
|
|
return $i->foo();
|
|
|
|
}',
|
|
|
|
],
|
2019-05-29 20:22:15 +02:00
|
|
|
'noTypeCoercionWhenIntersectionMatches' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {}
|
|
|
|
interface I2 {}
|
|
|
|
class A implements I1 {}
|
|
|
|
|
|
|
|
/** @param A|I2 $i */
|
|
|
|
function foo($i) : void {}
|
|
|
|
|
|
|
|
/** @param I1&I2 $i */
|
|
|
|
function bar($i) : void {
|
|
|
|
foo($i);
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-05-29 20:22:15 +02:00
|
|
|
],
|
2019-05-30 01:58:54 +02:00
|
|
|
'intersectIterators' => [
|
|
|
|
'<?php
|
|
|
|
class A {} function takesA(A $p): void {}
|
|
|
|
class B {} function takesB(B $p): void {}
|
|
|
|
|
|
|
|
/** @psalm-param iterable<A>&iterable<B> $i */
|
|
|
|
function takesIntersectionOfIterables(iterable $i): void {
|
|
|
|
foreach ($i as $c) {
|
|
|
|
takesA($c);
|
|
|
|
takesB($c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-param iterable<A&B> $i */
|
|
|
|
function takesIterableOfIntersections(iterable $i): void {
|
|
|
|
foreach ($i as $c) {
|
|
|
|
takesA($c);
|
|
|
|
takesB($c);
|
|
|
|
}
|
2019-12-20 02:42:57 +01:00
|
|
|
}',
|
|
|
|
],
|
|
|
|
'inheritDocFromObviousInterface' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function takesString($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface I2 extends I1 {
|
|
|
|
public function takesString($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
class C implements I2 {
|
|
|
|
public function takesString($type) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-05 22:24:00 +02:00
|
|
|
}',
|
2019-05-30 01:58:54 +02:00
|
|
|
],
|
2020-01-02 21:23:57 +01:00
|
|
|
'correctClassCasing' => [
|
|
|
|
'<?php
|
|
|
|
interface F {
|
|
|
|
/** @return static */
|
|
|
|
public function m(): self;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class G implements F {}
|
|
|
|
|
|
|
|
class H extends G {
|
|
|
|
public function m(): F {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function f1(F $f) : void {
|
|
|
|
$f->m()->m();
|
|
|
|
}
|
|
|
|
|
|
|
|
function f2(G $f) : void {
|
|
|
|
$f->m()->m();
|
|
|
|
}
|
|
|
|
|
|
|
|
function f3(H $f) : void {
|
|
|
|
$f->m()->m();
|
|
|
|
}'
|
|
|
|
],
|
2020-08-17 05:38:50 +02:00
|
|
|
'dontModifyAfterUnnecessaryAssertion' => [
|
|
|
|
'<?php
|
|
|
|
class A {}
|
|
|
|
interface I {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param A&I $a
|
|
|
|
* @return A&I
|
|
|
|
*/
|
|
|
|
function foo(I $a) {
|
|
|
|
/** @psalm-suppress RedundantConditionGivenDocblockType */
|
|
|
|
assert($a instanceof A);
|
|
|
|
return $a;
|
|
|
|
}'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-31 00:38:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2017-01-31 00:38:23 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
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',
|
|
|
|
],
|
2018-06-10 16:02:46 +02:00
|
|
|
'noInterfacePropertyFetch' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
interface A { }
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function fooFoo(A $a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
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
|
|
|
],
|
2018-06-10 16:02:46 +02:00
|
|
|
'noInterfacePropertyAssignment' => [
|
|
|
|
'<?php
|
|
|
|
interface A { }
|
|
|
|
|
|
|
|
function fooFoo(A $a): void {
|
|
|
|
$a->bar = 5;
|
|
|
|
}',
|
|
|
|
'error_message' => 'NoInterfaceProperties',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'unimplementedInterfaceMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface A {
|
2019-07-29 01:03:12 +02:00
|
|
|
public function fooFoo() : void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
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 {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a): void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class B implements A {
|
2018-01-11 21:50:45 +01:00
|
|
|
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 {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a, int $b): void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
trait T {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a, int $b): void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
trait T {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a, int $b): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
public function fooFoo(int $a): void {
|
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
|
|
|
],
|
2017-11-26 22:03:17 +01:00
|
|
|
'mismatchingReturnTypes' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): string;
|
2017-11-26 22:03:17 +01:00
|
|
|
}
|
|
|
|
interface I2 {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): int;
|
2017-11-26 22:03:17 +01:00
|
|
|
}
|
|
|
|
class A implements I1, I2 {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function foo(): string {
|
2017-11-26 22:03:17 +01:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
}',
|
2017-11-30 06:01:41 +01:00
|
|
|
'error_message' => 'ImplementedReturnTypeMismatch',
|
2017-11-26 22:03:17 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'abstractInterfaceImplementsButCallUndefinedMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
2019-07-29 01:03:12 +02:00
|
|
|
public function foo() : void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
abstract class A implements I {
|
2018-01-11 21:50:45 +01:00
|
|
|
public function bar(): void {
|
2017-04-25 05:45:02 +02:00
|
|
|
$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 {
|
2019-07-29 01:03:12 +02:00
|
|
|
public function fnc() : void;
|
2017-04-25 05:45:02 +02:00
|
|
|
}
|
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
|
|
|
],
|
2017-12-07 21:50:25 +01:00
|
|
|
'lessSpecificReturnStatement' => [
|
2017-04-25 05:45:02 +02:00
|
|
|
'<?php
|
|
|
|
interface A {}
|
|
|
|
interface B extends A {}
|
2017-06-29 16:22:49 +02:00
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(A $a): B {
|
2017-04-25 05:45:02 +02:00
|
|
|
return $a;
|
|
|
|
}',
|
2017-12-07 21:50:25 +01:00
|
|
|
'error_message' => 'LessSpecificReturnStatement',
|
2017-05-27 02:05:57 +02:00
|
|
|
],
|
2017-11-28 01:15:01 +01:00
|
|
|
'interfaceInstanceofAndTwoReturns' => [
|
2017-11-28 00:07:38 +01:00
|
|
|
'<?php
|
|
|
|
interface A {}
|
|
|
|
interface B {}
|
|
|
|
|
|
|
|
class C implements A, B {}
|
|
|
|
|
2018-01-11 21:50:45 +01:00
|
|
|
function foo(A $i): B {
|
2017-11-28 00:07:38 +01:00
|
|
|
if ($i instanceof B) {
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(new C);',
|
2017-12-07 21:50:25 +01:00
|
|
|
'error_message' => 'InvalidReturnStatement',
|
2017-11-28 00:07:38 +01:00
|
|
|
],
|
2018-01-23 15:09:43 +01:00
|
|
|
'deprecatedInterface' => [
|
|
|
|
'<?php
|
|
|
|
/** @deprecated */
|
|
|
|
interface Container {}
|
|
|
|
|
|
|
|
class A implements Container {}',
|
|
|
|
'error_message' => 'DeprecatedInterface',
|
|
|
|
],
|
2018-01-26 17:50:29 +01:00
|
|
|
'inheritMultipleInterfacesWithConflictingDocblocks' => [
|
|
|
|
'<?php
|
|
|
|
interface I1 {
|
2020-11-12 19:54:27 +01:00
|
|
|
/** @return string */
|
|
|
|
public function foo();
|
2018-01-26 17:50:29 +01:00
|
|
|
}
|
|
|
|
interface I2 {
|
2020-11-12 19:54:27 +01:00
|
|
|
/** @return int */
|
|
|
|
public function foo();
|
2018-01-26 17:50:29 +01:00
|
|
|
}
|
|
|
|
class A implements I1, I2 {
|
2020-11-12 19:54:27 +01:00
|
|
|
public function foo() {
|
|
|
|
return "hello";
|
|
|
|
}
|
2018-01-26 17:50:29 +01:00
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidReturnType',
|
|
|
|
],
|
2018-02-18 00:53:02 +01:00
|
|
|
'interfaceInstantiation' => [
|
|
|
|
'<?php
|
|
|
|
interface myInterface{}
|
|
|
|
new myInterface();',
|
|
|
|
'error_message' => 'InterfaceInstantiation',
|
|
|
|
],
|
2019-03-03 23:41:15 +01:00
|
|
|
'nonStaticInterfaceMethod' => [
|
|
|
|
'<?php
|
|
|
|
interface I {
|
|
|
|
public static function m(): void;
|
|
|
|
}
|
|
|
|
class C implements I {
|
|
|
|
public function m(): void {}
|
|
|
|
}',
|
2019-03-23 19:27:54 +01:00
|
|
|
'error_message' => 'MethodSignatureMismatch',
|
2019-03-03 23:41:15 +01:00
|
|
|
],
|
2019-03-28 13:22:44 +01:00
|
|
|
'staticInterfaceCall' => [
|
|
|
|
'<?php
|
|
|
|
interface Foo {
|
|
|
|
public static function doFoo();
|
|
|
|
}
|
|
|
|
|
|
|
|
Foo::doFoo();',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
],
|
2019-07-29 01:03:12 +02:00
|
|
|
'missingReturnType' => [
|
|
|
|
'<?php
|
|
|
|
interface foo {
|
|
|
|
public function withoutAnyReturnType();
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingReturnType'
|
|
|
|
],
|
|
|
|
'missingParamType' => [
|
|
|
|
'<?php
|
|
|
|
interface foo {
|
|
|
|
public function withoutAnyReturnType($s) : void;
|
|
|
|
}',
|
|
|
|
'error_message' => 'MissingParamType'
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-31 00:38:23 +01:00
|
|
|
}
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|