pixelfederation/coding-standards

针对后端开发者的标准化 PHPCS 检查

3.0.0 2024-05-23 10:04 UTC

This package is auto-updated.

Last update: 2024-08-28 21:09:00 UTC


README

此包为 Pixel Federation 的编码标准提供 PHPCS 规则集。应将其包含到 Pixel Federation 维护的每个使用 PHP Code Sniffer PHPCS 的项目中。

示例用法

1) 安装 composer 依赖项

composer require pixelfederation/coding-standards:^2.0 --dev

2) 规则集创建

在项目根目录下创建一个名为 phpcs.ruleset.xml 的文件,内容如下

<?xml version="1.0"?>
<ruleset name="PixelFederation">

  <description>PixelFederation rule set.</description>
    
  <config name="testVersion" value="7.2-7.4"/><!-- insert your php version -->
  <exclude-pattern>tests/</exclude-pattern>
  <rule ref="vendor/pixelfederation/coding-standards/phpcs.ruleset.xml">
    <!-- old projects may want to exclude these rules: -->
    <!-- <exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming"/>
    <exclude name="SlevomatCodingStandard.Classes.SuperfluousTraitNaming"/>
    <exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming"/>
    <exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming"/> -->
    <!-- <exclude name="SlevomatCodingStandard.Files.FunctionLength"/> -->
  </rule>

  <rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
    <properties>
      <property name="rootNamespaces" type="array">
        <element key="src" value="Your\Root\Namespace"/><!-- add your namespaces -->
      </property>
    </properties>
  </rule>

  <!-- include for PHP 7.2- -->
  <!--  <rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall"/>-->

  <!-- include for PHP 7.3+ -->
  <!--  <rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall"/>-->
  
  <!-- include for PHP 7.4+ -->
  <!--  <rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint"/>-->
  <!--  <rule ref="SlevomatCodingStandard.Functions.ArrowFunctionDeclaration"/>-->

</ruleset>

运行检查

在项目目录中运行以下命令

vendor/bin/phpcs --standard=phpcs.ruleset.xml --extensions=php --tab-width=4 -sp src

自动修复错误

在项目目录中运行以下命令

vendor/bin/phpcbf --standard=phpcs.ruleset.xml --extensions=php --tab-width=4 -sp src

其他链接

关于 slevomat 编码标准的 sniff 文档在此: https://github.com/slevomat/coding-standard