定制 gamegos/php-code-sniffer 二次开发

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

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

gamegos/php-code-sniffer

Composer 安装命令:

composer require gamegos/php-code-sniffer

包简介

Gamegos PHP Code Sniffer

README 文档

README

Gamegos PHP Code Sniffer is a PHP code standard checker/beautifier/fixer tool based on PHP_CodeSniffer and includes custom sniffs used in PHP projects developed by Gamegos.

Requirements

Gamegos PHP Code Sniffer requires PHP 5.3 or later.

Install via Composer

{
    "require-dev": {
        "gamegos/php-code-sniffer": "*"
    }
}

Binaries

Binaries are located in bin directory but composer installer creates links under your vendor binary directory depending on your composer configuration.

  • phpcs : Checks PHP files against defined coding standard rules.
  • phpcbf : Corrects fixable coding standard violations.
  • phpcs-pre-commit : Runs phpcs for modified files in git repository.

Pre-Commit Hook

Save the following script as .git/hooks/pre-commit by replacing COMPOSER_BIN_DIR as your vendor binary directory name depending on your composer configuration.

#!/bin/sh
./COMPOSER_BIN_DIR/phpcs-pre-commit

Make sure the hook script is executable.

chmod +x .git/hooks/pre-commit

Customize

You can customize configuration by adding a file called phpcs.xml file into the root directory of your project. The phpcs.xml file has exactly the same format as a normal ruleset.xml file, so all the same options are available in it. You need to define Gamegos rule to import all the Gamegos rules.

<?xml version="1.0" encoding="UTF-8"?>
<ruleset>
    <rule ref="Gamegos" />
</ruleset>

Using a custom bootstrap file

You can add custom bootstap files to be included before beginning the run. Some sniffs need to load classes from your project; so adding a autoload file will allow sniffs to do this.

<?xml version="1.0" encoding="UTF-8"?>
<ruleset>
    <rule ref="Gamegos" />
    <arg name="bootstrap" value="vendor/autoload.php" />
</ruleset>

Imported Standards

PSR2

All PSR2 sniffs except Squiz.WhiteSpace.ControlStructureSpacing are imported by default.

  • PSR2.ControlStructures.ElseIfDeclaration.NotAllowed rule type is considered as error instead of warning.

Generic

Imported sniffs:

Squiz

Imported sniffs:

  • Squiz.Commenting.DocCommentAlignment
  • Squiz.Commenting.InlineComment
    • InvalidEndChar rule type is considered as warning instead of error.
  • Squiz.WhiteSpace.SuperfluousWhitespace
  • Squiz.WhiteSpace.OperatorSpacing

Custom Sniffs

Gamegos.Arrays.ArrayDeclaration

  • Extended from Squiz.Arrays.ArrayDeclaration.
  • Arranged array element indents by start position of the first (declaration) line.
  • Number of spaces before array elements is increased from 1 to 4.
  • Removed rules:
    • NoKeySpecified
    • KeySpecified
    • MultiLineNotAllowed
    • NoCommaAfterLast
    • NoComma

Gamegos.Commenting.DocComment

  • Extended from Generic.Commenting.DocComment.
  • Ignored MissingShort rule for override methods and PHPUnit test class methods [1].
  • Changed MissingShort rule type from error to warning.
  • Removed rules for comments with long descriptions:
    • SpacingBetween
    • LongNotCapital
    • SpacingBeforeTags
    • ParamGroup
    • NonParamGroup
    • SpacingAfterTagGroup
    • TagValueIndent
    • ParamNotFirst
    • TagsNotGrouped

Gamegos.Commenting.FunctionComment

  • Extended from PEAR.Commenting.FunctionComment.
  • Allowed override methods and PHPUnit test class methods without doc comments [1].
  • Added {@inheritdoc} validation for override methods [1].
  • Removed MissingParamComment, MissingReturn, SpacingAfterParamType and SpacingAfterParamName rules.
  • Ignored MissingParamTag rule for PHPUnit test class methods [1].

Gamegos.Commenting.VariableComment

  • Extended from Squiz.Commenting.VariableComment.
  • Added bool and int into allowed variable types.

Gamegos.Formatting.DisallowMultipleStatements

  • Extended from Generic.Formatting.DisallowMultipleStatements.
  • Fixed adding 2 blank lines when applying SameLine fixer with Squiz.Functions.MultiLineFunctionDeclaration.ContentAfterBrace fixer together.

Gamegos.Strings.ConcatenationSpacing

This sniff has two rules and fixes.

  • PaddingFound: There must be only one space between the concatenation operator (.) and the strings being concatenated.
  • NotAligned: Multiline string concatenations must be aligned.

Gamegos.WhiteSpace.FunctionSpacing

  • Extended from Squiz.WhiteSpace.FunctionSpacing.
  • Expected no blank lines before the method which is the first defined element of a class.
  • Expected no blank lines after the method which is the last defined element of a class.
  • Fixed fixing spaces before method definitions.

Gamegos.WhiteSpace.MemberVarSpacing

  • Extended from Squiz.WhiteSpace.MemberVarSpacing.
  • Expected no blank lines before the property which is the first defined element of a class.
  • Fixed fixing spaces before property definitions.

Development

Live Testing

You can test any modifications by running phpcs.php, phpcbf.php and phpcs-pre-commit.php scripts under scripts directory.

Building Binaries

Run the command below to re-build binaries:

php scripts/build.php

PHP_CodeSniffer Dependency

Current version is built on PHP_CodeSniffer 2.9.2 which is locked in composer.lock file. To import new versions; edit composer.json file if required and run composer update command, then commit the modified composer.lock file. Updating PHP_CodeSniffer version may break some of Gamegos sniffs, so you must carefully track any changes on PHP_CodeSniffer before updating.

[1] A class loader is required (eg. via a bootstrap file), otherwise a warning (Internal.Gamegos.NeedClassLoader) will be generated. You can override this rule in phpcs.xml file in your project to prevent warnings.

License Notices

PHP_CodeSniffer is licensed under the BSD 3-Clause license.

gamegos/php-code-sniffer 适用场景与选型建议

gamegos/php-code-sniffer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.42k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2015 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 gamegos/php-code-sniffer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 13
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-22