2020-10-24 06:10:22 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2020-10-24 06:10:22 +02:00
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
2021-12-04 21:55:53 +01:00
|
|
|
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
|
|
|
|
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
|
|
|
|
|
2020-10-24 06:10:22 +02:00
|
|
|
class AttributeTest extends TestCase
|
|
|
|
{
|
2021-12-04 21:55:53 +01:00
|
|
|
use InvalidCodeAnalysisTestTrait;
|
|
|
|
use ValidCodeAnalysisTestTrait;
|
2020-10-24 06:10:22 +02:00
|
|
|
|
|
|
|
/**
|
2022-01-13 20:38:17 +01:00
|
|
|
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>}>
|
2020-10-24 06:10:22 +02:00
|
|
|
*/
|
|
|
|
public function providerValidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'classAndPropertyAttributesExists' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-24 06:10:22 +02:00
|
|
|
namespace Foo;
|
|
|
|
|
2020-10-30 18:28:14 +01:00
|
|
|
#[\Attribute(\Attribute::TARGET_CLASS)]
|
2020-10-24 06:10:22 +02:00
|
|
|
class Table {
|
|
|
|
public function __construct(public string $name) {}
|
|
|
|
}
|
|
|
|
|
2020-10-30 18:28:14 +01:00
|
|
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
2020-10-24 06:10:22 +02:00
|
|
|
class Column {
|
|
|
|
public function __construct(public string $name) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Table(name: "videos")]
|
|
|
|
class Video {
|
|
|
|
#[Column(name: "id")]
|
|
|
|
public string $id = "";
|
|
|
|
|
|
|
|
#[Column(name: "title")]
|
|
|
|
public string $name = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Table(name: "users")]
|
|
|
|
class User {
|
|
|
|
public function __construct(
|
|
|
|
#[Column(name: "id")]
|
|
|
|
public string $id,
|
|
|
|
|
|
|
|
#[Column(name: "name")]
|
|
|
|
public string $name = "",
|
|
|
|
) {}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-10-24 06:10:22 +02:00
|
|
|
],
|
|
|
|
'functionAttributeExists' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-24 06:10:22 +02:00
|
|
|
namespace {
|
2020-10-30 18:28:14 +01:00
|
|
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_FUNCTION | Attribute::TARGET_PARAMETER)]
|
2020-10-24 06:10:22 +02:00
|
|
|
class Deprecated {}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Foo\Bar {
|
|
|
|
#[\Deprecated]
|
|
|
|
function foo() : void {}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-10-24 06:10:22 +02:00
|
|
|
],
|
|
|
|
'paramAttributeExists' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-24 06:10:22 +02:00
|
|
|
namespace {
|
2020-10-30 18:28:14 +01:00
|
|
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_FUNCTION | Attribute::TARGET_PARAMETER)]
|
2020-10-24 06:10:22 +02:00
|
|
|
class Deprecated {}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Foo\Bar {
|
|
|
|
function foo(#[\Deprecated] string $foo) : void {}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-10-24 06:10:22 +02:00
|
|
|
],
|
2020-10-30 22:37:16 +01:00
|
|
|
'testReflectingClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-30 22:37:16 +01:00
|
|
|
abstract class BaseAttribute {
|
|
|
|
public function __construct(public string $name) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Attribute(Attribute::TARGET_CLASS)]
|
|
|
|
class Table extends BaseAttribute {}
|
|
|
|
|
|
|
|
/** @param class-string $s */
|
|
|
|
function foo(string $s) : void {
|
|
|
|
foreach ((new ReflectionClass($s))->getAttributes(BaseAttribute::class, 2) as $attr) {
|
|
|
|
$attribute = $attr->newInstance();
|
|
|
|
echo $attribute->name;
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-10-30 22:37:16 +01:00
|
|
|
],
|
2021-12-17 00:12:54 +01:00
|
|
|
'testReflectingAllAttributes' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-12-20 09:57:06 +01:00
|
|
|
/** @var class-string $a */
|
|
|
|
$cs = stdClass::class;
|
2021-12-17 00:12:54 +01:00
|
|
|
|
2021-12-20 09:57:06 +01:00
|
|
|
$a = new ReflectionClass($cs);
|
|
|
|
$b = $a->getAttributes();
|
2021-12-17 00:12:54 +01:00
|
|
|
',
|
2021-12-20 09:57:06 +01:00
|
|
|
'assertions' => [
|
|
|
|
'$b' => 'array<array-key, ReflectionAttribute<object>>',
|
|
|
|
],
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2021-12-17 00:12:54 +01:00
|
|
|
],
|
2020-12-07 20:39:58 +01:00
|
|
|
'convertKeyedArray' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-12-07 20:39:58 +01:00
|
|
|
#[Attribute(Attribute::TARGET_CLASS)]
|
|
|
|
class Route {
|
|
|
|
private $methods = [];
|
|
|
|
/**
|
|
|
|
* @param string[] $methods
|
|
|
|
*/
|
|
|
|
public function __construct(array $methods = []) {
|
|
|
|
$this->methods = $methods;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[Route(methods: ["GET"])]
|
|
|
|
class HealthController
|
|
|
|
{}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-12-07 20:39:58 +01:00
|
|
|
],
|
2021-01-11 03:55:06 +01:00
|
|
|
'allowsRepeatableFlag' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-01-11 03:55:06 +01:00
|
|
|
#[Attribute(Attribute::TARGET_ALL|Attribute::IS_REPEATABLE)] // results in int(127)
|
|
|
|
class A {}
|
|
|
|
',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2021-01-11 03:55:06 +01:00
|
|
|
],
|
2021-07-29 20:41:08 +02:00
|
|
|
'allowsClassString' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-07-29 20:41:08 +02:00
|
|
|
|
|
|
|
#[Attribute(Attribute::TARGET_CLASS)]
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param class-string<Baz> $_className
|
|
|
|
*/
|
|
|
|
public function __construct(string $_className)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Foo(_className: Baz::class)]
|
|
|
|
class Baz {}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2021-07-29 20:41:08 +02:00
|
|
|
],
|
2021-08-01 01:22:16 +02:00
|
|
|
'allowsClassStringFromDifferentNamespace' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-08-01 01:22:16 +02:00
|
|
|
|
|
|
|
namespace NamespaceOne {
|
|
|
|
use Attribute;
|
|
|
|
|
|
|
|
#[Attribute(Attribute::TARGET_CLASS)]
|
|
|
|
class FooAttribute
|
|
|
|
{
|
|
|
|
/** @var class-string */
|
|
|
|
private string $className;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<FoobarInterface> $className
|
|
|
|
*/
|
|
|
|
public function __construct(string $className)
|
|
|
|
{
|
|
|
|
$this->className = $className;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FoobarInterface {}
|
|
|
|
|
|
|
|
class Bar implements FoobarInterface {}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NamespaceTwo {
|
|
|
|
use NamespaceOne\FooAttribute;
|
|
|
|
use NamespaceOne\Bar as ZZ;
|
|
|
|
|
|
|
|
#[FooAttribute(className: ZZ::class)]
|
|
|
|
class Baz {}
|
|
|
|
}
|
|
|
|
'
|
2021-11-09 19:52:29 +01:00
|
|
|
],
|
|
|
|
'returnTypeWillChange7.1' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-09 19:52:29 +01:00
|
|
|
|
|
|
|
namespace Rabus\PsalmReturnTypeWillChange;
|
|
|
|
|
|
|
|
use EmptyIterator;
|
|
|
|
use IteratorAggregate;
|
|
|
|
use ReturnTypeWillChange;
|
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingTemplateParam
|
|
|
|
*/
|
2021-11-09 19:52:29 +01:00
|
|
|
final class EmptyCollection implements IteratorAggregate
|
|
|
|
{
|
|
|
|
#[ReturnTypeWillChange]
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new EmptyIterator();
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '7.1'
|
2021-11-09 19:52:29 +01:00
|
|
|
],
|
|
|
|
'returnTypeWillChange8.1' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2021-11-09 19:52:29 +01:00
|
|
|
|
|
|
|
namespace Rabus\PsalmReturnTypeWillChange;
|
|
|
|
|
|
|
|
use EmptyIterator;
|
|
|
|
use IteratorAggregate;
|
|
|
|
use ReturnTypeWillChange;
|
|
|
|
|
2022-01-26 18:46:02 +01:00
|
|
|
/**
|
|
|
|
* @psalm-suppress MissingTemplateParam
|
|
|
|
*/
|
2021-11-09 19:52:29 +01:00
|
|
|
final class EmptyCollection implements IteratorAggregate
|
|
|
|
{
|
|
|
|
#[ReturnTypeWillChange]
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new EmptyIterator();
|
|
|
|
}
|
|
|
|
}',
|
2022-01-13 19:49:37 +01:00
|
|
|
'assertions' => [],
|
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.1'
|
2021-08-01 01:22:16 +02:00
|
|
|
]
|
2020-10-24 06:10:22 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-13 20:38:17 +01:00
|
|
|
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}>
|
2020-10-24 06:10:22 +02:00
|
|
|
*/
|
|
|
|
public function providerInvalidCodeParse(): iterable
|
|
|
|
{
|
|
|
|
return [
|
2020-11-22 06:52:56 +01:00
|
|
|
'attributeClassHasNoAttributeAnnotation' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-22 06:52:56 +01:00
|
|
|
class A {}
|
|
|
|
|
|
|
|
#[A]
|
|
|
|
class B {}',
|
|
|
|
'error_message' => 'InvalidAttribute',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-11-22 06:52:56 +01:00
|
|
|
],
|
2020-10-24 06:10:22 +02:00
|
|
|
'missingAttributeOnClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-24 06:10:22 +02:00
|
|
|
use Foo\Bar\Pure;
|
|
|
|
|
|
|
|
#[Pure]
|
|
|
|
class Video {}',
|
|
|
|
'error_message' => 'UndefinedAttributeClass',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-10-24 06:10:22 +02:00
|
|
|
],
|
|
|
|
'missingAttributeOnFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-24 06:10:22 +02:00
|
|
|
use Foo\Bar\Pure;
|
|
|
|
|
|
|
|
#[Pure]
|
|
|
|
function foo() : void {}',
|
|
|
|
'error_message' => 'UndefinedAttributeClass',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-10-24 06:10:22 +02:00
|
|
|
],
|
|
|
|
'missingAttributeOnParam' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-24 06:10:22 +02:00
|
|
|
use Foo\Bar\Pure;
|
|
|
|
|
|
|
|
function foo(#[Pure] string $str) : void {}',
|
|
|
|
'error_message' => 'UndefinedAttributeClass',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-10-24 06:10:22 +02:00
|
|
|
],
|
|
|
|
'tooFewArgumentsToAttributeConstructor' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-24 06:10:22 +02:00
|
|
|
namespace Foo;
|
|
|
|
|
2020-10-30 18:28:14 +01:00
|
|
|
#[\Attribute(\Attribute::TARGET_CLASS)]
|
2020-10-24 06:10:22 +02:00
|
|
|
class Table {
|
|
|
|
public function __construct(public string $name) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Table()]
|
|
|
|
class Video {}',
|
|
|
|
'error_message' => 'TooFewArguments',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-10-24 06:10:22 +02:00
|
|
|
],
|
2020-11-22 06:44:44 +01:00
|
|
|
'invalidArgument' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-22 06:44:44 +01:00
|
|
|
#[Attribute]
|
|
|
|
class Foo
|
|
|
|
{
|
|
|
|
public function __construct(int $i)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Foo("foo")]
|
|
|
|
class Bar{}',
|
|
|
|
'error_message' => 'InvalidScalarArgument',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-11-22 06:44:44 +01:00
|
|
|
],
|
2020-10-30 18:28:14 +01:00
|
|
|
'classAttributeUsedOnFunction' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-10-30 18:28:14 +01:00
|
|
|
namespace Foo;
|
|
|
|
|
|
|
|
#[\Attribute(\Attribute::TARGET_CLASS)]
|
|
|
|
class Table {
|
|
|
|
public function __construct(public string $name) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Table("videos")]
|
|
|
|
function foo() : void {}',
|
|
|
|
'error_message' => 'InvalidAttribute',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-10-30 18:28:14 +01:00
|
|
|
],
|
2020-11-22 07:15:52 +01:00
|
|
|
'interfaceCannotBeAttributeClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-22 07:15:52 +01:00
|
|
|
#[Attribute]
|
|
|
|
interface Foo {}',
|
|
|
|
'error_message' => 'InvalidAttribute',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-11-22 07:15:52 +01:00
|
|
|
],
|
|
|
|
'traitCannotBeAttributeClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-22 07:15:52 +01:00
|
|
|
#[Attribute]
|
|
|
|
interface Foo {}',
|
|
|
|
'error_message' => 'InvalidAttribute',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-11-22 07:15:52 +01:00
|
|
|
],
|
|
|
|
'abstractClassCannotBeAttributeClass' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-22 07:15:52 +01:00
|
|
|
#[Attribute]
|
|
|
|
abstract class Baz {}',
|
|
|
|
'error_message' => 'InvalidAttribute',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-11-22 07:15:52 +01:00
|
|
|
],
|
|
|
|
'abstractClassCannotHavePrivateConstructor' => [
|
2022-01-13 19:49:37 +01:00
|
|
|
'code' => '<?php
|
2020-11-22 07:15:52 +01:00
|
|
|
#[Attribute]
|
|
|
|
class Baz {
|
|
|
|
private function __construct() {}
|
|
|
|
}',
|
|
|
|
'error_message' => 'InvalidAttribute',
|
2022-01-13 19:49:37 +01:00
|
|
|
'ignored_issues' => [],
|
|
|
|
'php_version' => '8.0'
|
2020-11-22 07:15:52 +01:00
|
|
|
],
|
2020-10-24 06:10:22 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|