1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
psalm/tests/AttributeTest.php

816 lines
28 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
use Psalm\Context;
2021-12-04 21:55:53 +01:00
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
use const DIRECTORY_SEPARATOR;
class AttributeTest extends TestCase
{
2021-12-04 21:55:53 +01:00
use InvalidCodeAnalysisTestTrait;
use ValidCodeAnalysisTestTrait;
public function testStubsWithDifferentAttributes(): void
{
$this->addStubFile(
'stubOne.phpstub',
'<?php
#[Attribute]
class Attr {}
#[Attr]
class Foo {}
2022-12-18 17:15:15 +01:00
',
);
$this->addFile(
'somefile.php',
'<?php
class Foo {}
2022-12-18 17:15:15 +01:00
',
);
$this->analyzeFile('somefile.php', new Context());
}
public function providerValidCodeParse(): iterable
{
return [
'classAndPropertyAttributesExists' => [
'code' => '<?php
namespace Foo;
#[\Attribute(\Attribute::TARGET_CLASS)]
class Table {
public function __construct(public string $name) {}
}
#[\Attribute(\Attribute::TARGET_PROPERTY)]
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 = "",
) {}
}',
],
'functionAttributeExists' => [
'code' => '<?php
namespace {
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_FUNCTION | Attribute::TARGET_PARAMETER)]
class Deprecated {}
}
namespace Foo\Bar {
#[\Deprecated]
function foo() : void {}
}',
],
'paramAttributeExists' => [
'code' => '<?php
namespace {
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_FUNCTION | Attribute::TARGET_PARAMETER)]
class Deprecated {}
}
namespace Foo\Bar {
function foo(#[\Deprecated] string $foo) : void {}
}',
],
'testReflectingClass' => [
'code' => '<?php
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;
}
}',
'assertions' => [],
'ignored_issues' => [],
2022-12-18 17:15:15 +01:00
'php_version' => '8.0',
],
2021-12-17 00:12:54 +01:00
'testReflectingAllAttributes' => [
'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' => 'list<ReflectionAttribute<object>>',
2021-12-20 09:57:06 +01:00
],
'ignored_issues' => [],
2022-12-18 17:15:15 +01:00
'php_version' => '8.0',
2021-12-17 00:12:54 +01:00
],
'convertKeyedArray' => [
'code' => '<?php
#[Attribute(Attribute::TARGET_CLASS)]
class Route {
private $methods = [];
/**
* @param string[] $methods
*/
public function __construct(array $methods = []) {
$this->methods = $methods;
}
}
#[Route(methods: ["GET"])]
class HealthController
{}',
],
'allowsRepeatableFlag' => [
'code' => '<?php
#[Attribute(Attribute::TARGET_ALL|Attribute::IS_REPEATABLE)] // results in int(127)
class A {}
',
],
2021-07-29 20:41:08 +02:00
'allowsClassString' => [
'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 {}',
],
'allowsClassStringFromDifferentNamespace' => [
'code' => '<?php
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 {}
}
2022-12-18 17:15:15 +01:00
',
],
'returnTypeWillChange7.1' => [
'code' => '<?php
namespace Rabus\PsalmReturnTypeWillChange;
use EmptyIterator;
use IteratorAggregate;
use ReturnTypeWillChange;
/**
* @psalm-suppress MissingTemplateParam
*/
final class EmptyCollection implements IteratorAggregate
{
#[ReturnTypeWillChange]
public function getIterator()
{
return new EmptyIterator();
}
}',
'assertions' => [],
'ignored_issues' => [],
2022-12-18 17:15:15 +01:00
'php_version' => '7.1',
],
'returnTypeWillChange8.1' => [
'code' => '<?php
namespace Rabus\PsalmReturnTypeWillChange;
use EmptyIterator;
use IteratorAggregate;
use ReturnTypeWillChange;
/**
* @psalm-suppress MissingTemplateParam
*/
final class EmptyCollection implements IteratorAggregate
{
#[ReturnTypeWillChange]
public function getIterator()
{
return new EmptyIterator();
}
}',
'assertions' => [],
'ignored_issues' => [],
2022-12-18 17:15:15 +01:00
'php_version' => '8.1',
],
'allowDynamicProperties' => [
2022-10-16 13:59:15 +02:00
'code' => '<?php
namespace AllowDynamicPropertiesAttribute;
2022-08-22 16:55:26 +02:00
use AllowDynamicProperties;
#[AllowDynamicProperties]
class Foo
2022-08-22 16:58:23 +02:00
{}
',
2023-03-03 09:37:08 +01:00
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.2',
],
'sensitiveParameter' => [
2022-10-16 13:59:15 +02:00
'code' => '<?php
namespace SensitiveParameter;
2022-08-22 16:55:26 +02:00
use SensitiveParameter;
class HelloWorld {
public function __construct(
2022-08-22 16:55:26 +02:00
#[SensitiveParameter] string $password
) {}
2022-08-22 16:58:23 +02:00
}
',
2023-03-03 09:37:08 +01:00
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.2',
],
'createObjectAsAttributeArg' => [
'code' => '<?php
#[Attribute]
class B
{
public function __construct(?array $listOfB = null) {}
}
#[Attribute(Attribute::TARGET_CLASS)]
class A
{
/**
* @param B[] $listOfB
*/
public function __construct(?array $listOfB = null) {}
}
#[A([new B])]
class C {}
',
],
2022-02-21 17:44:59 +01:00
'selfInClassAttribute' => [
2022-02-26 21:28:15 +01:00
'code' => '<?php
#[Attribute]
class SomeAttr
{
/** @param class-string $class */
public function __construct(string $class) {}
}
#[SomeAttr(self::class)]
2022-02-21 17:44:59 +01:00
class A
{
#[SomeAttr(self::class)]
public const CONST = "const";
#[SomeAttr(self::class)]
public string $foo = "bar";
#[SomeAttr(self::class)]
public function baz(): void {}
}
',
],
'parentInClassAttribute' => [
2022-02-26 21:28:15 +01:00
'code' => '<?php
2022-02-21 17:44:59 +01:00
#[Attribute]
class SomeAttr
{
/** @param class-string $class */
public function __construct(string $class) {}
}
class A {}
#[SomeAttr(parent::class)]
2022-02-21 17:44:59 +01:00
class B extends A
{
#[SomeAttr(parent::class)]
public const CONST = "const";
#[SomeAttr(parent::class)]
public string $foo = "bar";
#[SomeAttr(parent::class)]
public function baz(): void {}
}
',
],
'selfInInterfaceAttribute' => [
2022-02-26 21:28:15 +01:00
'code' => '<?php
2022-02-21 17:44:59 +01:00
#[Attribute]
class SomeAttr
{
/** @param class-string $class */
public function __construct(string $class) {}
}
#[SomeAttr(self::class)]
2022-02-21 17:44:59 +01:00
interface C
{
#[SomeAttr(self::class)]
public const CONST = "const";
#[SomeAttr(self::class)]
public function baz(): void {}
}
',
],
2022-02-24 01:50:05 +01:00
'allowBothParamAndPropertyAttributesForPromotedProperties' => [
2022-02-26 21:28:15 +01:00
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute(Attribute::TARGET_PARAMETER)]
class Foo {}
#[Attribute(Attribute::TARGET_PROPERTY)]
class Bar {}
class Baz
{
public function __construct(#[Foo, Bar] private int $test) {}
}
',
],
'multipleAttributesInMultipleGroups' => [
2022-02-26 21:28:15 +01:00
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute]
class A {}
#[Attribute]
class B {}
#[Attribute]
class C {}
#[Attribute]
class D {}
#[A, B]
#[C, D]
class Foo {}
',
],
'propertyLevelSuppression' => [
2022-02-26 21:28:15 +01:00
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute(Attribute::TARGET_CLASS)]
class ClassAttr {}
class Foo
{
/** @psalm-suppress InvalidAttribute */
#[ClassAttr]
public string $bar = "baz";
}
',
],
'invalidAttributeDoesntCrash' => [
2022-02-26 21:28:15 +01:00
'code' => '<?php
2022-02-24 01:50:05 +01:00
/** @psalm-suppress InvalidScalarArgument */
#[Attribute("foobar")]
class Foo {}
#[Foo]
class Bar {}
',
],
'dontCrashWhenRedefiningStubbedMethodWithFewerParams' => [
2022-07-07 07:21:35 +02:00
'code' => '<?php
if (!class_exists(ArrayObject::class)) {
class ArrayObject
{
public function __construct() {}
}
}
2022-12-18 17:15:15 +01:00
',
],
];
}
public function providerInvalidCodeParse(): iterable
{
return [
'attributeClassHasNoAttributeAnnotation' => [
'code' => '<?php
class A {}
#[A]
class B {}',
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:4:23',
],
'missingAttributeOnClass' => [
'code' => '<?php
use Foo\Bar\Pure;
#[Pure]
class Video {}',
'error_message' => 'UndefinedAttributeClass - src' . DIRECTORY_SEPARATOR . 'somefile.php:4:23',
2022-02-24 01:50:05 +01:00
],
'missingAttributeOnProperty' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
use Foo\Bar\Pure;
class Baz
{
#[Pure]
public string $foo = "bar";
}
',
'error_message' => 'UndefinedAttributeClass - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:27',
],
'missingAttributeOnFunction' => [
'code' => '<?php
use Foo\Bar\Pure;
#[Pure]
function foo() : void {}',
'error_message' => 'UndefinedAttributeClass - src' . DIRECTORY_SEPARATOR . 'somefile.php:4:23',
],
'missingAttributeOnParam' => [
'code' => '<?php
use Foo\Bar\Pure;
function foo(#[Pure] string $str) : void {}',
'error_message' => 'UndefinedAttributeClass - src' . DIRECTORY_SEPARATOR . 'somefile.php:4:36',
],
'tooFewArgumentsToAttributeConstructor' => [
'code' => '<?php
namespace Foo;
#[\Attribute(\Attribute::TARGET_CLASS)]
class Table {
public function __construct(public string $name) {}
}
#[Table()]
class Video {}',
'error_message' => 'TooFewArguments - src' . DIRECTORY_SEPARATOR . 'somefile.php:9:23',
],
'invalidArgument' => [
'code' => '<?php
#[Attribute]
class Foo
{
public function __construct(int $i)
{
}
}
#[Foo("foo")]
class Bar{}',
'error_message' => 'InvalidScalarArgument - src' . DIRECTORY_SEPARATOR . 'somefile.php:10:27',
],
'classAttributeUsedOnFunction' => [
'code' => '<?php
namespace Foo;
#[\Attribute(\Attribute::TARGET_CLASS)]
class Table {
public function __construct(public string $name) {}
}
#[Table("videos")]
function foo() : void {}',
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:9:23',
],
2020-11-22 07:15:52 +01:00
'interfaceCannotBeAttributeClass' => [
'code' => '<?php
2020-11-22 07:15:52 +01:00
#[Attribute]
interface Foo {}',
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:23',
2020-11-22 07:15:52 +01:00
],
'traitCannotBeAttributeClass' => [
'code' => '<?php
2020-11-22 07:15:52 +01:00
#[Attribute]
2022-02-24 01:50:05 +01:00
trait Foo {}',
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:23',
2020-11-22 07:15:52 +01:00
],
'abstractClassCannotBeAttributeClass' => [
'code' => '<?php
2020-11-22 07:15:52 +01:00
#[Attribute]
abstract class Baz {}',
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:23',
2020-11-22 07:15:52 +01:00
],
2022-02-24 01:50:05 +01:00
'attributeClassCannotHavePrivateConstructor' => [
'code' => '<?php
2020-11-22 07:15:52 +01:00
#[Attribute]
class Baz {
private function __construct() {}
}',
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:2:23',
2022-02-24 01:50:05 +01:00
],
'SKIPPED-attributeInvalidTargetClassConst' => [ // Will be implemented in Psalm 5 where we have better class const analysis
'code' => '<?php
2022-02-24 01:50:05 +01:00
class Foo {
#[Attribute]
public const BAR = "baz";
}
',
'error_message' => 'InvalidAttribute',
],
'attributeInvalidTargetProperty' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
class Foo {
#[Attribute]
public string $bar = "baz";
}
',
'error_message' => 'InvalidAttribute',
],
'attributeInvalidTargetMethod' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
class Foo {
#[Attribute]
public function bar(): void {}
}
',
'error_message' => 'InvalidAttribute',
],
'attributeInvalidTargetFunction' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute]
function foo(): void {}
',
'error_message' => 'InvalidAttribute',
],
'attributeInvalidTargetParameter' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
function foo(#[Attribute] string $_bar): void {}
',
'error_message' => 'InvalidAttribute',
],
'attributeTargetArgCannotBeVariable' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
$target = 1;
#[Attribute($target)]
class Foo {}
',
'error_message' => 'UndefinedVariable',
],
'attributeTargetArgCannotBeSelfConst' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute(self::BAR)]
class Foo
{
public const BAR = 1;
}
',
'error_message' => 'NonStaticSelfCall',
2020-11-22 07:15:52 +01:00
],
'noParentInAttributeOnClassWithoutParent' => [
'code' => '<?php
#[Attribute]
class SomeAttr
{
/** @param class-string $class */
public function __construct(string $class) {}
}
#[SomeAttr(parent::class)]
class A {}
',
'error_message' => 'ParentNotFound',
],
2022-02-24 01:50:05 +01:00
'undefinedConstantInAttribute' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute]
class Foo
{
public function __construct(int $i) {}
}
#[Foo(self::BAR_CONST)]
class Bar {}
',
'error_message' => 'UndefinedConstant',
],
2022-02-24 02:54:17 +01:00
'getAttributesOnClassWithNonClassAttribute' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute(Attribute::TARGET_PROPERTY)]
class Attr {}
class Foo {}
$r = new ReflectionClass(Foo::class);
$r->getAttributes(Attr::class);
',
2022-02-24 02:54:17 +01:00
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:8:39 - Attribute Attr cannot be used on a class',
2022-02-24 01:50:05 +01:00
],
2022-02-24 02:54:17 +01:00
'getAttributesOnFunctionWithNonFunctionAttribute' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute(Attribute::TARGET_PROPERTY)]
class Attr {}
function foo(): void {}
2022-02-24 02:54:17 +01:00
/** @psalm-suppress InvalidArgument */
2022-02-24 01:50:05 +01:00
$r = new ReflectionFunction("foo");
$r->getAttributes(Attr::class);
',
2022-02-24 02:54:17 +01:00
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:9:39 - Attribute Attr cannot be used on a function',
2022-02-24 01:50:05 +01:00
],
2022-02-24 02:54:17 +01:00
'getAttributesOnMethodWithNonMethodAttribute' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute(Attribute::TARGET_PROPERTY)]
class Attr {}
class Foo
{
public function bar(): void {}
}
$r = new ReflectionMethod("Foo::bar");
$r->getAttributes(Attr::class);
',
2022-02-24 02:54:17 +01:00
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:11:39 - Attribute Attr cannot be used on a method',
2022-02-24 01:50:05 +01:00
],
2022-02-24 02:54:17 +01:00
'getAttributesOnPropertyWithNonPropertyAttribute' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute(Attribute::TARGET_CLASS)]
class Attr {}
class Foo
{
public string $bar = "baz";
}
$r = new ReflectionProperty(Foo::class, "bar");
$r->getAttributes(Attr::class);
',
2022-02-24 02:54:17 +01:00
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:11:39 - Attribute Attr cannot be used on a property',
2022-02-24 01:50:05 +01:00
],
2022-02-24 02:54:17 +01:00
'getAttributesOnClassConstantWithNonClassConstantAttribute' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute(Attribute::TARGET_PROPERTY)]
class Attr {}
class Foo
{
public const BAR = "baz";
}
$r = new ReflectionClassConstant(Foo::class, "BAR");
$r->getAttributes(Attr::class);
',
2022-02-24 02:54:17 +01:00
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:11:39 - Attribute Attr cannot be used on a class constant',
2022-02-24 01:50:05 +01:00
],
2022-02-24 02:54:17 +01:00
'getAttributesOnParameterWithNonParameterAttribute' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute(Attribute::TARGET_PROPERTY)]
class Attr {}
function foo(int $bar): void {}
$r = new ReflectionParameter("foo", "bar");
$r->getAttributes(Attr::class);
',
2022-02-24 02:54:17 +01:00
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:8:39 - Attribute Attr cannot be used on a function/method parameter',
],
'getAttributesWithNonAttribute' => [
'code' => '<?php
2022-02-24 02:54:17 +01:00
class NonAttr {}
function foo(int $bar): void {}
$r = new ReflectionParameter("foo", "bar");
$r->getAttributes(NonAttr::class);
',
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:7:39 - The class NonAttr doesn\'t have the Attribute attribute',
2022-02-24 01:50:05 +01:00
],
'analyzeConstructorForNonexistentAttributes' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
class Foo
{
public function __construct(string $_arg) {}
}
/** @psalm-suppress UndefinedAttributeClass */
#[AttrA(new Foo(1))]
class Bar {}
',
'error_message' => 'InvalidScalarArgument',
],
'multipleAttributesShowErrors' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute(Attribute::TARGET_CLASS)]
class Foo {}
#[Attribute(Attribute::TARGET_PARAMETER)]
class Bar {}
#[Foo, Bar]
class Baz {}
',
'error_message' => 'InvalidAttribute',
],
'repeatNonRepeatableAttribute' => [
'code' => '<?php
2022-02-24 01:50:05 +01:00
#[Attribute]
class Foo {}
#[Foo, Foo]
class Baz {}
',
'error_message' => 'InvalidAttribute - src' . DIRECTORY_SEPARATOR . 'somefile.php:5:28 - Attribute Foo is not repeatable',
2020-11-22 07:15:52 +01:00
],
'invalidAttributeConstructionWithReturningFunction' => [
'code' => '<?php
enum Enumm
{
case SOME_CASE;
}
#[Attribute]
final class Attr
{
public function __construct(public Enumm $e) {}
}
final class SomeClass
{
#[Attr(Enumm::WRONG_CASE)]
public function anotherMethod(): string
{
return "";
}
}
',
'error_message' => 'UndefinedConstant',
'ignored_issues' => [],
'php_version' => '8.1',
],
'sensitiveParameterOnMethod' => [
2022-10-16 13:59:15 +02:00
'code' => '<?php
namespace SensitiveParameter;
use SensitiveParameter;
class HelloWorld {
#[SensitiveParameter]
public function __construct(
string $password
) {}
}
',
'error_message' => 'Attribute SensitiveParameter cannot be used on a method',
2023-03-03 09:37:08 +01:00
'ignored_issues' => [],
'php_version' => '8.2',
],
];
}
}