previousnext/coding-standard

安装次数: 12,229

依赖项: 4

建议者: 0

安全性: 0

星星: 3

关注者: 7

分支: 0

开放问题: 0

类型:phpcodesniffer-standard

1.1.0 2024-06-04 11:05 UTC

This package is auto-updated.

Last update: 2024-09-04 12:10:05 UTC


README

Latest Stable Version Total Downloads GitHub branch checks state License

这是一个增强Drupal Coder项目的PHP Codesniffer标准。它旨在在完整的Drupal站点项目中使用,而不是在核心或贡献项目中,或在Drupal之外的项目中。

该标准在保持Drupal通用风格的同时,改进了Drupal标准。

在静态分析工具(如PHPStan)更擅长强制执行的地方,放宽了规则。

放宽了文档规则,相信开发者能正确自我描述代码。鼓励在MR/PR的人工代码审查期间提出改进文档的建议。

增加的规则通常是我们认为Drupal Coder本身也会接受的。

一般来说,无法自动修复的规则,特别是与文档/注释相关的,将不会包含在规则集中。

新规则可能会在次要版本中引入。您可以选择锁定版本直到准备好,或者使用基线项目。

许可证

MIT

过渡

在过渡时考虑使用Baseliner项目。在创建基线之前快速运行phpcbf可以消除大多数或所有问题。

composer require --dev digitalrevolution/php-codesniffer-baseline
php bin/phpcs --report=\\DR\\CodeSnifferBaseline\\Reports\\Baseline --report-file=phpcs.baseline.xml

与PHPStan的reportUnmatchedIgnoredErrors不同,当PHPCS基线中的项目被解决时,不会显示警告。相反,使用上面的相同基线命令,直到没有CS问题报告给phpcs,然后再重新生成。

使用标准

通过Composer安装程序使用

composer require dealerdirect/phpcodesniffer-composer-installer

使用以下命令安装此项目

composer require previousnext/coding-standard

将自动引入依赖项,例如编码标准、Drupal Coder和Slevomat Coding Standard。

配置模板

在项目根目录中创建一个phpcs.xml文件,内容如下

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="My Coding Standard">
    <file>./src</file>
    <file>./tests</file>
    <file>./app/modules/custom</file>
    <file>./app/profiles/custom</file>
    <file>./app/themes/custom</file>
    <rule ref="PreviousNextDrupal" />
    <arg name="report" value="full"/>
</ruleset>

此标准不假设代码的位置,因此必须将所有受保护的路径添加到扩展规则集中。

技巧

参数和返回文档可以省略。如果您打算添加类型,但不关心文档化参数或返回值做什么,则考虑使用以@phpstan-前缀的注解。例如@phpstan-param@phpstan-return

示例

Drupal Coder风格

/**
 * Does a thing.
 * 
 * @param array $data
 *   Some data.
 * 
 * @return int
 *   An integer.
 */
function foo(array $data): int {
}

删除函数/方法本身的无用文档。人工代码审查必须确保函数/方法/变量/参数等是自我描述的。

/**
 * @phpstan-param array{bar: int} $data
 * @phpstan-return int{0, max}
 */
function doesAThing(array $data): int {
}

样式

添加的Sniffs

PSR2.Methods.FunctionClosingBrace.SpacingBeforeClose

SlevomatCodingStandard.Classes.ClassStructure

SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature

SlevomatCodingStandard.Commenting.UselessInheritDocComment

SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator

SlevomatCodingStandard.Functions.RequireTrailingCommaInCall

SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration

SlevomatCodingStandard.Functions.StaticClosure

SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure

SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses

SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions

可以在PHPStorm中进行配置:编辑器 -> 一般 -> 自动导入: 函数Prefer FQN

SlevomatCodingStandard.TypeHints.DeclareStrictTypes

SlevomatCodingStandard.TypeHints.ReturnTypeHint

SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing

Squiz.WhiteSpace.FunctionOpeningBraceSpace.SpacingAfter

移除/放宽的继承Sniffs

Drupal.Arrays.Array.LongLineDeclaration

Drupal.Commenting.*

  • Drupal.Commenting.ClassComment.Short
  • Drupal.Commenting.DocComment.MissingShort
  • Drupal.Commenting.FileComment.Missing
  • Drupal.Commenting.FunctionComment.IncorrectParamVarName
  • Drupal.注释.函数注释.无效返回
  • Drupal.注释.待办注释.待办格式
  • Drupal.注释.变量注释.缺失

注释规则无法自动修复,通常比较模板化。

开发者被委托正确地自我描述代码。在MR/PR的人码审查过程中鼓励提出改进文档的建议。

Squiz.PHP.不可执行代码.不可达

此规则与较新的PHP语法有麻烦,特别是表达式可抛出。无论如何,此规则最好通过静态分析来强制执行。

Drupal是Dries Buytaert的注册商标。