|
1 |
| -# attributes |
2 |
| -Attributes used for static analysis |
| 1 | +# PHP Static Analysis Attributes |
| 2 | + |
| 3 | +[](https://github.com/php-static-analysis/attributes/actions) |
| 4 | +[](https://packagist.org/packages/php-static-analysis/attributes) |
| 5 | +[](https://packagist.org/packages/php-static-analysis/attributes) |
| 6 | +[](https://github.com/php-static-analysis/attributes/blob/main/LICENSE) |
| 7 | +[](https://packagist.org/packages/php-static-analysis/attributes/stats) |
| 8 | + |
| 9 | +Since the release of PHP 8.0 more and more libraries, frameworks and tools have been updated to use attributes instead of annotations in PHPDocs. |
| 10 | + |
| 11 | +However, static analysis tools like PHPStan or Psalm or IDEs like PHPStorm or VS Code have not made this transition to attributes and they still rely on annotations in PHPDocs for a lot of their functionality. |
| 12 | + |
| 13 | +This library aims to provide a set of PHP attributes which could replace the most commonly used annotations accepted by these tools and will aim to provide related repositories with the extensions or plugins that would allow these attributes to be used in these tools. |
| 14 | + |
| 15 | +In particular, these repositories are: |
| 16 | + |
| 17 | +- [PHPStan extension](https://github.com/php-static-analysis/phpstan-extension) |
| 18 | +- [Psalm plugin](https://github.com/php-static-analysis/psalm-plugin) |
| 19 | +- [RectorPHP rules to migrate annotations to attributes](https://github.com/php-static-analysis/rector-rule) |
| 20 | + |
| 21 | +## PHPStorm |
| 22 | +A plugin that fully supports these attributes will need to be created. Until this is ready you can get partial support by installing PHPStan, our PHPStan extension and enabling PHPStan support in PHPStorm (as described [here](https://www.jetbrains.com/help/phpstorm/using-phpstan.html)). You will then be able to see errors and messages related to these attributes in your code. |
| 23 | + |
| 24 | +Alternatively install Psalm, our Psalm extension and enable Psalm support in PHPStorm (as described [here](https://www.jetbrains.com/help/phpstorm/using-psalm.html)) |
| 25 | + |
| 26 | +## VS Code Support |
| 27 | +An extension that fully supports these attributes will need to be created. Until this is ready you can get partial support by installing PHPStan, our PHPStan extension and a VS Code extension that supports PHPStan (for example [this one](https://github.com/SanderRonde/phpstan-vscode)). When you enable the extension you will be able to see errors and messages related to these attributes in your code. |
| 28 | + |
| 29 | +Alternatively install Psalm, our Psalm extension and a VS Code extension that supports Psam (for example [this one](https://github.com/psalm/psalm-vscode-plugin)) |
| 30 | + |
| 31 | +## Example |
| 32 | + |
| 33 | +In order to show how code would look with these attributes, we can look at the following example. This is how a class looks like with the current annotations: |
| 34 | + |
| 35 | +```php |
| 36 | +<?php |
| 37 | + |
| 38 | +class ArrayAdder |
| 39 | +{ |
| 40 | + /** @var array<string> */ |
| 41 | + private array $result; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param array<string> $array1 |
| 45 | + * @param array<string> $array2 |
| 46 | + * @return array<string> |
| 47 | + */ |
| 48 | + public function addArrays(array $array1, array $array2): array |
| 49 | + { |
| 50 | + $this->result = $array1 + $array2; |
| 51 | + return $this->result; |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +And this is how it would look like using the new attributes: |
| 57 | + |
| 58 | +```php |
| 59 | +<?php |
| 60 | + |
| 61 | +use PhpStaticAnalysis\Attributes\Type; |
| 62 | +use PhpStaticAnalysis\Attributes\Param; |
| 63 | +use PhpStaticAnalysis\Attributes\Returns; |
| 64 | + |
| 65 | +class ArrayAdder |
| 66 | +{ |
| 67 | + #[Type('array<string>')] |
| 68 | + private array $result; |
| 69 | + |
| 70 | + #[Param(array1: 'array<string>')] |
| 71 | + #[Param(array2: 'array<string>')] |
| 72 | + #[Returns('array<string>')] |
| 73 | + public function addArrays(array $array1, array $array2): array |
| 74 | + { |
| 75 | + $this->array = $array1 + $array2; |
| 76 | + return $this->array; |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Installation |
| 82 | + |
| 83 | +To use these attributes, require this library in Composer: |
| 84 | + |
| 85 | +``` |
| 86 | +composer require php-static-analysis/attributes |
| 87 | +``` |
| 88 | + |
| 89 | +And then install any needed extensions/plugins for the tools that you use. |
| 90 | + |
| 91 | +## List of implemented attributes |
| 92 | + |
| 93 | +These are the available attributes and their corresponding PHPDoc annotation: |
| 94 | + |
| 95 | +| Attribute | PHPDoc Annotation | |
| 96 | +|---|-------------------| |
| 97 | +| [IsReadOnly](doc/IsReadOnly.md) | `@readonly` | |
| 98 | +| [Param](doc/Param.md) | `@param` | |
| 99 | +| [Returns](doc/Returns.md) | `@return` | |
| 100 | +| [Template](doc/Template.md) | `@template` | |
| 101 | +| [Type](doc/Type.md) | `@var` | |
| 102 | + |
0 commit comments