1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-06 05:29:00 +01:00
psalm/tests/InterfaceTest.php

1044 lines
34 KiB
PHP
Raw Normal View History

2016-10-25 01:20:28 +02:00
<?php
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;
class InterfaceTest extends TestCase
2016-10-25 01:20:28 +02:00
{
2021-12-04 21:55:53 +01:00
use InvalidCodeAnalysisTestTrait;
use ValidCodeAnalysisTestTrait;
/**
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395) * Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389) * Default to sealed arrays * Fix array_merge bug * Fixes * Fix * Sealed type checks * Properly infer properties-of and get_object_vars on final classes * Fix array_map zipping * Fix tests * Fixes * Fixes * Fix more stuff * Recursively resolve type aliases * Fix typo * Fixes * Fix array_is_list assertion on keyed array * Add BC docs * Fixes * fix * Update * Update * Update * Update * Seal arrays with count assertions * Fix #8528 * Fix * Update * Improve sealed array foreach logic * get_object_vars on template properties * Fix sealed array assertion reconciler logic * Improved reconciler * Add tests * Single source of truth for test types * Fix tests * Fixup tests * Fixup tests * Fixup tests * Update * Fix tests * Fix tests * Final fixes * Fixes * Use list syntax only when needed * Fix tests * Cs-fix * Update docs * Update docs * Update docs * Update docs * Update docs * Document missing types * Update docs * Improve class-string-map docs * Update * Update * I love working on psalm :) * Keep arrays unsealed by default * Fixup tests * Fix syntax mistake * cs-fix * Fix typo * Re-import missing types * Keep strict types only in return types * argc/argv fixes * argc/argv fixes * Fix test * Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
2022-11-05 22:34:42 +01:00
*
*/
public function providerValidCodeParse(): iterable
{
return [
'extendsAndImplements' => [
'code' => '<?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' => [
'code' => '<?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' => [
'code' => '<?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 {}
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' => [
'code' => '<?php
interface A
{
/**
* @return string
*/
public function fooFoo();
}
2017-06-29 16:22:49 +02:00
interface B extends A
{
public function barBar() : void;
}
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' => [
'code' => '<?php
interface A {
2018-01-11 21:50:45 +01:00
public function fooFoo(int $a): void;
}
2017-06-29 16:22:49 +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-05-27 02:05:57 +02:00
}',
],
'interfaceMethodImplementedInParent' => [
'code' => '<?php
interface MyInterface {
2018-01-11 21:50:45 +01:00
public function fooFoo(int $a): void;
}
2017-06-29 16:22:49 +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-06-29 16:22:49 +02:00
2017-05-27 02:05:57 +02:00
class C extends B implements MyInterface { }',
],
'interfaceMethodSignatureInTrait' => [
'code' => '<?php
interface A {
2018-01-11 21:50:45 +01:00
public function fooFoo(int $a, int $b): void;
}
2017-06-29 16:22:49 +02:00
trait T {
2018-01-11 21:50:45 +01:00
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' => [
'code' => '<?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' => [
'code' => '<?php
interface A { }
interface B {
function foo() : void;
}
2018-01-11 21:50:45 +01:00
function bar(A $a): void {
if ($a instanceof B) {
$a->foo();
}
2017-05-27 02:05:57 +02:00
}',
],
'abstractInterfaceImplements' => [
'code' => '<?php
interface I {
public function fnc() : void;
}
2017-06-29 16:22:49 +02:00
2017-05-27 02:05:57 +02:00
abstract class A implements I {}',
],
'abstractInterfaceImplementsButCallMethod' => [
'code' => '<?php
interface I {
public function foo() : void;
}
2017-06-29 16:22:49 +02:00
abstract class A implements I {
2018-01-11 21:50:45 +01:00
public function bar(): void {
$this->foo();
}
2017-05-27 02:05:57 +02:00
}',
],
'implementsPartialInterfaceMethods' => [
'code' => '<?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' => [],
'ignored_issues' => ['MissingReturnType'],
],
'interfaceConstants' => [
'code' => '<?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' => [
'code' => '<?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 {
return $a;
2017-05-27 02:05:57 +02:00
}',
],
'interfaceInstanceofReturningInitial' => [
'code' => '<?php
2017-07-10 16:34:45 +02:00
interface A {}
interface B {}
class C implements A, B {}
2018-01-11 21:50:45 +01:00
function takesB(B $b): void {}
2018-01-11 21:50:45 +01: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' => [
'code' => '<?php
interface A {}
interface B {}
class C implements A, B {}
2018-01-11 21:50:45 +01:00
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' => [
'code' => '<?php
/**
* @template TKey
* @template TValue
* @extends IteratorIterator<TKey, TValue, Traversable<TKey, TValue>>
*/
class SomeIterator extends IteratorIterator {}',
],
'SKIPPED-suppressMismatch' => [
'code' => '<?php
interface I {
/**
* @return int
*/
public function check();
}
class C implements I
{
/**
* @psalm-suppress ImplementedReturnTypeMismatch
*/
public function check(): bool
{
return false;
}
}',
],
'implementStaticReturn' => [
'code' => '<?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' => [
'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;
}
}',
],
'inheritMultipleInterfacesWithDocblocks' => [
'code' => '<?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' => [
'code' => '<?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();',
],
'interfaceExtendsTraversible' => [
'code' => '<?php
/**
* @extends IteratorAggregate<mixed, mixed>
* @extends ArrayAccess<mixed, mixed>
*/
interface Collection extends Countable, IteratorAggregate, ArrayAccess {}
function takesCollection(Collection $c): void {
takesIterable($c);
}
function takesIterable(iterable $i): void {}',
],
'interfaceInstanceofInterfaceOrClass' => [
'code' => '<?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;
}',
],
'filterIteratorExtension' => [
'code' => '<?php
/**
* @extends Iterator<mixed, mixed>
*/
interface I2 extends Iterator {}
/**
2022-01-31 10:28:47 +01:00
* @extends FilterIterator<mixed, mixed, Iterator<mixed, mixed>>
*/
class DedupeIterator extends FilterIterator {
public function __construct(I2 $i) {
parent::__construct($i);
}
public function accept() : bool {
return true;
}
}',
],
'interfacInstanceMayContainOtherInterfaceInstance' => [
'code' => '<?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);',
],
'interfacePropertyIntersection' => [
'code' => '<?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";
}
}',
],
'interfacePropertyIntersectionMockPropertyAccess' => [
'code' => '<?php
class A {
/** @var ?string */
private $a;
}
/** @psalm-override-property-visibility */
interface I {}
function takeI(I $i) : void {
if ($i instanceof A) {
echo $i->a;
$i->a = "hello";
}
}',
],
'interfacePropertyIntersectionMockMethodAccess' => [
'code' => '<?php
class A {
private function foo() : void {}
}
/** @psalm-override-method-visibility */
interface I {}
function takeI(I $i) : void {
if ($i instanceof A) {
$i->foo();
}
}
function takeA(A $a) : void {
if ($a instanceof I) {
$a->foo();
}
}',
],
2018-12-21 17:01:24 +01:00
'docblockParamInheritance' => [
'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
}',
],
'allowStaticCallOnInterfaceMethod' => [
'code' => '<?php
interface IFoo {
public static function doFoo() : void;
}
function bar(IFoo $i) : void {
$i::doFoo();
2019-07-05 22:24:00 +02:00
}',
],
'SKIPPED-inheritSystemInterface' => [
'code' => '<?php
interface I extends \RecursiveIterator {}
function f(I $c): void {
$c->current();
2019-07-05 22:24:00 +02:00
}',
],
'intersectMixedTypes' => [
'code' => '<?php
interface IFoo {
function foo() : string;
}
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();
}',
],
'intersectionObjectTypes' => [
'code' => '<?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();
}',
],
'noTypeCoercionWhenIntersectionMatches' => [
'code' => '<?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
}',
],
'intersectIterators' => [
'code' => '<?php
2022-07-28 12:03:12 +02:00
interface A {} function takesA(A $p): void {}
interface 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);
}
}',
],
'inheritDocFromObviousInterface' => [
'code' => '<?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
}',
],
'correctClassCasing' => [
'code' => '<?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();
}'
],
'dontModifyAfterUnnecessaryAssertion' => [
'code' => '<?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;
}'
],
'interfaceAssertionOnClassInterfaceUnion' => [
'code' => '<?php
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',
],
];
}
/**
Add support for strict arrays, fix type alias intersection, fix array_is_list assertion on non-lists (#8395) * Immutable CodeLocation * Remove excess clones * Remove external clones * Remove leftover clones * Fix final clone issue * Immutable storages * Refactoring * Fixes * Fixes * Fix * Fix * Fixes * Simplify * Fixes * Fix * Fixes * Update * Fix * Cache global types * Fix * Update * Update * Fixes * Fixes * Refactor * Fixes * Fix * Fix * More caching * Fix * Fix * Update * Update * Fix * Fixes * Update * Refactor * Update * Fixes * Break one more test * Fix * FIx * Fix * Fix * Fix * Fix * Improve performance and readability * Equivalent logic * Fixes * Revert * Revert "Revert" This reverts commit f9175100c8452c80559234200663fd4c4f4dd889. * Fix * Fix reference bug * Make default TypeVisitor immutable * Bugfix * Remove clones * Partial refactoring * Refactoring * Fixes * Fix * Fixes * Fixes * cs-fix * Fix final bugs * Add test * Misc fixes * Update * Fixes * Experiment with removing different property * revert "Experiment with removing different property" This reverts commit ac1156e077fc4ea633530d51096d27b6e88bfdf9. * Uniform naming * Uniform naming * Hack hotfix * Clean up $_FILES ref #8621 * Undo hack, try fixing properly * Helper method * Remove redundant call * Partially fix bugs * Cleanup * Change defaults * Fix bug * Fix (?, hope this doesn't break anything else) * cs-fix * Review fixes * Bugfix * Bugfix * Improve logic * Add support for list{} and callable-list{} types, properly implement array_is_list assertions (fixes #8389) * Default to sealed arrays * Fix array_merge bug * Fixes * Fix * Sealed type checks * Properly infer properties-of and get_object_vars on final classes * Fix array_map zipping * Fix tests * Fixes * Fixes * Fix more stuff * Recursively resolve type aliases * Fix typo * Fixes * Fix array_is_list assertion on keyed array * Add BC docs * Fixes * fix * Update * Update * Update * Update * Seal arrays with count assertions * Fix #8528 * Fix * Update * Improve sealed array foreach logic * get_object_vars on template properties * Fix sealed array assertion reconciler logic * Improved reconciler * Add tests * Single source of truth for test types * Fix tests * Fixup tests * Fixup tests * Fixup tests * Update * Fix tests * Fix tests * Final fixes * Fixes * Use list syntax only when needed * Fix tests * Cs-fix * Update docs * Update docs * Update docs * Update docs * Update docs * Document missing types * Update docs * Improve class-string-map docs * Update * Update * I love working on psalm :) * Keep arrays unsealed by default * Fixup tests * Fix syntax mistake * cs-fix * Fix typo * Re-import missing types * Keep strict types only in return types * argc/argv fixes * argc/argv fixes * Fix test * Comment-out valinor code, pinging @romm pls merge https://github.com/CuyZ/Valinor/pull/246 so we can add valinor to the psalm docs :)
2022-11-05 22:34:42 +01:00
*
*/
public function providerInvalidCodeParse(): iterable
{
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' => [
'code' => '<?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
class C2 implements A { }',
'error_message' => 'UndefinedClass',
],
'noInterfacePropertyFetch' => [
'code' => '<?php
interface A { }
2017-06-29 16:22:49 +02:00
2018-01-11 21:50:45 +01: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',
],
'noInterfacePropertyAssignment' => [
'code' => '<?php
interface A { }
function fooFoo(A $a): void {
$a->bar = 5;
}',
'error_message' => 'NoInterfaceProperties',
],
'unimplementedInterfaceMethod' => [
'code' => '<?php
interface A {
public function fooFoo() : void;
}
2017-06-29 16:22:49 +02:00
class B implements A { }',
2017-05-27 02:05:57 +02:00
'error_message' => 'UnimplementedInterfaceMethod',
],
'mismatchingInterfaceMethodSignature' => [
'code' => '<?php
interface A {
2018-01-11 21:50:45 +01:00
public function fooFoo(int $a): void;
}
2017-06-29 16:22:49 +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-05-27 02:05:57 +02:00
'error_message' => 'MethodSignatureMismatch',
],
'mismatchingInterfaceMethodSignatureInTrait' => [
'code' => '<?php
interface A {
2018-01-11 21:50:45 +01:00
public function fooFoo(int $a, int $b): void;
}
2017-06-29 16:22:49 +02:00
trait T {
2018-01-11 21:50:45 +01:00
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' => [
'code' => '<?php
interface A {
2018-01-11 21:50:45 +01:00
public function fooFoo(int $a, int $b): void;
}
2017-06-29 16:22:49 +02:00
trait T {
2018-01-11 21:50:45 +01:00
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
2018-01-11 21:50:45 +01:00
public function fooFoo(int $a): void {
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'MethodSignatureMismatch',
],
'mismatchingReturnTypes' => [
'code' => '<?php
interface I1 {
2018-01-11 21:50:45 +01:00
public function foo(): string;
}
interface I2 {
2018-01-11 21:50:45 +01:00
public function foo(): int;
}
class A implements I1, I2 {
2018-01-11 21:50:45 +01:00
public function foo(): string {
return "hello";
}
}',
'error_message' => 'MethodSignatureMismatch',
],
'mismatchingDocblockReturnTypes' => [
'code' => '<?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' => [
'code' => '<?php
interface I {
public function foo() : void;
}
2017-06-29 16:22:49 +02:00
abstract class A implements I {
2018-01-11 21:50:45 +01:00
public function bar(): void {
$this->foo2();
}
}',
2017-05-27 02:05:57 +02:00
'error_message' => 'UndefinedMethod',
],
'abstractInterfaceImplementsWithSubclass' => [
'code' => '<?php
interface I {
public function fnc() : void;
}
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',
],
'lessSpecificReturnStatement' => [
'code' => '<?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 {
return $a;
}',
'error_message' => 'LessSpecificReturnStatement',
2017-05-27 02:05:57 +02:00
],
2017-11-28 01:15:01 +01:00
'interfaceInstanceofAndTwoReturns' => [
'code' => '<?php
interface A {}
interface B {}
class C implements A, B {}
2018-01-11 21:50:45 +01:00
function foo(A $i): B {
if ($i instanceof B) {
return $i;
}
return $i;
}
foo(new C);',
'error_message' => 'InvalidReturnStatement',
],
'deprecatedInterface' => [
'code' => '<?php
/** @deprecated */
interface Container {}
class A implements Container {}',
'error_message' => 'DeprecatedInterface',
],
'inheritMultipleInterfacesWithConflictingDocblocks' => [
'code' => '<?php
interface I1 {
/** @return string */
public function foo();
}
interface I2 {
/** @return int */
public function foo();
}
class A implements I1, I2 {
public function foo() {
return "hello";
}
}',
'error_message' => 'InvalidReturnType',
],
'interfaceInstantiation' => [
'code' => '<?php
interface myInterface{}
new myInterface();',
'error_message' => 'InterfaceInstantiation',
],
'nonStaticInterfaceMethod' => [
'code' => '<?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',
],
'staticInterfaceCall' => [
'code' => '<?php
interface Foo {
public static function doFoo();
}
Foo::doFoo();',
'error_message' => 'UndefinedClass',
],
'missingReturnType' => [
'code' => '<?php
interface foo {
public function withoutAnyReturnType();
}',
'error_message' => 'MissingReturnType'
],
'missingParamType' => [
'code' => '<?php
interface foo {
public function withoutAnyReturnType($s) : void;
}',
'error_message' => 'MissingParamType'
],
'missingTemplateExtendsInterface' => [
2022-01-26 18:56:35 +01:00
'code' => '<?php
/** @template T */
interface A {}
interface B extends A {}
',
'error_message' => 'MissingTemplateParam',
],
'missingTemplateExtendsNativeInterface' => [
2022-01-26 18:56:35 +01:00
'code' => '<?php
interface a extends Iterator {
}
',
'error_message' => 'MissingTemplateParam',
],
'missingTemplateExtendsNativeMultipleInterface' => [
2022-01-26 18:56:35 +01:00
'code' => '<?php
/**
* @extends Iterator<mixed, mixed>
*/
interface a extends Iterator, Traversable {
}
',
'error_message' => 'MissingTemplateParam',
],
'reconcileAfterClassInstanceof' => [
'code' => '<?php
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',
],
'reconcileAfterInterfaceInstanceof' => [
'code' => '<?php
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',
],
];
}
2016-10-25 01:20:28 +02:00
}