2018-07-07 06:06:05 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
2018-07-07 06:06:05 +02:00
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class MagicMethodAnnotationTest extends TestCase
|
|
|
|
{
|
2018-11-06 03:57:36 +01:00
|
|
|
use Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Traits\ValidCodeAnalysisTestTrait;
|
2018-07-07 06:06:05 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPhpDocMethodWhenUndefined(): void
|
2018-07-07 06:06:05 +02:00
|
|
|
{
|
2018-11-30 21:13:25 +01:00
|
|
|
Config::getInstance()->use_phpdoc_method_without_magic_or_parent = true;
|
2018-07-07 06:06:05 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method string getString()
|
|
|
|
* @method void setInteger(int $integer)
|
|
|
|
* @method setString(int $integer)
|
|
|
|
* @method getBool(string $foo) : bool
|
2020-05-30 22:54:16 +02:00
|
|
|
* @method (string|int)[] getArray()
|
|
|
|
* @method (callable() : string) getCallable()
|
2018-07-07 06:06:05 +02:00
|
|
|
*/
|
|
|
|
class Child {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$a = $child->getString();
|
|
|
|
$child->setInteger(4);
|
|
|
|
/** @psalm-suppress MixedAssignment */
|
|
|
|
$b = $child->setString(5);
|
|
|
|
$c = $child->getBool("hello");
|
|
|
|
$d = $child->getArray();
|
|
|
|
$e = $child->getCallable();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testPhpDocMethodWhenTemplated(): void
|
2019-07-11 18:51:43 +02:00
|
|
|
{
|
|
|
|
Config::getInstance()->use_phpdoc_method_without_magic_or_parent = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @template T */
|
|
|
|
class A {
|
|
|
|
/** @return ?T */
|
|
|
|
public function find() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
class Obj {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method Obj|null find()
|
|
|
|
*/
|
|
|
|
class C extends B {}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testAnnotationWithoutCallConfig(): void
|
2018-11-30 21:13:25 +01:00
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectExceptionMessage('UndefinedMethod');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2018-11-30 21:13:25 +01:00
|
|
|
Config::getInstance()->use_phpdoc_method_without_magic_or_parent = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method string getString()
|
|
|
|
*/
|
|
|
|
class Child {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
2019-03-07 00:32:29 +01:00
|
|
|
$child->getString();'
|
2018-11-30 21:13:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testOverrideParentClassRetunType(): void
|
2018-07-07 06:06:05 +02:00
|
|
|
{
|
2018-11-30 21:13:25 +01:00
|
|
|
Config::getInstance()->use_phpdoc_method_without_magic_or_parent = true;
|
2018-07-07 06:06:05 +02:00
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
|
|
|
public static function getMe() : self {
|
|
|
|
return new self();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method getMe() : Child
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = Child::getMe();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
|
|
|
|
$this->assertSame('Child', (string) $context->vars_in_scope['$child']);
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:42:12 +02:00
|
|
|
public function testOverrideExceptionMethodReturn() : void
|
|
|
|
{
|
|
|
|
Config::getInstance()->use_phpdoc_method_without_magic_or_parent = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method int getCode()
|
|
|
|
*/
|
|
|
|
class MyException extends Exception {}
|
|
|
|
|
|
|
|
function foo(MyException $e): int {
|
|
|
|
return $e->getCode();
|
|
|
|
}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
2018-07-07 06:06:05 +02:00
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
|
2018-07-07 06:06:05 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerValidCodeParse(): iterable
|
2018-07-07 06:06:05 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'validSimpleAnnotations' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method string getString() dsa sada
|
|
|
|
* @method void setInteger(int $integer) dsa sada
|
|
|
|
* @method setString(int $integer) dsa sada
|
|
|
|
* @method setMixed(mixed $foo) dsa sada
|
|
|
|
* @method setImplicitMixed($foo) dsa sada
|
|
|
|
* @method setAnotherImplicitMixed( $foo, $bar,$baz) dsa sada
|
|
|
|
* @method setYetAnotherImplicitMixed( $foo ,$bar, $baz ) dsa sada
|
|
|
|
* @method getBool(string $foo) : bool dsa sada
|
2020-05-30 22:54:16 +02:00
|
|
|
* @method (string|int)[] getArray() with some text dsa sada
|
|
|
|
* @method (callable() : string) getCallable() dsa sada
|
2018-07-07 06:06:05 +02:00
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$a = $child->getString();
|
|
|
|
$child->setInteger(4);
|
|
|
|
/** @psalm-suppress MixedAssignment */
|
|
|
|
$b = $child->setString(5);
|
|
|
|
$c = $child->getBool("hello");
|
|
|
|
$d = $child->getArray();
|
|
|
|
$e = $child->getCallable();
|
|
|
|
$child->setMixed("hello");
|
|
|
|
$child->setMixed(4);
|
|
|
|
$child->setImplicitMixed("hello");
|
|
|
|
$child->setImplicitMixed(4);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
'$b' => 'mixed',
|
|
|
|
'$c' => 'bool',
|
2019-10-17 07:14:33 +02:00
|
|
|
'$d' => 'array<array-key, int|string>',
|
2018-07-07 06:06:05 +02:00
|
|
|
'$e' => 'callable():string',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'validAnnotationWithDefault' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method void setArray(array $arr = array(), int $foo = 5) with some more text
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$child->setArray(["boo"]);
|
|
|
|
$child->setArray(["boo"], 8);',
|
|
|
|
],
|
2019-12-11 17:29:00 +01:00
|
|
|
'validAnnotationWithNonEmptyDefaultArray' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
|
|
|
public function __call(string $name, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method void setArray(array $arr = [1, 2, 3]) with some more text
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$child->setArray(["boo"]);
|
|
|
|
$child->setArray(["boo"]);',
|
|
|
|
],
|
|
|
|
'validAnnotationWithNonEmptyDefaultOldStyleArray' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
|
|
|
public function __call(string $name, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method void setArray(array $arr = array(1, 2, 3)) with some more text
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$child->setArray(["boo"]);
|
|
|
|
$child->setArray(["boo"]);',
|
|
|
|
],
|
2019-01-18 17:37:52 +01:00
|
|
|
'validStaticAnnotationWithDefault' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
|
|
|
public static function __callStatic(string $name, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method static string getString(int $foo) with some more text
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$a = $child::getString(5);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2018-07-07 06:06:05 +02:00
|
|
|
'validAnnotationWithVariadic' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method void setInts(int ...$foo) with some more text
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$child->setInts(1, 2, 3, 4);',
|
|
|
|
],
|
|
|
|
'validUnionAnnotations' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method setBool(string $foo, string|bool $bar) : bool dsa sada
|
|
|
|
* @method void setAnotherArray(int[]|string[] $arr = [], int $foo = 5) with some more text
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$b = $child->setBool("hello", true);
|
|
|
|
$c = $child->setBool("hello", "true");
|
|
|
|
$child->setAnotherArray(["boo"]);',
|
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'bool',
|
|
|
|
'$c' => 'bool',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'namespacedValidAnnotations' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method setBool(string $foo, string|bool $bar) : bool
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$c = $child->setBool("hello", true);
|
|
|
|
$c = $child->setBool("hello", "true");',
|
|
|
|
],
|
|
|
|
'globalMethod' => [
|
|
|
|
'<?php
|
|
|
|
/** @method void global() */
|
|
|
|
class A {
|
|
|
|
public function __call(string $s) {}
|
|
|
|
}',
|
|
|
|
],
|
2018-09-09 17:45:52 +02:00
|
|
|
'magicMethodInternalCall' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method I[] work()
|
|
|
|
*/
|
|
|
|
class I {
|
|
|
|
function __call(string $method, array $args) { return [new I, new I]; }
|
|
|
|
|
|
|
|
function zugzug(): void {
|
|
|
|
echo count($this->work());
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2018-11-30 21:13:25 +01:00
|
|
|
'magicMethodOverridesParentWithMoreSpecificType' => [
|
|
|
|
'<?php
|
|
|
|
class C {}
|
|
|
|
class D extends C {}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function foo(string $s) : C {
|
|
|
|
return new C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @method D foo(string $s) */
|
|
|
|
class B extends A {}',
|
|
|
|
],
|
|
|
|
'complicatedMagicMethodInheritance' => [
|
|
|
|
'<?php
|
|
|
|
class BaseActiveRecord {
|
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @param array $link
|
|
|
|
* @return ActiveQueryInterface
|
|
|
|
*/
|
|
|
|
public function hasMany($class, $link)
|
|
|
|
{
|
|
|
|
return new ActiveQuery();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method ActiveQuery hasMany($class, array $link)
|
|
|
|
*/
|
|
|
|
class ActiveRecord extends BaseActiveRecord {}
|
|
|
|
|
|
|
|
interface ActiveQueryInterface {}
|
|
|
|
|
|
|
|
class ActiveQuery implements ActiveQueryInterface {
|
|
|
|
/**
|
|
|
|
* @param string $tableName
|
|
|
|
* @param array $link
|
|
|
|
* @param callable $callable
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function viaTable($tableName, $link, callable $callable = null)
|
|
|
|
{
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Boom extends ActiveRecord {
|
|
|
|
/**
|
|
|
|
* @return ActiveQuery
|
|
|
|
*/
|
|
|
|
public function getUsers()
|
|
|
|
{
|
|
|
|
$query = $this->hasMany("User", ["id" => "user_id"])
|
|
|
|
->viaTable("account_to_user", ["account_id" => "id"]);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
],
|
2019-01-30 15:46:23 +01:00
|
|
|
'magicMethodReturnSelf' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method static self getSelf()
|
|
|
|
* @method $this getThis()
|
|
|
|
*/
|
|
|
|
class C {
|
|
|
|
public static function __callStatic(string $c, array $args) {}
|
|
|
|
public function __call(string $c, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$a = C::getSelf();
|
|
|
|
$b = (new C)->getThis();',
|
|
|
|
[
|
|
|
|
'$a' => 'C',
|
|
|
|
'$b' => 'C',
|
2019-03-23 19:27:54 +01:00
|
|
|
],
|
2019-01-30 15:46:23 +01:00
|
|
|
],
|
2019-01-30 17:44:12 +01:00
|
|
|
'allowMagicMethodStatic' => [
|
|
|
|
'<?php
|
|
|
|
/** @method static getStatic() */
|
|
|
|
class C {
|
|
|
|
public function __call(string $c, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class D extends C {}
|
|
|
|
|
|
|
|
$c = (new C)->getStatic();
|
|
|
|
$d = (new D)->getStatic();',
|
|
|
|
[
|
|
|
|
'$c' => 'C',
|
|
|
|
'$d' => 'D',
|
2019-03-23 19:27:54 +01:00
|
|
|
],
|
2019-01-30 17:44:12 +01:00
|
|
|
],
|
2019-02-23 02:42:36 +01:00
|
|
|
'validSimplePsalmAnnotations' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
|
|
|
public function __call(string $name, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-method string getString() dsa sada
|
|
|
|
* @psalm-method void setInteger(int $integer) dsa sada
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
2019-02-23 15:37:37 +01:00
|
|
|
$a = $child->getString();
|
|
|
|
$child->setInteger(4);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'overrideMethodAnnotations' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
|
|
|
public function __call(string $name, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method int getString() dsa sada
|
|
|
|
* @method void setInteger(string $integer) dsa sada
|
|
|
|
* @psalm-method string getString() dsa sada
|
|
|
|
* @psalm-method void setInteger(int $integer) dsa sada
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
2019-02-23 02:42:36 +01:00
|
|
|
$a = $child->getString();
|
|
|
|
$child->setInteger(4);',
|
|
|
|
'assertions' => [
|
|
|
|
'$a' => 'string',
|
|
|
|
],
|
|
|
|
],
|
2019-05-27 05:35:03 +02:00
|
|
|
'alwaysAllowAnnotationOnInterface' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method string sayHello()
|
|
|
|
*/
|
|
|
|
interface A {}
|
|
|
|
|
|
|
|
function makeConcrete() : A {
|
|
|
|
return new class implements A {
|
|
|
|
function sayHello() : string {
|
|
|
|
return "Hello";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
echo makeConcrete()->sayHello();',
|
2019-05-27 05:35:03 +02:00
|
|
|
],
|
2019-07-05 04:39:28 +02:00
|
|
|
'inheritInterfacePseudoMethodsFromParent' => [
|
|
|
|
'<?php
|
2019-08-08 23:25:56 +02:00
|
|
|
namespace Foo;
|
|
|
|
|
2019-07-05 04:39:28 +02:00
|
|
|
interface ClassMetadata {}
|
|
|
|
interface ORMClassMetadata extends ClassMetadata {}
|
|
|
|
|
|
|
|
interface EntityManagerInterface {
|
|
|
|
public function getClassMetadata() : ClassMetadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method ORMClassMetadata getClassMetadata()
|
|
|
|
* @method int getOtherMetadata()
|
|
|
|
*/
|
|
|
|
interface ORMEntityManagerInterface extends EntityManagerInterface{}
|
|
|
|
|
|
|
|
interface ConcreteEntityManagerInterface extends ORMEntityManagerInterface {}
|
|
|
|
|
|
|
|
/** @psalm-suppress InvalidReturnType */
|
|
|
|
function em(): ORMEntityManagerInterface {}
|
|
|
|
/** @psalm-suppress InvalidReturnType */
|
|
|
|
function concreteEm(): ConcreteEntityManagerInterface {}
|
|
|
|
|
|
|
|
function test(ORMClassMetadata $metadata): void {}
|
|
|
|
function test2(int $metadata): void {}
|
|
|
|
|
|
|
|
test(em()->getClassMetadata());
|
|
|
|
test(concreteEm()->getClassMetadata());
|
|
|
|
|
|
|
|
test2(em()->getOtherMetadata());
|
2019-07-05 22:24:00 +02:00
|
|
|
test2(concreteEm()->getOtherMetadata());',
|
2019-07-05 04:39:28 +02:00
|
|
|
],
|
2019-08-08 23:25:56 +02:00
|
|
|
'fullyQualifiedParam' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo {
|
|
|
|
/**
|
|
|
|
* @method void setInteger(\Closure $c)
|
|
|
|
*/
|
|
|
|
class Child {
|
|
|
|
public function __call(string $s, array $args) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
$child = new Foo\Child();
|
|
|
|
$child->setInteger(function() : void {});
|
|
|
|
}',
|
|
|
|
],
|
2020-01-10 17:29:10 +01:00
|
|
|
'allowMethodsNamedBooleanAndInteger' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method boolean(int $foo) : bool
|
|
|
|
* @method integer(int $foo) : bool
|
|
|
|
*/
|
|
|
|
class Child {
|
|
|
|
public function __call(string $name, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$child->boolean(5);
|
|
|
|
$child->integer(5);'
|
|
|
|
],
|
2020-02-02 00:21:32 +01:00
|
|
|
'overrideWithSelfBeforeMethodName' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public static function make(): self {
|
|
|
|
return new self();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method static self make()
|
|
|
|
*/
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
function makeB(): B {
|
|
|
|
return B::make();
|
|
|
|
}'
|
|
|
|
],
|
2020-02-26 19:50:45 +01:00
|
|
|
'validMethodAsAnnotation' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method string as(string $value)
|
|
|
|
*/
|
|
|
|
class Foo {}'
|
|
|
|
],
|
2020-03-11 23:27:07 +01:00
|
|
|
'annotationWithSealedSuppressingUndefinedMagicMethod' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
|
|
|
public function __call(string $name, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method string getString()
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
/** @psalm-suppress UndefinedMagicMethod */
|
|
|
|
$child->foo();'
|
|
|
|
],
|
2020-05-08 13:28:12 +02:00
|
|
|
'allowFinalOverrider' => [
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function foo()
|
|
|
|
{
|
|
|
|
return new static();
|
|
|
|
}
|
2020-08-06 01:39:27 +02:00
|
|
|
|
|
|
|
final public function __construct() {}
|
2020-05-08 13:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method static B foo()
|
|
|
|
*/
|
|
|
|
final class B extends A {}'
|
|
|
|
],
|
2020-06-20 21:17:19 +02:00
|
|
|
'namespacedMethod' => [
|
|
|
|
'<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
interface FooInterface {}
|
|
|
|
|
|
|
|
/**
|
2020-06-20 21:30:47 +02:00
|
|
|
* @method \IteratorAggregate<int, FooInterface> getAll():\IteratorAggregate
|
2020-06-20 21:17:19 +02:00
|
|
|
*/
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
private \IteratorAggregate $items;
|
|
|
|
|
2020-06-20 21:30:47 +02:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedReturnTypeCoercion
|
|
|
|
*/
|
2020-06-20 21:17:19 +02:00
|
|
|
public function getAll(): \IteratorAggregate
|
|
|
|
{
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct(\IteratorAggregate $foos)
|
|
|
|
{
|
|
|
|
$this->items = $foos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedReturnTypeCoercion
|
2020-06-20 21:30:47 +02:00
|
|
|
* @method \IteratorAggregate<int, FooInterface> getAll():\IteratorAggregate
|
2020-06-20 21:17:19 +02:00
|
|
|
*/
|
|
|
|
class Bar
|
|
|
|
{
|
|
|
|
private \IteratorAggregate $items;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-suppress MixedReturnTypeCoercion
|
|
|
|
*/
|
|
|
|
public function getAll(): \IteratorAggregate
|
|
|
|
{
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct(\IteratorAggregate $foos)
|
|
|
|
{
|
|
|
|
$this->items = $foos;
|
|
|
|
}
|
|
|
|
}'
|
|
|
|
],
|
2020-08-18 18:13:27 +02:00
|
|
|
'parseFloatInDefault' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo {
|
|
|
|
/**
|
|
|
|
* @method int randomInt()
|
|
|
|
* @method void takesFloat($a = 0.1)
|
|
|
|
*/
|
|
|
|
class G
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param array $attributes
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __call($method, $attributes)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Bar {
|
|
|
|
(new \Foo\G)->randomInt();
|
|
|
|
}'
|
|
|
|
],
|
2020-08-29 17:46:24 +02:00
|
|
|
'namespacedUnion' => [
|
|
|
|
'<?php
|
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method string bar(\DateTimeInterface|\DateInterval|self $a, Cache|\Exception $e)
|
|
|
|
*/
|
|
|
|
class Cache {
|
|
|
|
public function __call(string $method, array $args) {
|
|
|
|
return $method;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(new Cache)->bar(new \DateTime(), new Cache());'
|
|
|
|
],
|
2018-07-07 06:06:05 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-01 21:55:20 +01:00
|
|
|
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
|
2018-07-07 06:06:05 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function providerInvalidCodeParse(): iterable
|
2018-07-07 06:06:05 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'annotationWithBadDocblock' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method string getString(\)
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
|
|
|
'annotationWithByRefParam' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method string getString(&$a)
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
|
|
|
'annotationWithSealed' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method string getString()
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
$child->getString();
|
|
|
|
$child->foo();',
|
2020-01-06 20:23:33 +01:00
|
|
|
'error_message' => 'UndefinedMagicMethod - src' . DIRECTORY_SEPARATOR . 'somefile.php:13:29 - Magic method Child::foo does not exist',
|
2018-07-07 06:06:05 +02:00
|
|
|
],
|
|
|
|
'annotationInvalidArg' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method setString(int $integer)
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$child->setString("five");',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'unionAnnotationInvalidArg' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method setBool(string $foo, string|bool $bar) : bool dsa sada
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$b = $child->setBool("hello", 5);',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
|
|
|
],
|
|
|
|
'validAnnotationWithInvalidVariadicCall' => [
|
|
|
|
'<?php
|
|
|
|
class ParentClass {
|
2018-07-17 04:48:53 +02:00
|
|
|
public function __call(string $name, array $args) {}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method void setInts(int ...$foo) with some more text
|
|
|
|
*/
|
|
|
|
class Child extends ParentClass {}
|
|
|
|
|
|
|
|
$child = new Child();
|
|
|
|
|
|
|
|
$child->setInts([1, 2, 3]);',
|
|
|
|
'error_message' => 'InvalidArgument',
|
|
|
|
],
|
2018-11-30 21:13:25 +01:00
|
|
|
'magicMethodOverridesParentWithDifferentReturnType' => [
|
|
|
|
'<?php
|
|
|
|
class C {}
|
|
|
|
class D {}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function foo(string $s) : C {
|
|
|
|
return new C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @method D foo(string $s) */
|
|
|
|
class B extends A {}',
|
2020-08-22 16:00:36 +02:00
|
|
|
'error_message' => 'ImplementedReturnTypeMismatch - src' . DIRECTORY_SEPARATOR . 'somefile.php:11:33',
|
2018-11-30 21:13:25 +01:00
|
|
|
],
|
|
|
|
'magicMethodOverridesParentWithDifferentParamType' => [
|
|
|
|
'<?php
|
|
|
|
class C {}
|
|
|
|
class D extends C {}
|
|
|
|
|
|
|
|
class A {
|
|
|
|
public function foo(string $s) : C {
|
|
|
|
return new C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @method D foo(int $s) */
|
|
|
|
class B extends A {}',
|
2020-08-22 16:00:36 +02:00
|
|
|
'error_message' => 'ImplementedParamTypeMismatch - src' . DIRECTORY_SEPARATOR . 'somefile.php:11:21',
|
2018-11-30 21:13:25 +01:00
|
|
|
],
|
2019-09-08 04:30:09 +02:00
|
|
|
'parseBadMethodAnnotation' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method aaa
|
|
|
|
*/
|
|
|
|
class AAA {
|
|
|
|
function __call() {
|
|
|
|
echo $b."\n";
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2020-04-09 18:16:58 +02:00
|
|
|
'methodwithDash' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* A test class
|
|
|
|
*
|
|
|
|
* @method ClientInterface exchange-connect(array $options = [])
|
|
|
|
*/
|
|
|
|
abstract class TestClassA {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2020-05-12 00:39:07 +02:00
|
|
|
'methodWithAmpersandAndSpace' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method void alloc(string & $result)
|
|
|
|
*/
|
|
|
|
class Foo {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2020-06-11 17:28:41 +02:00
|
|
|
'inheritSealedMethods' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @psalm-seal-methods
|
|
|
|
*/
|
|
|
|
class A {
|
|
|
|
public function __call(string $method, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$b->foo();',
|
|
|
|
'error_message' => 'UndefinedMagicMethod',
|
|
|
|
],
|
2020-07-01 15:00:33 +02:00
|
|
|
'lonelyMethod' => [
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
class C {}',
|
|
|
|
'error_message' => 'InvalidDocblock',
|
|
|
|
],
|
2018-07-07 06:06:05 +02:00
|
|
|
];
|
|
|
|
}
|
2020-06-16 04:36:42 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSealAllMethodsWithoutFoo(): void
|
2020-06-16 04:36:42 +02:00
|
|
|
{
|
|
|
|
Config::getInstance()->seal_all_methods = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __call(string $method, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$b->foo();
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
|
|
|
$error_message = 'UndefinedMagicMethod';
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
|
|
|
$this->expectExceptionMessage($error_message);
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSealAllMethodsWithFoo(): void
|
2020-06-16 04:36:42 +02:00
|
|
|
{
|
|
|
|
Config::getInstance()->seal_all_methods = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __call(string $method, array $args) {}
|
|
|
|
public function foo(): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$b->foo();
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSealAllMethodsWithFooInSubclass(): void
|
2020-06-16 04:36:42 +02:00
|
|
|
{
|
|
|
|
Config::getInstance()->seal_all_methods = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __call(string $method, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {
|
|
|
|
public function foo(): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$b->foo();
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSealAllMethodsWithFooAnnotated(): void
|
2020-06-16 04:36:42 +02:00
|
|
|
{
|
|
|
|
Config::getInstance()->seal_all_methods = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @method foo(): int */
|
|
|
|
class A {
|
|
|
|
public function __call(string $method, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$b->foo();
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function testSealAllMethodsSetToFalse(): void
|
2020-06-16 04:36:42 +02:00
|
|
|
{
|
|
|
|
Config::getInstance()->seal_all_methods = false;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
class A {
|
|
|
|
public function __call(string $method, array $args) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class B extends A {}
|
|
|
|
|
|
|
|
$b = new B();
|
|
|
|
$b->foo();
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', new Context());
|
|
|
|
}
|
2018-07-07 06:06:05 +02:00
|
|
|
}
|