定制 inpsyde/php-coding-standards 二次开发

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

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

inpsyde/php-coding-standards

Composer 安装命令:

composer require --dev inpsyde/php-coding-standards

包简介

PHP 7.4+ coding standards for Syde WordPress projects.

README 文档

README

PHP 7.4+ coding standards for Syde WordPress projects.

PHP Quality Assurance

Important

Please note that development on this package has shifted to maintenance mode only. We remain committed to fixing bugs, ensuring compatibility, and addressing security concerns until July 31, 2025. New features are not planned at this time. Maintenance mode will end on August 1, 2025, effectively marking the End-of-Life date of this package.

For active projects, we strongly recommend migrating to syde/phpcs, the next-generation Syde PHP Coding Standards for WordPress development at scale, supporting PHP 8.1+. Please refer to the dedicated Migration docs to get a high-level understanding of the potential migration effort.

Usage

When the package is installed via Composer, and dependencies are updated, everything is ready and the coding standards can be checked via:

vendor/bin/phpcs --standard="Inpsyde" <path>

Here, <path> is at least one file or directory to check, for example:

vendor/bin/phpcs --standard="Inpsyde" ./src/ ./my-plugin.php

There are many options that can be used to customise the behavior of the command, to get documentation use:

vendor/bin/phpcs --help

Configuration File

A phpcs.xml.dist file can be used to avoid passing many arguments via the command line. For example:

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

    <description>My Project coding standard.</description>

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

    <arg value="sp"/>
    <arg name="colors"/>

    <config name="testVersion" value="7.4-"/>
    <config name="text_domain" value="my-project"/>

    <rule ref="Inpsyde">
        <exclude name="WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize"/>
    </rule>

    <rule ref="Inpsyde.CodeQuality.Psr4">
        <properties>
            <property
                name="psr4"
                type="array"
                value="
                    Inpsyde\MyProject=>src,
                    Inpsyde\MyProject\Tests=>tests/src|tests/unit
                "/>
        </properties>
    </rule>

</ruleset>

Such a configuration allows to run the code style check like so:

vendor/bin/phpcs

Moreover, thanks to the text_domain setting, PHP_CodeSniffer will also check that all WordPress internationalization functions are called with the proper text domain.

Included rules

For the detailed lists of included rules, refer to ruleset.xml.

PSR-1, PSR-2, PSR-12

For more information about included rules from PHP Standards Recommendations (PSR), refer to the official documentation:

WordPress Coding Standards

To ensure code quality, and compatibility with WordPress VIP, some rules have been included from:

Slevomat

A few rules have been included from the Slevomat Coding Standard.

PHPCompatibility

For PHP cross-version compatibility checks, the full PHP Compatibility Coding Standard for PHP CodeSniffer standard has been included.

The target PHP version (range) can be changed via a custom phpcs.xml file.

Generic Rules

Some rules are also included from PHP_CodeSniffer itself, as well as PHPCSExtra.

Custom Rules

The following custom rules are in use:

Sniff Name Description Has Config Auto-Fixable
ArgumentTypeDeclaration Enforce argument type declaration.
DisableCallUserFunc Disable usage of call_user_func.
DisableMagicSerialize Disable usage of __sleep, __wakeup.
DisableSerializeInterface Disable usage of Serializable interface.
DisallowShortOpenTag Disallow short open PHP tag (short echo tag allowed).
ElementNameMinimalLength Use minimum 3 chars for names (with a few exclusions)
EncodingComment Detect usage of opening -*- coding: utf-8 -*-
ForbiddenPublicProperty No public class properties
FunctionBodyStart Handle blank line at start of function body.
FunctionLength Max 50 lines per function/method, excluding blank lines and comments-only lines.
HookClosureReturn Ensure that actions callbacks do not return anything, while filter callbacks return something.
HookPriority Report usage of PHP_INT_MAX and PHP_INT_MIN as hook priority.
LineLength Max 100 chars per line
NestingLevel Max indent level of 3 inside functions
NoAccessors Discourage usage of getters and setters.
NoElse Discourage usage of else.
NoRootNamespaceFunctions Report usage of global functions in the root namespace.
NoTopLevelDefine Discourage usage of define where const is preferable.
PropertyPerClassLimit Discourage usage of more than 10 properties per class.
Psr4 Check PSR-4 compliance
ReturnTypeDeclaration Enforce return type declaration
StaticClosure Points closures that can be static.
VariablesName Check variable (and properties) names

