2016-10-25 01:20:28 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2016-10-25 01:20:28 +02:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2021-12-03 21:25:22 +01:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2017-04-25 05:45:02 +02:00
|
|
|
class InterfaceTest extends TestCase
|
2016-10-25 01:20:28 +02:00
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2017-01-12 17:16:00 +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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-07-25 22:11:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
// 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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => ['MissingReturnType'],
|
2017-04-25 05:45:02 +02:00
|
|
|
],
|
|
|
|
'interfaceConstants' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-07-10 16:34:45 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-27 22:49:59 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @template TKey
|
|
|
|
* @template TValue
|
|
|
|
* @extends IteratorIterator<TKey, TValue, Traversable<TKey, TValue>>
|
|
|
|
*/
|
2017-11-27 17:43:06 +01:00
|
|
|
class SomeIterator extends IteratorIterator {}',
|
|
|
|
],
|
2020-11-12 19:54:27 +01:00
|
|
|
'SKIPPED-suppressMismatch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-30 06:07:36 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-23 07:32:16 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-03-22 22:55:36 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-26 17:50:29 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-26 17:50:29 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @extends IteratorAggregate<mixed, mixed>
|
|
|
|
* @extends ArrayAccess<mixed, mixed>
|
|
|
|
*/
|
2018-01-26 20:08:45 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-04-18 16:59:46 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @extends Iterator<mixed, mixed>
|
|
|
|
*/
|
2018-07-22 02:38:55 +02:00
|
|
|
interface I2 extends Iterator {}
|
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
2022-01-31 10:28:47 +01:00
|
|
|
* @extends FilterIterator<mixed, mixed, Iterator<mixed, mixed>>
|
2022-01-26 18:46:02 +01:00
|
|
|
*/
|
2018-07-22 02:38:55 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-09-09 18:20:49 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-25 00:31:00 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-25 00:31:00 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-11-25 17:11:33 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-12-21 17:01:24 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-28 13:22:44 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-28 22:44:31 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-05-24 18:48:37 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-05-26 19:11:43 +02:00
|
|
|
|
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-05-29 20:22:15 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2022-07-28 12:03:12 +02:00
|
|
|
interface A {} function takesA(A $p): void {}
|
|
|
|
interface B {} function takesB(B $p): void {}
|
2019-05-30 01:58:54 +02:00
|
|
|
|
|
|
|
/** @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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-12-20 02:42:57 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-01-02 21:23:57 +01:00
|
|
|
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();
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-01-02 21:23:57 +01:00
|
|
|
],
|
2020-08-17 05:38:50 +02:00
|
|
|
'dontModifyAfterUnnecessaryAssertion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-08-17 05:38:50 +02:00
|
|
|
class A {}
|
|
|
|
interface I {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param A&I $a
|
|
|
|
* @return A&I
|
|
|
|
*/
|
|
|
|
function foo(I $a) {
|
|
|
|
/** @psalm-suppress RedundantConditionGivenDocblockType */
|
|
|
|
assert($a instanceof A);
|
|
|
|
return $a;
|
2022-12-18 17:15:15 +01:00
|
|
|
}',
|
2020-08-17 05:38:50 +02:00
|
|
|
],
|
2021-02-24 06:05:12 +01:00
|
|
|
'interfaceAssertionOnClassInterfaceUnion' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-02-24 06:05:12 +01:00
|
|
|
class SomeClass {}
|
|
|
|
|
|
|
|
interface SomeInterface {
|
|
|
|
public function doStuff(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
function takesAorB(SomeClass|SomeInterface $some): void {
|
|
|
|
if ($some instanceof SomeInterface) {
|
|
|
|
$some->doStuff();
|
|
|
|
}
|
2022-01-22 01:00:45 +01:00
|
|
|
}',
|
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0',
|
2021-02-24 06:05:12 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-07-25 22:11:02 +02:00
|
|
|
class C2 implements A { }',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
],
|
2018-06-10 16:02:46 +02:00
|
|
|
'noInterfacePropertyFetch' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-06-10 16:02:46 +02:00
|
|
|
interface A { }
|
|
|
|
|
|
|
|
function fooFoo(A $a): void {
|
|
|
|
$a->bar = 5;
|
|
|
|
}',
|
|
|
|
'error_message' => 'NoInterfaceProperties',
|
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
'unimplementedInterfaceMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-26 22:03:17 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-26 22:03:17 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-04-25 05:45:02 +02:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2017-11-28 00:07:38 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-23 15:09:43 +01:00
|
|
|
/** @deprecated */
|
|
|
|
interface Container {}
|
|
|
|
|
|
|
|
class A implements Container {}',
|
|
|
|
'error_message' => 'DeprecatedInterface',
|
|
|
|
],
|
2018-01-26 17:50:29 +01:00
|
|
|
'inheritMultipleInterfacesWithConflictingDocblocks' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-01-26 17:50:29 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2018-02-18 00:53:02 +01:00
|
|
|
interface myInterface{}
|
|
|
|
new myInterface();',
|
|
|
|
'error_message' => 'InterfaceInstantiation',
|
|
|
|
],
|
2019-03-03 23:41:15 +01:00
|
|
|
'nonStaticInterfaceMethod' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-03 23:41:15 +01:00
|
|
|
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' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-03-28 13:22:44 +01:00
|
|
|
interface Foo {
|
|
|
|
public static function doFoo();
|
|
|
|
}
|
|
|
|
|
|
|
|
Foo::doFoo();',
|
|
|
|
'error_message' => 'UndefinedClass',
|
|
|
|
],
|
2019-07-29 01:03:12 +02:00
|
|
|
'missingReturnType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-07-29 01:03:12 +02:00
|
|
|
interface foo {
|
|
|
|
public function withoutAnyReturnType();
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'MissingReturnType',
|
2019-07-29 01:03:12 +02:00
|
|
|
],
|
|
|
|
'missingParamType' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2019-07-29 01:03:12 +02:00
|
|
|
interface foo {
|
|
|
|
public function withoutAnyReturnType($s) : void;
|
|
|
|
}',
|
2022-12-18 17:15:15 +01:00
|
|
|
'error_message' => 'MissingParamType',
|
2019-07-29 01:03:12 +02:00
|
|
|
],
|
2022-01-26 18:46:02 +01:00
|
|
|
'missingTemplateExtendsInterface' => [
|
2022-01-26 18:56:35 +01:00
|
|
|
'code' => '<?php
|
2022-01-26 18:46:02 +01:00
|
|
|
/** @template T */
|
|
|
|
interface A {}
|
|
|
|
interface B extends A {}
|
|
|
|
',
|
|
|
|
'error_message' => 'MissingTemplateParam',
|
|
|
|
],
|
|
|
|
'missingTemplateExtendsNativeInterface' => [
|
2022-01-26 18:56:35 +01:00
|
|
|
'code' => '<?php
|
2022-01-26 18:46:02 +01:00
|
|
|
interface a extends Iterator {
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'MissingTemplateParam',
|
|
|
|
],
|
|
|
|
'missingTemplateExtendsNativeMultipleInterface' => [
|
2022-01-26 18:56:35 +01:00
|
|
|
'code' => '<?php
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @extends Iterator<mixed, mixed>
|
|
|
|
*/
|
|
|
|
interface a extends Iterator, Traversable {
|
|
|
|
}
|
|
|
|
',
|
|
|
|
'error_message' => 'MissingTemplateParam',
|
|
|
|
],
|
2021-01-27 18:48:32 +01:00
|
|
|
'reconcileAfterClassInstanceof' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-01-27 18:48:32 +01:00
|
|
|
interface Base {}
|
|
|
|
|
|
|
|
class E implements Base {
|
|
|
|
public function bar() : void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function foobar(Base $foo) : void {
|
|
|
|
if ($foo instanceof E) {
|
|
|
|
$foo->bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
$foo->bar();
|
|
|
|
}',
|
2021-12-03 21:25:22 +01:00
|
|
|
'error_message' => 'UndefinedInterfaceMethod - src' . DIRECTORY_SEPARATOR . 'somefile.php:13:31',
|
2021-01-27 18:48:32 +01:00
|
|
|
],
|
|
|
|
'reconcileAfterInterfaceInstanceof' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-01-27 18:48:32 +01:00
|
|
|
interface Base {}
|
|
|
|
|
|
|
|
interface E extends Base {
|
|
|
|
public function bar() : void;
|
|
|
|
}
|
|
|
|
|
|
|
|
function foobar(Base $foo) : void {
|
|
|
|
if ($foo instanceof E) {
|
|
|
|
$foo->bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
$foo->bar();
|
|
|
|
}',
|
2021-12-03 21:25:22 +01:00
|
|
|
'error_message' => 'UndefinedInterfaceMethod - src' . DIRECTORY_SEPARATOR . 'somefile.php:13:31',
|
2021-01-27 18:48:32 +01:00
|
|
|
],
|
2017-04-25 05:45:02 +02:00
|
|
|
];
|
2017-01-31 00:38:23 +01:00
|
|
|
}
|
2016-10-25 01:20:28 +02:00
|
|
|
}
|