swivl/php-coding-standard

Swivl PHP_CodeSniffer 编码标准

安装次数: 38,353

依赖: 7

建议者: 0

安全性: 0

星星: 3

关注者: 9

分支: 3

公开问题: 0

类型:phpcodesniffer-standard

v1.5.0 2024-04-19 17:22 UTC

This package is auto-updated.

Last update: 2024-09-19 18:14:29 UTC


README

这个编码标准是 PSR-12 的更严格版本。

安装

使用 composer 安装编码标准

composer require --dev swivl/php-coding-standard

配置

在项目的根目录中创建文件 phpcs.xml.dist,内容类似以下

<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

    <arg name="basepath" value="."/>
    <arg name="cache" value=".phpcs-cache"/>
    <arg name="colors"/>
    <arg value="p"/>
    <arg name="extensions" value="php"/>
    <arg name="tab-width" value="4"/>
    <arg name="report-width" value="120"/>

    <rule ref="vendor/swivl/php-coding-standard/Swivl/ruleset.xml"/>

    <rule ref="Generic.Files.LineLength.TooLong">
        <exclude-pattern>*/src/Migrations/*</exclude-pattern>
    </rule>

    <file>src/</file>
    <file>tests/</file>

    <exclude-pattern>*/Resources/*</exclude-pattern>

</ruleset>

将您的 IDE 配置为使用 phpcs.xml.dist 作为 PHP_CodeSniffer 的默认规则集。

在您的项目中检查代码风格

检查整个项目

bin/phpcs

检查单个文件或目录

bin/phpcs path/to/file/or/directory

自动修复错误

bin/phpcbf

高级配置

Swivl.Commenting.DoctrineEntity

此嗅探器提供以下选项

<rule ref="Swivl.Commenting.DoctrineEntity">
    <properties>
        <property name="concreteTypeToBaseTypeMap" type="array">
            <element key="FeedItemComment" value="CommentInterface"/>
        </property>
        <property name="useDynamicalCalculationForEnumColumnType" value="true"/>
    </properties>
</rule>
  • concreteTypeToBaseTypeMap - 当列映射到具体类型 FeedItemComment,但修改方法使用基类 CommentInterface 类型提示时
  • useDynamicalCalculationForEnumColumnType - 当枚举映射到某些 PHP 类型时。例如,type="auth_enum_type" 映射到 PHP AuthType。

Swivl.Commenting.FunctionComment

此嗅探器提供以下选项

<rule ref="Swivl.Commenting.FunctionComment">
    <properties>
        <property name="requiredPhpdoc" value="always"/>
        <property name="minimumVisibility" value="private"/>
    </properties>
</rule>
  • requiredPhpdoc - 配置何时需要 PHPDoc 方法。可能的值
    • always - 所有的方法都需要 PHPDoc。
    • map - 如果至少有一个类型提示或返回类型在 FunctionCommentSniff::REQUIRED_PHPDOC_MAP 中定义的列表中,则方法需要 PHPDoc。这是默认行为。
    • never - 不需要 PHPDoc。

Swivl.Functions.ConstructorPropertyPromotion

此嗅探器提供以下选项

<rule ref="Swivl.Functions.ConstructorPropertyPromotion">
    <properties>
        <property name="indent" value="4"/>
    </properties>
</rule>

Swivl.Commenting.UselessFunctionComment

此嗅探器提供以下选项

<rule ref="Swivl.Commenting.UselessFunctionComment">
    <properties>
        <property name="ignoreClasses" value="false"/>
        <property name="ignoreInterfaces" value="false"/>
        <property name="ignoreTraits" value="false"/>
        <property name="includeNames" type="array">
            <element value="__construct"/>
        </property>
        <property name="excludeNames" type="array">
            <element value="__serialize"/>
        </property>
        <property name="maxDescriptionLines" value="1"/>
    </properties>
</rule>

Swivl.Commenting.UselessClassComment

此嗅探器提供以下选项

<rule ref="Swivl.Commenting.UselessClassComment">
    <properties>
        <property name="maxDescriptionLines" value="1"/>
        <property name="uselessTags" type="array">
            <element value="@package"/>
        </property>
        <property name="uselessDescriptions" type="array">
            <element value="Doctrine migration"/>
        </property>
    </properties>
</rule>

依赖更新

Swivl Coding Standard 的一些嗅探器覆盖了依赖列表中库的嗅探器。在依赖更新期间,以下嗅探器应与原始代码同步