1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
psalm/tests/InterfaceTest.php

515 lines
16 KiB
PHP
Raw Normal View History

2016-10-25 01:20:28 +02:00
<?php
namespace Psalm\Tests;
class InterfaceTest extends TestCase
2016-10-25 01:20:28 +02:00
{
use Traits\FileCheckerInvalidCodeParseTestTrait;
use Traits\FileCheckerValidCodeParseTestTrait;
/**
* @return array
*/
public function providerFileCheckerValidCodeParse()
{
return [
'extendsAndImplements' => [
'<?php
interface A
{
/**
* @return string
*/
public function fooFoo();
}
2017-06-29 16:22:49 +02:00
interface B
{
/**
* @return string
*/
public function barBar();
}
2017-06-29 16:22:49 +02:00
interface C extends A, B
{
/**
* @return string
*/
public function baz();
}
2017-06-29 16:22:49 +02:00
class D implements C
{
/**
* @return string
*/
public function fooFoo()
{
return "hello";
}
2017-06-29 16:22:49 +02:00
/**
* @return string
*/
public function barBar()
{
return "goodbye";
}
2017-06-29 16:22:49 +02:00
/**
* @return string
*/
public function baz()
{
return "hello again";
}
}
2017-06-29 16:22:49 +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
],
],
'isExtendedInterface' => [
'<?php
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
interface A {}
class B implements A {}
2017-06-29 16:22:49 +02:00
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
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
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
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
/**
* @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());',
],
'extendsWithMethod' => [
'<?php
interface A
{
/**
* @return string
*/
public function fooFoo();
}
2017-06-29 16:22:49 +02:00
interface B extends A
{
public function barBar();
}
2017-06-29 16:22:49 +02:00
/** @return void */
function mux(B $b) {
$b->fooFoo();
2017-05-27 02:05:57 +02:00
}',
],
'correctInterfaceMethodSignature' => [
'<?php
interface A {
public function fooFoo(int $a) : void;
}
2017-06-29 16:22:49 +02:00
class B implements A {
public function fooFoo(int $a) : void {
2017-06-29 16:22:49 +02:00
}
2017-05-27 02:05:57 +02:00
}',
],
'interfaceMethodImplementedInParent' => [
'<?php
interface MyInterface {
public function fooFoo(int $a) : void;
}
2017-06-29 16:22:49 +02:00
class B {
public function fooFoo(int $a) : void {
2017-06-29 16:22:49 +02:00
}
}
2017-06-29 16:22:49 +02:00
2017-05-27 02:05:57 +02:00
class C extends B implements MyInterface { }',
],
'interfaceMethodSignatureInTrait' => [
'<?php
interface A {
public function fooFoo(int $a, int $b) : void;
}
2017-06-29 16:22:49 +02:00
trait T {
public function fooFoo(int $a, int $b) : void {
}
}
2017-06-29 16:22:49 +02:00
class B implements A {
use T;
2017-05-27 02:05:57 +02:00
}',
],
'delayedInterface' => [
'<?php
// fails in PHP, whatcha gonna do
$c = new C;
2017-06-29 16:22:49 +02:00
class A { }
2017-06-29 16:22:49 +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 { }',
],
'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
}',
],
'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 {}',
],
'abstractInterfaceImplementsButCallMethod' => [
'<?php
interface I {
public function foo();
}
2017-06-29 16:22:49 +02:00
abstract class A implements I {
public function bar() : void {
$this->foo();
}
2017-05-27 02:05:57 +02:00
}',
],
'implementsPartialInterfaceMethods' => [
'<?php
namespace Bat;
2017-06-29 16:22:49 +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'],
],
'interfaceConstants' => [
'<?php
interface I1 {
const A = 5;
const B = "two";
const C = 3.0;
}
2017-06-29 16:22:49 +02:00
interface I2 extends I1 {
const D = 5;
const E = "two";
}
2017-06-29 16:22:49 +02:00
class A implements I2 {
/** @var int */
public $foo = I1::A;
2017-06-29 16:22:49 +02:00
/** @var string */
public $bar = self::B;
2017-06-29 16:22:49 +02:00
/** @var float */
public $bar2 = I2::C;
2017-06-29 16:22:49 +02:00
/** @var int */
public $foo2 = I2::D;
2017-06-29 16:22:49 +02:00
/** @var string */
public $bar3 = self::E;
2017-05-27 02:05:57 +02:00
}',
],
'interfaceExtendsReturnType' => [
'<?php
interface A {}
interface B extends A {}
2017-06-29 16:22:49 +02:00
function foo(B $a) : A {
return $a;
2017-05-27 02:05:57 +02:00
}',
],
'interfaceInstanceofReturningInitial' => [
2017-07-10 16:34:45 +02:00
'<?php
interface A {}
interface B {}
class C implements A, B {}
function takesB(B $b) : void {}
2017-07-10 16:34:45 +02:00
function foo(A $i) : A {
if ($i instanceof B) {
takesB($i);
return $i;
}
2017-07-10 16:34:45 +02:00
return $i;
}
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
],
'extendIteratorIterator' => [
'<?php
class SomeIterator extends IteratorIterator {}',
],
'suppressMismatch' => [
'<?php
interface I {
/**
* @return Boolean
*/
public function check();
}
class C implements I
{
/**
* @psalm-suppress ImplementedReturnTypeMismatch
*/
public function check(): bool
{
return false;
}
}',
],
];
}
/**
* @return array
*/
public function providerFileCheckerInvalidCodeParse()
{
return [
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
'invalidInterface' => [
'<?php
class C2 implements A { }',
'error_message' => 'UndefinedClass',
],
'noInterfaceProperties' => [
'<?php
interface A { }
2017-06-29 16:22:49 +02:00
function fooFoo(A $a) : void {
if ($a->bar) {
2017-06-29 16:22:49 +02:00
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'NoInterfaceProperties',
],
'unimplementedInterfaceMethod' => [
'<?php
interface A {
public function fooFoo();
}
2017-06-29 16:22:49 +02:00
class B implements A { }',
2017-05-27 02:05:57 +02:00
'error_message' => 'UnimplementedInterfaceMethod',
],
'mismatchingInterfaceMethodSignature' => [
'<?php
interface A {
public function fooFoo(int $a) : void;
}
2017-06-29 16:22:49 +02:00
class B implements A {
public function fooFoo(string $a) : void {
2017-06-29 16:22:49 +02:00
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'MethodSignatureMismatch',
],
'mismatchingInterfaceMethodSignatureInTrait' => [
'<?php
interface A {
public function fooFoo(int $a, int $b) : void;
}
2017-06-29 16:22:49 +02:00
trait T {
public function fooFoo(int $a) : void {
}
}
2017-06-29 16:22:49 +02:00
class B implements A {
use T;
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'MethodSignatureMismatch',
],
'mismatchingInterfaceMethodSignatureInImplementer' => [
'<?php
interface A {
public function fooFoo(int $a, int $b) : void;
}
2017-06-29 16:22:49 +02:00
trait T {
public function fooFoo(int $a, int $b) : void {
}
}
2017-06-29 16:22:49 +02:00
class B implements A {
use T;
2017-06-29 16:22:49 +02:00
public function fooFoo(int $a) : void {
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'MethodSignatureMismatch',
],
'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' => 'ImplementedReturnTypeMismatch',
],
'abstractInterfaceImplementsButCallUndefinedMethod' => [
'<?php
interface I {
public function foo();
}
2017-06-29 16:22:49 +02:00
abstract class A implements I {
public function bar() : void {
$this->foo2();
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'UndefinedMethod',
],
'abstractInterfaceImplementsWithSubclass' => [
'<?php
interface I {
public function fnc();
}
2017-06-29 16:22:49 +02:00
abstract class A implements I {}
2017-06-29 16:22:49 +02:00
class B extends A {}',
2017-05-27 02:05:57 +02:00
'error_message' => 'UnimplementedInterfaceMethod',
],
'moreSpecificReturnType' => [
'<?php
interface A {}
interface B extends A {}
2017-06-29 16:22:49 +02:00
function foo(A $a) : B {
return $a;
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'MoreSpecificReturnType',
],
'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 01:15:01 +01:00
'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);',
2017-11-28 01:15:01 +01:00
'error_message' => 'InvalidReturnType',
],
];
}
2016-10-25 01:20:28 +02:00
}