previousnext/coding-standard 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

previousnext/coding-standard

Composer 安装命令:

composer require previousnext/coding-standard

包简介

关键字:

README 文档

README

Latest Stable Version Total Downloads GitHub branch checks state License

This is a PHP Codesniffer standard enhancing the Drupal Coder project. It is designed to be utilised on full Drupal site projects, rather than core or contrib, or projects outside of Drupal.

The standard improves upon the Drupal standard, while maintaining the general style of Drupal.

Rules are loosened where static analysis tools such as PHPStan are better at enforcing.

Documentation rules are loosened, trusting developers to properly self describe code. Suggestions to improve documentation are encouraged during human code review of MR/PR's.

Rules added are generally those we think would be accepted by Drupal Coder itself.

Generally, rules that are not auto-fixable, especially documentation/commenting related, will be stripped from the ruleset.

New rules may be introduced in minor versions. You can choose to lock a version until you are ready, or utilise the baseline project.

License

MIT

Transitioning

Consider using the Baseliner project when transitioning. A quick phpcbf beforehand will eliminate most or all problems before they are added to a baseline.

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

Unlike PHPStan's reportUnmatchedIgnoredErrors, warnings will not display when items in the PHPCS baseline are resolved. Instead, work until no CS issues are reported with phpcs, then regenerate with the same baseline command above.

Using the standard

Use the installer via Composer:

composer require dealerdirect/phpcodesniffer-composer-installer

Install this project with:

composer require previousnext/coding-standard

Dependencies such as Coding standard, Drupal Coder, and Slevomat Coding Standard will be brought in automatically.

Configuration Template

Create a phpcs.xml file in a project root with the following contents:

<?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>

This standard does not assume the location of code, so all covered paths must be added to the extending ruleset.

Tips

Parameters and return documentation may be omitted. If you intend to add typing, but do not care to document what a param or return does, then consider using @phpstan- prefixed annotations. Such as @phpstan-param or @phpstan-return.

Example

From Drupal Coder style:

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

To:

Remove useless documentation for the function/method itself. Human code review must ensure the function/method/variable/param, etc, are self describing.

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

Style

Added 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 can be configured with Editor -> General -> Auto Import: Function: Prefer FQN

SlevomatCodingStandard.TypeHints.DeclareStrictTypes

SlevomatCodingStandard.TypeHints.ReturnTypeHint

SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing

Squiz.WhiteSpace.FunctionOpeningBraceSpace.SpacingAfter

Removed/Loosened Inherited Sniffs

Drupal.Arrays.Array.LongLineDeclaration

Drupal.Commenting.*

  • Drupal.Commenting.ClassComment.Short
  • Drupal.Commenting.DocComment.MissingShort
  • Drupal.Commenting.FileComment.Missing
  • Drupal.Commenting.FunctionComment.IncorrectParamVarName
  • Drupal.Commenting.FunctionComment.InvalidReturn
  • Drupal.Commenting.TodoComment.TodoFormat
  • Drupal.Commenting.VariableComment.Missing

Commenting rules are not auto-fixable, are usually boilerplate-y.

Developers are entrusted to properly self-describe code. Suggestions to improve documentation are encouraged during human code review of MR/PR's.

Squiz.PHP.NonExecutableCode.Unreachable

This rule has trouble with newer PHP syntax, especially expression throwables. In any case this rule is best enforced with static analysis.

Drupal is a registered trademark of Dries Buytaert.

previousnext/coding-standard 适用场景与选型建议

previousnext/coding-standard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50.28k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「drupal」 「phpcs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 previousnext/coding-standard 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 50.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 26
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 5
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-16