定制 php-static-analysis/attributes 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

php-static-analysis/attributes

Composer 安装命令:

composer require php-static-analysis/attributes

包简介

Attributes used instead of PHPDocs for static analysis tools

README 文档

README

Continuous Integration Latest Stable Version License Total Downloads

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:

Attribute PHPDoc Annotations
Assert @assert
AssertIfFalse @assert-if-false
AssertIfTrue @assert-if-true
DefineType @type
Deprecated @deprecated
Immutable @immutable
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 @type

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

php-static-analysis/attributes 适用场景与选型建议

php-static-analysis/attributes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 100.19k 次下载、GitHub Stars 达 94, 最近一次更新时间为 2024 年 01 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 php-static-analysis/attributes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 php-static-analysis/attributes 我们能提供哪些服务?
定制开发 / 二次开发

基于 php-static-analysis/attributes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 100.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 94
  • 点击次数: 12
  • 依赖项目数: 9
  • 推荐数: 0

GitHub 信息

  • Stars: 94
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-28