wayofdev/cs-fixer-config

🧹 为PHP CS Fixer添加自定义规则集,以实现一致的编码标准。

v1.5.3 2024-06-18 09:13 UTC

README



构建
Build Status

项目
Total Downloads Latest Stable Version Commits since latest release PHP Version Require

质量
Codecov Mutation testing badge PHP Stan Level 9 of 9

社区
Discord Follow on Twitter (X)


PHP CS Fixer Config

PHP-CS-Fixer包的包装器,包含预定义的规则 - 一个用于自动修复PHP编码标准问题的工具。

此存储库旨在为多个项目提供一种标准化的方法来应用编码标准,确保一致性和遵循最佳实践。通过使用预定义的规则集,它简化了设置过程,并允许团队快速将PHP-CS-Fixer集成到其开发工作流程中。


如果您喜欢/使用此包,请考虑⭐️ 星标它。谢谢!


📜 自定义规则集

WayOfDev\PhpCsFixer\Config\RuleSets\DefaultRuleset::class

基于@Symfony规则集

WayOfDev\PhpCsFixer\Config\RuleSets\ExtendedPERSet::class

基于@PER-CS2.0规则集


💿 安装

→ 使用composer

作为依赖项要求

composer req --dev wayofdev/cs-fixer-config

🛠 配置

→ 设置

  • 创建一个PHP文件,命名为.php-cs-fixer.dist.php并将其放置在项目根目录中。它将被PHP CS Fixer自动识别。

  • .php-cs-fixer.dist.php文件的示例内容

     <?php
    
     declare(strict_types=1);
    
     use WayOfDev\PhpCsFixer\Config\ConfigBuilder;
     use WayOfDev\PhpCsFixer\Config\RuleSets\DefaultSet;
    
     require_once 'vendor/autoload.php';
    
     $config = ConfigBuilder::createFromRuleSet(new DefaultSet())
         ->inDir(__DIR__ . '/src')
         ->inDir(__DIR__ . '/tests')
         ->addFiles([__FILE__])
         ->getConfig()
     ;
    
     $config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/php-cs-fixer.cache');
    
     return $config;

→ Composer脚本

  • scripts部分添加到composer.json

    {
        "scripts": {
    +       "cs:diff": "php vendor/bin/php-cs-fixer fix --dry-run -v --diff",
    +       "cs:fix": "php vendor/bin/php-cs-fixer fix -v"
        }
    }

→ Git

  • .build文件夹文件放入.gitignore

    +/.build/
     /vendor/

→ Makefile

  • 如果您正在使用Makefile,创建一个带有lint-phplint-diff目标的Makefile

    +APP_RUNNER ?= php
    +APP_COMPOSER ?= $(APP_RUNNER) composer
    +
    +prepare:
    +  mkdir -p .build/php-cs-fixer
    +.PHONY: prepare
    
    +lint-php: prepare ## Fixes code to follow coding standards using php-cs-fixer
    +  $(APP_COMPOSER) cs:fix
    +.PHONY: lint-php
    
    +lint-diff: prepare ## Runs php-cs-fixer in dry-run mode and shows diff which will by applied
    +  $(APP_COMPOSER) cs:diff
    +.PHONY: lint-diff

或者,您可以从以下任何一个存储库检查我们预配置的Makefile

https://github.com/wayofdev/php-cs-fixer-config/blob/master/Makefile

https://github.com/wayofdev/laravel-package-tpl/blob/master/Makefile

→ GitHub Actions

  • 要在GitHub Actions中使用此包,请向您的存储库添加一个coding-standards.yml工作流程

    ---
    
    on:  # yamllint disable-line rule:truthy
      pull_request:
        branches:
          - master
      push:
        branches:
          - master
    
    name: 🧹 Fix PHP coding standards
    
    jobs:
      coding-standards:
        timeout-minutes: 4
        runs-on: ${{ matrix.os }}
        concurrency:
          cancel-in-progress: true
          group: coding-standards-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
        strategy:
          matrix:
            os:
              - ubuntu-latest
            php-version:
              - '8.1'
            dependencies:
              - locked
        permissions:
          contents: write
        steps:
          - name: ⚙️ Set git to use LF line endings
            run: |
              git config --global core.autocrlf false
              git config --global core.eol lf
    
          - name: 🛠️ Setup PHP
            uses: shivammathur/[email protected]
            with:
              php-version: ${{ matrix.php-version }}
              extensions: none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter
              ini-values: error_reporting=E_ALL
              coverage: none
    
          - name: 📦 Check out the codebase
            uses: actions/[email protected]
    
          - name: 🛠️ Setup problem matchers
            run: |
              echo "::add-matcher::${{ runner.tool_cache }}/php.json"
    
          - name: 🤖 Validate composer.json and composer.lock
            run: composer validate --ansi --strict
    
          - name: 🔍 Get composer cache directory
            uses: wayofdev/gh-actions/actions/composer/[email protected]
    
          - name: ♻️ Restore cached dependencies installed with composer
            uses: actions/[email protected]
            with:
              path: ${{ env.COMPOSER_CACHE_DIR }}
              key: php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}
              restore-keys: php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-
    
          - name: 📥 Install "${{ matrix.dependencies }}" dependencies with composer
            uses: wayofdev/gh-actions/actions/composer/[email protected]
            with:
              dependencies: ${{ matrix.dependencies }}
    
          - name: 🛠️ Prepare environment
            run: make prepare
    
          - name: 🚨 Run coding standards task
            run: composer cs:fix
            env:
              PHP_CS_FIXER_IGNORE_ENV: true
    
          - name: 📤 Commit and push changed files back to GitHub
            uses: stefanzweifel/[email protected]
            with:
              commit_message: 'style(php-cs-fixer): lint php files and fix coding standards'
              branch: ${{ github.head_ref }}
              commit_author: 'github-actions <[email protected]>'
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

或者,您可以从以下任何一个存储库检查我们预配置的工作流程

https://github.com/wayofdev/php-cs-fixer-config/blob/master/.github/workflows/coding-standards.yml


💻 使用方法

通过简单地运行控制台命令来修复编码标准

→ 直接

vendor/bin/php-cs-fixer fix -v

→ 通过Composer脚本

要使用Composer脚本命令

  • 使用php-cs-fixer修复代码以符合编码标准

    composer cs:diff
  • 以干运行模式运行php-cs-fixer并显示将要应用的差异

    composer cs:fix

→ 使用Makefile

要使用Makefile

  • 使用php-cs-fixer修复代码以符合编码标准

    make lint-php
  • 以干运行模式运行php-cs-fixer并显示将要应用的差异

    make lint-diff

🔒 安全策略

此项目有一个安全策略


🙌 想要贡献?

感谢您考虑为wayofdev社区做出贡献!我们欢迎所有类型的贡献。如果您想

非常欢迎您的参与。在贡献之前,请查阅我们的贡献指南

Conventional Commits


🫡 贡献者

Contributors Badge

🌐 社交链接


🧱 资源


⚖️ 许可证

Licence