PhpStorm Attributes Project Overview
The PhpStorm Attributes project enhances the capabilities of the PhpStorm integrated development environment (IDE) for PHP developers by leveraging PHP 8 attributes. This introduction explains the features and functionalities of these attributes, which are designed to improve code completion and analysis within PhpStorm.
Installation
The attributes are seamlessly integrated within PhpStorm versions 2020.3 and later. Since they come bundled with the IDE, no separate installation is necessary. However, if other static analysis tools in your environment pose "Class not found" issues, adding the attributes package as a development dependency in your composer.json
can resolve this:
composer require --dev jetbrains/phpstorm-attributes
Key Attributes
-
#[Deprecated]
: This attribute helps in indicating to developers when an element (such as a function or class) is slated for future removal. It’s advisable to provide reasoning and possible updates usingreason
andreplacement
parameters within this attribute.#[Deprecated( reason: 'since Symfony 5.2, use setPublic() instead', replacement: '%class%->setPublic(!%parameter0%)' )]
-
#[ArrayShape]
: Ideal for dealing with object-like arrays, this attribute specifies keys and corresponding value types, improving code suggestions and accuracy.#[ArrayShape([ 'key1' => 'int', 'key2' => 'string', 'key3' => App\PHP8\Foo::class, ])] function functionName(...): array
-
#[ObjectShape]
: When applied, the IDE understands and suggests predefined object field names and types, enhancing coding productivity.#[ObjectShape(["age" => "int", "name" => "string"])] function functionName(): object {...}
-
#[Immutable]
: This attribute marks classes or properties as immutable, ensuring that they cannot be altered post-initialization.#[Immutable] class DTO { public string $val; public function __construct(string $val) { $this->val = $val; } }
-
#[Pure]
: Applied to functions that are free of side effects, this attribute aids in analyzing and understanding code behavior predictably.#[Pure] function compare(Foo $a, Foo $b): int { return $a->a <=> $b->b; }
-
#[ExpectedValues]
: By specifying acceptable parameter values, this attribute refines code suggestions and validation.function response( #[ExpectedValues(valuesFromClass: Response::class)] $httpStatusCode ) { //... }
-
#[NoReturn]
: This attribute is used to tag functions that end script execution, assisting the IDE in flow analysis and control.#[NoReturn] function redirect(): void { exit(); }
-
#[Language]
: Enhance the recognition of string parameters containing code in other languages, such as SQL or RegExp, for improved highlighting and IDE features.This suite of attributes not only enriches PhpStorm's code assistance but also presents developers with a more intuitive coding experience. The attributes address a broad spectrum of needs from type assurance to code deprecation notices, significantly aiding in writing clean and maintainable PHP code.
Contribution and Issues
For any bugs encountered or feature suggestions, you are encouraged to use the PhpStorm issue tracker at YouTrack. Contributions through pull requests are also appreciated, fostering an environment of collaboration and continuous improvement.