1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-06 05:29:00 +01:00
psalm/tests/MixinAnnotationTest.php
Daniil Gentili 1986c8b4a8
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

720 lines
22 KiB
PHP

<?php
namespace Psalm\Tests;
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
class MixinAnnotationTest extends TestCase
{
use ValidCodeAnalysisTestTrait;
use InvalidCodeAnalysisTestTrait;
/**
*
*/
public function providerValidCodeParse(): iterable
{
return [
'validSimpleAnnotations' => [
'code' => '<?php
class ParentClass {
public function __call(string $name, array $args) {}
public static function __callStatic(string $name, array $args) {}
}
class Provider {
public function getString() : string {
return "hello";
}
public function setInteger(int $i) : void {}
public static function getInt() : int {
return 5;
}
}
/** @mixin Provider */
class Child extends ParentClass {}
$child = new Child();
$a = $child->getString();
$b = $child::getInt();',
'assertions' => [
'$a' => 'string',
'$b' => 'int',
],
],
'anotherSimpleExample' => [
'code' => '<?php
/**
* @mixin B
*/
class A {
/** @var B */
private $b;
public function __construct() {
$this->b = new B();
}
public function c(string $s) : void {}
/**
* @param array<mixed> $arguments
* @return mixed
*/
public function __call(string $method, array $arguments)
{
return $this->b->$method(...$arguments);
}
}
class B {
public function b(): void {
echo "b";
}
public function c(int $s) : void {}
}
$a = new A();
$a->b();'
],
'allowConstructor' => [
'code' => '<?php
abstract class AParent {
protected int $i;
public function __construct() {
$this->i = 1;
}
}
class M {
public function __construct() {}
}
/**
* @mixin M
*/
class A extends AParent {}'
],
'implicitMixin' => [
'code' => '<?php
function foo(string $dir) : void {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir)
);
while ($iterator->valid()) {
if (!$iterator->isDot() && $iterator->isLink()) {}
$iterator->next();
}
}'
],
'wrapCustomIterator' => [
'code' => '<?php
/**
* @implements Iterator<1, 2>
*/
class Subject implements Iterator {
/**
* the index method exists
*
* @param int $index
* @return bool
*/
public function index($index) {
return true;
}
public function current() {
return 2;
}
public function next() {}
public function key() {
return 1;
}
public function valid() {
return false;
}
public function rewind() {}
}
$iter = new IteratorIterator(new Subject());
$b = $iter->index(0);',
'assertions' => [
'$b' => 'bool',
]
],
'templatedMixin' => [
'code' => '<?php
/**
* @template T
*/
abstract class Foo {
/** @return T */
abstract public function hi();
}
/**
* @mixin Foo<string>
*/
class Bar {}
$bar = new Bar();
$b = $bar->hi();',
'assertions' => [
'$b' => 'string',
]
],
'templatedMixinSelf' => [
'code' => '<?php
/**
* @template T
*/
class Animal {
/** @var T */
private $item;
/**
* @param T $item
*/
public function __construct($item) {
$this->item = $item;
}
/**
* @return T
*/
public function get() {
return $this->item;
}
}
/**
* @mixin Animal<self>
*/
class Dog {
public function __construct() {}
}
function getDog(): Dog {
return (new Dog())->get();
}'
],
'inheritPropertyAnnotations' => [
'code' => '<?php
/**
* @property string $foo
*/
class A {}
/**
* @mixin A
*/
class B {
/** @return mixed */
public function __get(string $s) {
return 5;
}
}
function toArray(B $b) : string {
return $b->foo;
}'
],
'inheritTemplatedMixinWithStatic' => [
'code' => '<?php
/**
* @template T
*/
class Mixin {
/**
* @psalm-var T
*/
private $var;
/**
* @psalm-param T $var
*/
public function __construct ($var) {
$this->var = $var;
}
/**
* @psalm-return T
*/
public function type() {
return $this->var;
}
}
/**
* @template T as object
* @mixin Mixin<T>
* @psalm-consistent-constructor
*/
abstract class Foo {
/** @var Mixin<T> */
public object $obj;
public function __call(string $name, array $args) {
return $this->obj->$name(...$args);
}
public function __callStatic(string $name, array $args) {
return (new static)->obj->$name(...$args);
}
}
/**
* @extends Foo<static>
*/
abstract class FooChild extends Foo{}
/**
* @psalm-suppress MissingConstructor
* @psalm-suppress PropertyNotSetInConstructor
*/
final class FooGrandChild extends FooChild {}
function test2() : FooGrandChild {
return FooGrandChild::type();
}
function test() : FooGrandChild {
return (new FooGrandChild)->type();
}'
],
'inheritTemplatedMixinWithStaticAndFinalClass' => [
'code' => '<?php
/**
* @template T
*/
class Mixin {
/**
* @psalm-var T
*/
private $var;
/**
* @psalm-param T $var
*/
public function __construct ($var) {
$this->var = $var;
}
/**
* @psalm-return self<T>
*/
public function getMixin() {
return $this;
}
}
/**
* @template T as object
* @mixin Mixin<T>
*/
abstract class Foo {
/** @var Mixin<T> */
public object $obj;
public function __call(string $name, array $args) {
return $this->obj->$name(...$args);
}
}
/**
* @extends Foo<static>
*/
abstract class FooChild extends Foo{}
/**
* @psalm-suppress MissingConstructor
*/
final class FooGrandChild extends FooChild {}
/**
* @psalm-return Mixin<FooGrandChild>
*/
function test() : Mixin {
return (new FooGrandChild)->getMixin();
}'
],
'mixinParseWithTextAfter' => [
'code' => '<?php
class M {}
/**
* @mixin M
* Hello
*/
class C {}'
],
'templatedMixinWithTemplateWithStatic' => [
'code' => '<?php
/**
* @template T as object
* @mixin T
*/
class Builder {
private $t;
/** @param T $t */
public function __construct(object $t) {
$this->t = $t;
}
public function __call(string $method, array $parameters) {
/** @psalm-suppress MixedMethodCall */
return $this->t->$method($parameters);
}
}
/**
* @method self active()
*/
class Model {
/**
* @return Builder<static>
*/
public function query(): Builder {
return new Builder($this);
}
public function __call(string $method, array $parameters) {
if ($method === "active") {
return new Model();
}
}
}
/** @param Builder<Model> $b */
function foo(Builder $b) : Model {
return $b->active();
}'
],
'multipleMixins' => [
'code' => '<?php
class MixinA {
function a(): string { return "foo"; }
}
class MixinB {
function b(): int { return 0; }
}
/**
* @mixin MixinA
* @mixin MixinB
*/
class Test {}
$test = new Test();
$a = $test->a();
$b = $test->b();',
'assertions' => [
'$a' => 'string',
'$b' => 'int',
],
],
'inheritMultipleTemplatedMixinsWithStatic' => [
'code' => '<?php
/**
* @template T
*/
class Mixin {
/**
* @psalm-var T
*/
private $var;
/**
* @psalm-param T $var
*/
public function __construct ($var) {
$this->var = $var;
}
/**
* @psalm-return T
*/
public function type() {
return $this->var;
}
}
/**
* @template T
*/
class OtherMixin {
/**
* @psalm-var T
*/
private $var;
/**
* @psalm-param T $var
*/
public function __construct ($var) {
$this->var = $var;
}
/**
* @psalm-return T
*/
public function other() {
return $this->var;
}
}
/**
* @template T as object
* @template T2 as string
* @mixin Mixin<T>
* @mixin OtherMixin<T2>
* @psalm-consistent-constructor
*/
abstract class Foo {
/** @var Mixin<T> */
public object $obj;
/** @var OtherMixin<T2> */
public object $otherObj;
public function __call(string $name, array $args) {
if ($name === "test") {
return $this->obj->$name(...$args);
}
return $this->otherObj->$name(...$args);
}
public function __callStatic(string $name, array $args) {
if ($name === "test") {
return (new static)->obj->$name(...$args);
}
return (new static)->otherObj->$name(...$args);
}
}
/**
* @extends Foo<static, string>
*/
abstract class FooChild extends Foo{}
/**
* @psalm-suppress MissingConstructor
* @psalm-suppress PropertyNotSetInConstructor
*/
final class FooGrandChild extends FooChild {}
function test() : FooGrandChild {
return FooGrandChild::type();
}
function testStatic() : FooGrandChild {
return (new FooGrandChild)->type();
}
function other() : string {
return FooGrandChild::other();
}
function otherStatic() : string {
return (new FooGrandChild)->other();
}'
],
'multipleMixinsWithSameMethod' => [
'code' => '<?php
class Mix1
{
public function foo(): string
{
return "";
}
}
class Mix2
{
public function foo(): string
{
return "";
}
}
/**
* @mixin Mix1
* @mixin Mix2
*/
class Bar
{
}
$bar = new Bar();
$bar->foo();'
],
'templatedMixinBindStatic' => [
'code' => '<?php
/**
* @template-covariant TModel of Model
*/
class QueryBuilder {
/**
* @return list<TModel>
*/
public function getInner() {
return [];
}
}
/**
* @mixin QueryBuilder<static>
*/
abstract class Model {}
class FooModel extends Model {}
$f = new FooModel();
$g = $f->getInner();',
'assertions' => [
'$g' => 'list<FooModel>',
]
],
];
}
/**
*
*/
public function providerInvalidCodeParse(): iterable
{
return [
'undefinedMixinClass' => [
'code' => '<?php
/** @mixin B */
class A {}',
'error_message' => 'UndefinedDocblockClass'
],
'undefinedMixinClassWithPropertyFetch' => [
'code' => '<?php
/** @mixin B */
class A {}
(new A)->foo;',
'error_message' => 'UndefinedPropertyFetch'
],
'undefinedMixinClassWithPropertyAssignment' => [
'code' => '<?php
/** @mixin B */
class A {}
(new A)->foo = "bar";',
'error_message' => 'UndefinedPropertyAssignment'
],
'undefinedMixinClassWithMethodCall' => [
'code' => '<?php
/** @mixin B */
class A {}
(new A)->foo();',
'error_message' => 'UndefinedMethod'
],
'inheritTemplatedMixinWithSelf' => [
'code' => '<?php
/**
* @template T
*/
class Mixin {
/**
* @psalm-var T
*/
private $var;
/**
* @psalm-param T $var
*/
public function __construct ($var) {
$this->var = $var;
}
/**
* @psalm-return T
*/
public function type() {
return $this->var;
}
}
/**
* @template T as object
* @mixin Mixin<T>
*/
abstract class Foo {
/** @var Mixin<T> */
public object $obj;
public function __call(string $name, array $args) {
return $this->obj->$name(...$args);
}
}
/**
* @extends Foo<self>
*/
abstract class FooChild extends Foo{}
/**
* @psalm-suppress MissingConstructor
*/
final class FooGrandChild extends FooChild {}
function test() : FooGrandChild {
return (new FooGrandChild)->type();
}',
'error_message' => 'LessSpecificReturnStatement'
],
'mixinStaticCallShouldNotPolluteContext' => [
'code' => '<?php
/**
* @template T
*/
class Foo
{
public function foobar(): void {}
}
/**
* @template T
* @mixin Foo<T>
*/
class Bar
{
public function baz(): self
{
self::foobar();
return $__tmp_mixin_var__;
}
}',
'error_message' => 'UndefinedVariable'
],
];
}
}