php-static-analysis/rector-rule
Composer 安装命令:
composer require --dev php-static-analysis/rector-rule
包简介
RectorPHP rule to convert PHPDoc annotations for static analysis to PHP attributes
关键字:
README 文档
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 have not made this transition to attributes and they still rely on annotations in PHPDocs for a lot of their functionality.
This is a set of RectorPHP rules that allows us to convert standard PHP static analysis annotations into a new set of attributes that replace these 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 these rules, install this package:
composer require --dev php-static-analysis/rector-rule
Using the rules
To replace all the annotations that this package covers, use the set provided by it:
use Rector\Config\RectorConfig; use PhpStaticAnalysis\RectorRule\Set\PhpStaticAnalysisAnnotationsToAttributesSetList; return RectorConfig::configure() ->withSets([ PhpStaticAnalysisAnnotationsToAttributesSetList::ANNOTATIONS_TO_ATTRIBUTES ]) ->withImportNames();
(We recommend that you add the withImportNames() option so that attributes are not added with their fully qualified name)
If you only want to replace some annotations and leave the others as they are, use the rule configured with the annotations that you need. For example, if you only want to replace the @return and @param annotations, use this configuration:
use Rector\Config\RectorConfig; use Rector\Php80\ValueObject\AnnotationToAttribute; use PhpStaticAnalysis\Attributes\Param; use PhpStaticAnalysis\Attributes\Returns; use PhpStaticAnalysis\RectorRule\AnnotationsToAttributesRector; return RectorConfig::configure() ->withConfiguredRule( AnnotationsToAttributesRector::class, [ new AnnotationToAttribute('param', Param::class), new AnnotationToAttribute('return', Returns::class), ] );
If you want to replace most annotations but exclude a few, you can use the excludeAnnotations config parameter like this:
use Rector\Config\RectorConfig; use PhpStaticAnalysis\RectorRule\AnnotationsToAttributesRector; return RectorConfig::configure() ->withConfiguredRule( AnnotationsToAttributesRector::class, [ 'excludeAnnotations' => ['throws', 'deprecated'], ] );
That would convert all annotations except @throws and @deprecated
These are the available attributes and their corresponding PHPDoc annotations:
| Attribute | PHPDoc Annotations |
|---|---|
| Assert | @assert |
| AssertIfFalse | @assert-if-false |
| AssertIfTrue | @assert-if-true |
| DefineType | @type |
| Deprecated | @deprecated |
| Immmutable | @immmutable |
| ImportType | @import-type |
| Impure | @impure |
| Internal | @internal |
| IsReadOnly | @readonly |
| Method | @method |
| Mixin | @mixin |
| Param | @param |
| ParamOut | @param-out |
| Property | @property @var |
| PropertyRead | @property-read |
| PropertyWrite | @property-write |
| Pure | @pure |
| RequireExtends | @require-extends |
| RequireImplements | @require-implements |
| Returns | @return |
| SelfOut | @self-out @this-out |
| Template | @template |
| TemplateContravariant | @template-contravariant |
| TemplateCovariant | @template-covariant |
| TemplateExtends | @extends @template-extends |
| TemplateImplements | @implements @template-implements |
| TemplateUse | @use @template-use |
| Throws | @throws |
| Type | @var @return |
Location of Param and ParamOut attributes
By default Param and ParamOut attributes are added on the method/function where the @param or @param-out annotation was located. It is possible to instead add them on the corresponding parameter in the function. To activate this option, add this code to your configuration:
use PhpStaticAnalysis\RectorRule\AnnotationsToAttributesRector; use Rector\Config\RectorConfig; ... return RectorConfig::configure() ... ->withConfiguredRule( AnnotationsToAttributesRector::class, [ 'addParamAttributeOnParameters' => true, ] );
Location of Assert, AssertIfFalse and AssertIfTrue attributes
By default Assert, AssertIfFalse and AssertIfTrue attributes are added on the method/function where the @assert, @assert-if-false or @assert-if-true annotation was located. It is possible to instead add them on the corresponding parameter in the function. To activate this option, add this code to your configuration:
use PhpStaticAnalysis\RectorRule\AnnotationsToAttributesRector; use Rector\Config\RectorConfig; ... return RectorConfig::configure() ... ->withConfiguredRule( AnnotationsToAttributesRector::class, [ 'addAssertAttributeOnParameters' => true, ] );
Attribute to use for the return type of methods and functions
By default Returns attributes are added to define the return type of methods/functions. It is possible to use the Type attribute instead. To activate this option, add this code to your configuration:
use PhpStaticAnalysis\RectorRule\AnnotationsToAttributesRector; use Rector\Config\RectorConfig; ... return RectorConfig::configure() ... ->withConfiguredRule( AnnotationsToAttributesRector::class, [ 'useTypeAttributeForReturnAnnotation' => true, ] );
Attribute to use for the type of class properties
By default Type attributes are added to define the type of class properties. It is possible to use the Property attribute instead. To activate this option, add this code to your configuration:
use PhpStaticAnalysis\RectorRule\AnnotationsToAttributesRector; use Rector\Config\RectorConfig; ... return RectorConfig::configure() ... ->withConfiguredRule( AnnotationsToAttributesRector::class, [ 'usePropertyAttributeForVarAnnotation' => true, ] );
Attribute to use for the definition of types for classes
By default DefineType attributes are added to define a type for a class. It is possible to use the Type attribute instead. To activate this option, add this code to your configuration:
use PhpStaticAnalysis\RectorRule\AnnotationsToAttributesRector; use Rector\Config\RectorConfig; ... return RectorConfig::configure() ... ->withConfiguredRule( AnnotationsToAttributesRector::class, [ 'useTypeAttributeForTypeClassAnnotation' => true, ] );
Using these rules with Rector
Once you have converted your static analysis annotations to attributes, you may want to use them with Rector, so that Rector can understand them and use them to apply its rules. To do this, when running rector we first need to temporarily convert these attributes back to annotations, then run your Rector rules and finally convert the annotations back to attributes. To do this, use this in your configuration:
use PhpStaticAnalysis\RectorRule\Set\PhpStaticAnalysisAnnotationsToAttributesSetList; use PhpStaticAnalysis\RectorRule\Set\PhpStaticAnalysisAttributesToAnnotationsSetList; use Rector\Config\RectorConfig; ... return RectorConfig::configure() ->withSets([ PhpStaticAnalysisAttributesToAnnotationsSetList::ATTRIBUTES_TO_ANNOTATIONS ]) ... //any other Rector rules or sets ... ->withSets([ PhpStaticAnalysisAnnotationsToAttributesSetList::ANNOTATIONS_TO_ATTRIBUTES ]);
If you use any special configuration for the Annotations to Attributes process, for example only converting some of the annotations or setting some flags, use it in this last part of the block of code instead of the sample.
Sponsor this project
If you would like to support the development of this project, please consider sponsoring me
php-static-analysis/rector-rule 适用场景与选型建议
php-static-analysis/rector-rule 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.7k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2024 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「static analysis」 「dev」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 php-static-analysis/rector-rule 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-static-analysis/rector-rule 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 php-static-analysis/rector-rule 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
Monolog bridge for PHP Debugbar
Simple and fast methods to read private properties and call private methods
Sentiment analysis library for PHP.
Analysis module for finding problematical shop data.
统计信息
- 总下载量: 25.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-07