For notes and configuration, refer to the inpsyde-custom-sniffs.md file in this repository.

Template Rules

The InpsydeTemplates ruleset extends the standard Inpsyde ruleset with some template-specific sniffs.

The recommended way to use the InpsydeTemplates ruleset is as follows:

<ruleset>
    <file>./src</file>
    <file>./templates</file>
    <file>./tests</file>
    <file>./views</file>

    <rule ref="Inpsyde"/>

    <rule ref="InpsydeTemplates">
        <include-pattern>*/templates/*</include-pattern>
        <include-pattern>*/views/*</include-pattern>
    </rule>
</ruleset>

The following template-specific rules are available:

Sniff Name Description Has Config Auto-Fixable
AlternativeControlStructure Encourage usage of alternative syntax with inline HTML.
ShortEchoTag Replace echo with short echo tag in single-line statements.
TrailingSemicolon Remove trailing semicolon before closing PHP tag.

Removing or Disabling Rules

Rules Tree

Sometimes it is necessary not to follow some rules. To avoid error reporting, it is possible to:

  • remove rules for an entire project via configuration;
  • disable rules from code, only is specific places.

In both cases, it is possible to remove or disable:

  • a complete standard;
  • a standard subset;
  • a single sniff;
  • a single rule.

These things are in a hierarchical relationship: standards are made of one or more subsets, which contain one or more sniffs, which in turn contain one or more rules.

Removing Rules via Configuration File

Rules can be removed for the entire project by using a custom phpcs.xml file, like this:

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

    <rule ref="Inpsyde">
        <exclude name="PSR1.Classes.ClassDeclaration"/>
    </rule>

</ruleset>

In the example above, the PSR1.Classes.ClassDeclaration sniff (and all the rules it contains) has been removed.

By using PSR1 instead of PSR1.Classes.ClassDeclaration, one would remove the entire PSR1 standard, whereas using PSR1.Classes.ClassDeclaration.MultipleClasses would remove this one rule only, but no other rules in the PSR1.Classes.ClassDeclaration sniff.

Removing Rules via Code Comments

Removing a rule/sniff/subset/standard only for a specific file or a part of it can be done by using special phpcs annotations/comments, for example, // phpcs:disable followed by an optional name of a standard/subset/sniff/rule. Like so:

// phpcs:disable PSR1.Classes.ClassDeclaration

For more information about ignoring files, please refer to the official PHP_CodeSniffer Wiki.

IDE Integration

PhpStorm

After installing the package as explained above, open PhpStorm settings, and navigate to

Language & Frameworks -> PHP -> Quality Tools -> PHP_CodeSniffer

Choose "Local" in the "Configuration" dropdown.

Click the "..." button next to the dropdown. It will show a dialog where you need to specify the path for the PHP_CodeSniffer executable.

Open the file selection dialog, navigate to vendor/bin/ in your project, and select phpcs. On Windows, choose phpcs.bat.

Click the "Validate" button next to the path input field. If everything is working fine, a success message will be shown at the bottom of the window.

Still in the PhpStorm settings, navigate to:

Editor -> Inspections

Type codesniffer in the search field before the list of inspections, then select:

PHP -> Quality Tools -> PHP_CodeSniffer validation

Enable it using the checkbox in the list, press "Apply".

Select "PHP_CodeSniffer validation", click the refresh icon next to the "Coding standard" dropdown on the right, and choose Inpsyde.

If you don't see Inpsyde here, you may need to specify the phpcs.xml file by selecting "Custom" as standard and then use the "..." button next to the dropdown.

Once the PhpStorm integration is complete, warnings and errors in your code will automatically be shown in your IDE editor.

Installation

Via Composer, require as development dependency:

composer require --dev inpsyde/php-coding-standards

inpsyde/php-coding-standards 适用场景与选型建议

inpsyde/php-coding-standards 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 172.36k 次下载、GitHub Stars 达 101, 最近一次更新时间为 2017 年 06 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 172.36k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 101
  • 点击次数: 19
  • 依赖项目数: 58
  • 推荐数: 0

GitHub 信息

  • Stars: 101
  • Watchers: 4
  • Forks: 23
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-21