php-static-analysis / attributes
Attributes used instead of PHPDocs for static analysis tools
Fund package maintenance!
carlos-granados
Installs: 11 551
Dependents: 6
Suggesters: 0
Security: 0
Stars: 73
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.0
Requires (Dev)
- php-static-analysis/node-visitor: ^0.4.0 || dev-main
- php-static-analysis/phpstan-extension: ^0.4.0 || dev-main
- php-static-analysis/psalm-plugin: ^0.4.0 || dev-main
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^9.0
- symplify/easy-coding-standard: ^12.1
- vimeo/psalm: ^5
Suggests
- php-static-analysis/phpstan-extension: PHPStan extension to read static analysis attributes
- php-static-analysis/psalm-plugin: Psalm plugin to read static analysis attributes
- php-static-analysis/rector-rule: RectorPHP rule to convert PHPDoc annotations to static analysis attributes
README
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.
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.
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.
In particular, these repositories are:
Example
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:
<?php class ArrayAdder { /** @var array<string> */ private array $result; /** * @param array<string> $array1 the first array * @param array<string> $array2 the second array * @return array<string> */ public function addArrays(array $array1, array $array2): array { $this->result = $array1 + $array2; return $this->result; } }
And this is how it would look like using the new attributes:
<?php use PhpStaticAnalysis\Attributes\Type; use PhpStaticAnalysis\Attributes\Param; use PhpStaticAnalysis\Attributes\Returns; class ArrayAdder { #[Type('array<string>')] private array $result; #[Param(array1: 'array<string>')] // the first array #[Param(array2: 'array<string>')] // the second array #[Returns('array<string>')] public function addArrays(array $array1, array $array2): array { $this->array = $array1 + $array2; return $this->array; } }
Installation
To use these attributes, require this library in Composer:
composer require php-static-analysis/attributes
And then install any needed extensions/plugins for the tools that you use.
List of implemented attributes
These are the available attributes and their corresponding PHPDoc annotations:
PhpStorm Support
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). You will then be able to see errors and messages related to these attributes in your code.
Alternatively install Psalm, our Psalm extension and enable Psalm support in PhpStorm (as described here)
VS Code Support
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). When you enable the extension you will be able to see errors and messages related to these attributes in your code.
Alternatively install Psalm, our Psalm extension and a VS Code extension that supports Psam (for example this one)
Sponsor this project
If you would like to support the development of this project, please consider sponsoring me