承接 silverorange/coding-standard 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

silverorange/coding-standard

Composer 安装命令:

composer require silverorange/coding-standard

包简介

silverorange PHP coding standards.

README 文档

README

Build Status

Coding standards for silverorange PHP projects. These are standards to be used with the PHPCS tool.

Per-Project Usage

per-project configuration is preferred over global configuration but does require support to be added to the project. To use these rules for a project:

1. install as a require-dev dependency using composer

$ composer require --dev silverorange/coding-standard squizlabs/php_codesniffer

2. add a post-install-cmd and post-update-cmd to register the coding standard with phpcs

Post install and post update are both required because composer install without a lock file will not execute the post-install-cmd script.

{
  "scripts": {
    "post-install-cmd": "./vendor/bin/phpcs --config-set installed_paths vendor/silverorange/coding-standard/src",
    "post-update-cmd": "./vendor/bin/phpcs --config-set installed_paths vendor/silverorange/coding-standard/src"
  }
}

3. create a phpcs.xml

<?xml version="1.0"?>
<ruleset name="MyProjectName">
  <description>A custom coding standard.</description>

  <arg name="colors"/>
  <arg name="tab-width" value="4"/>
  <arg name="extensions" value="php"/>
  <arg name="encoding" value="utf-8"/>
  <arg name="warning-severity" value="0"/>

  <rule src="SilverorangeLegacy"/>
</ruleset>

4. create a composer script to lint the project

{
  "scripts": {
    "lint": "./vendor/bin/phpcs"
  }
}

5. add a Jenkins pipeline stage to automatically lint files

The code lint pipeline stage should be added before other pipeline stages so they do not run if the code lint fails. See the Jenkins Pipeline Manual for help with adding a stage to an existing pipeline, or for help creating a new pipeline.

For new packages:

stage('Lint Modified Files') {
  when {
    not {
      branch 'master'
    }
  }
  steps {
    sh '''
      master_sha=$(git rev-parse origin/master)
      newest_sha=$(git rev-parse HEAD)
      files=$(git diff --diff-filter=ACRM --name-only $master_sha...$newest_sha)

      if [ -n "$files" ]; then
        ./vendor/bin/phpcs \
          --standard=Silverorange \
          --tab-width=4 \
          --encoding=utf-8 \
          --warning-severity=0 \
          --extensions=php \
          $files
      fi
    '''
  }
}

For legacy packages:

stage('Lint Modified Files') {
  when {
    not {
      branch 'master'
    }
  }
  steps {
    sh '''
      master_sha=$(git rev-parse origin/master)
      newest_sha=$(git rev-parse HEAD)
      files=$(git diff --diff-filter=ACRM --name-only $master_sha...$newest_sha)

      if [ -n "$files" ]; then
        ./vendor/bin/phpcs \
          --standard=SilverorangeTransitional \
          --tab-width=4 \
          --encoding=utf-8 \
          --warning-severity=0 \
          --extensions=php \
          $files
      fi
    '''
  }
}

Global Usage

The SilverorangeLegacy standard can be set to be used by default if no per-project configuration is available.

1. Install standard globally

$ composer global require silverorange/coding-standard:dev-master

2. Register the standard with PHP Code Sniffer

You can use commas to delineate multiple paths.

$ phpcs --config-set installed_paths ~/.composer/vendor/silverorange/coding-standard/src

3. Set the global phpcs standard

$ phpcs --config-set default_standard SilverorangeLegacy

Now calling phpcs with no additional arguments will use the SilverorangeLegacy standard.

Standards

Several standards are provided:

SilverorangeLegacy

Intended for linting the entire project for a legacy package. This omits some rules we'd like to use for new code written for legacy packages in order to run without error. It is not recommended to use this standard for new projects.

Documentation exists for the legacy standard.

SilverorangeTransitional

Intended for use as a post-commit hook or CI test. This ensures all new code added to legacy packages follows best practices within the legacy guidelines. This includes rules that will not pass for the entire project, but should pass for all modified files in a new pull request.

SilverorangeTransitionalPrettier

Based on PSR-2 standard but updated to support Prettier code auto-formatting. The PSR-4 autoloading rules of PSR-2 are relaxed to allow our legacy code to comply with the ruleset. This standard should be used for all legacy silverorange PHP packages.

Silverorange

Based on PSR-2. PSR-2 builds on, and includes all rules from PSR-1, The Silverorange standard extends PSR-2 to add forbidden functions.

For autoloading classes, projects must follow PSR-4. This allows efficient auto-loading and promotes organizing code using namespaces.

SilverorangePrettier

Based on Silverorange standard but updated to support Prettier code auto-formatting. In addition to PSR-2 rules, additional checks are included to promote code quality and consistency. This standard should be used for all new silverorange PHP packages.

SilverorangePEAR

Based on PEAR standard but updated to not require method and class documentation. This should not be used for new packages.

Sublime Setup

If you are using Sublime Text:

  1. Set up Sublime Linter with PHPCS as described here.
  2. In the Sublime Linter settings, make sure you have the following settings (do not remove the other settings):
    {
      "user": {
        "linters": {
          "phpcs": {
            "@disable": false,
            "args": []
          }
        }
      }
    }
  3. Create a Sublime project in the project root.
  4. Add the following to the Sublime project settings:
    {
      "SublimeLinter": {
        "linters": {
          "phpcs": {
            "phpcs_executable_path": "{project}/vendor/bin/phpcs"
          }
        }
      }
    }

This will allow you to use different rulesets with different projects.

Atom Setup

If you are using Atom:

  1. Set up linter-phpcs as described here.
  2. Open the package settings for linter-phpcs and set Coding Standard Or Config File to SilverorangeLegacy or whichever current coding standard you are using. Also set the Tab Width field to 4.

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 17
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-02-13