php-static-analysis / psalm-plugin
Psalm plugin to read static analysis attributes
Fund package maintenance!
carlos-granados
Installs: 1 467
Dependents: 4
Suggesters: 1
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Type:psalm-plugin
Requires
- php: >=8.0
- ext-simplexml: *
- php-static-analysis/attributes: ^0.3.1 || dev-main
- php-static-analysis/node-visitor: ^0.3.1 || dev-main
- vimeo/psalm: ^5
- webmozart/assert: ^1.11
Requires (Dev)
- php-static-analysis/phpstan-extension: dev-main
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.0
- symplify/easy-coding-standard: ^12.1
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 Psalm have not made this transition to attributes and they still rely on annotations in PHPDocs for a lot of their functionality.
This is a Psalm plugin that allows Psalm to understand a new set of attributes that replace the PHPDoc annotations. These attributes are defined in this repository
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 * @param array<string> $array2 * @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>')] #[Param(array2: 'array<string>')] #[Returns('array<string>')] public function addArrays(array $array1, array $array2): array { $this->array = $array1 + $array2; return $this->array; } }
Installation
First of all, to make the attributes available for your codebase use:
composer require php-static-analysis/attributes
To use this plugin, require it in Composer:
composer require --dev php-static-analysis/psalm-plugin
Then run this command to enable the plugin:
vendor/bin/psalm-plugin enable php-static-analysis/psalm-plugin
This will add this plugin configuration to the psalm.xml
configuration file:
<plugins> <pluginClass class="PhpStaticAnalysis\PsalmPlugin\Plugin" /> </plugins>
If you prefer, you can also manually add this configuration to your psalm.xml
file instead of running the psalm-plugin enable
command.
Using the extension
This extension works by interacting with the parser that Psalm uses to parse the code and replacing the new Attributes with PHPDoc annotations that Psalm can understand. The functionality provided by the attribute is exactly the same as the one provided by the corresponding PHPDoc annotation.
These are the available attributes and their corresponding PHPDoc annotations:
Sponsor this project
If you would like to support the development of this project, please consider sponsoring me