dragon-code/codestyler

Dragon Code 提供的一个自动修复编码风格标准问题的工具。

安装次数: 1,238,403

依赖项: 0

建议者: 0

安全性: 0

星标: 25

关注者: 2

分支: 0

开放问题: 0

类型:项目

4.2.0 2024-03-27 19:21 UTC

README

the dragon code code styler

Stable Version Unstable Version Total Downloads License

介绍

Dragon Code Styler 是一个面向简约主义者,具有偏见的 PHP 代码风格修复器。 Codestyler 基于 Laravel PintPHP-CS-Fixer 构建,使得确保您的代码风格保持清洁和一致变得简单。

默认情况下,Codestyler 不需要任何配置,并且会根据 PER 规则集,遵循 The Dragon Code 的具有偏见的编码风格来修复您的代码中的编码风格问题。

感谢您使用您所使用的包的开发者最简单和免费的方式是“星”GitHub 仓库。

更多信息请查看 https://github.com/symfony/thanks

安装

必需

  • PHP: ^8.1
  • Composer: ^2.0

本地

composer global require dragon-code/codestyler

使用方法

当您在项目根目录下运行命令时,将自动读取 composer.json 文件,从中获取您项目的最小 PHP 版本。

这是为了制定应用 Codestyler 的规则。

例如,如果您的项目支持 PHP 8.0 及以上版本,并且您在其中使用了 mkdir($path, 0755) 函数,那么应用 PHP 8.0 的规则将破坏您的代码,因为它会将 0755 替换为 0o755mkdir($path, 0o755))。

为了避免这种情况发生,我们检查最小 PHP 版本。

请注意,只有在启动脚本执行在包含它的文件夹中时才会读取 composer.json 文件。

CLI

# Check code-style
codestyle --test

# Fix code-style
codestyle

# Update `.editorconfig`
codestyle editorconfig

# Update Dependabot rules
codestyle dependabot

# Publishes code-style settings for the phpStorm IDE
codestyle phpstorm

# Show list of available commands
codestyle list

选项

路径

修复路径

codestyle foo/bar

测试

测试代码风格错误而不修复它们

codestyle --test

配置

应使用的配置。目标目录将从 Laravel Pint 读取 pint.json 文件,但不包括样式集。

codestyle --config=foo/bar

风险

允许设置是否可以运行风险规则

codestyle --risky --test
codestyle --risky

仅修复有未提交更改的文件。

codestyle --dirty

退出

测试代码风格错误而不修复它们,并在第一个错误时停止

codestyle --bail

输出格式

应使用的输出格式。

codestyle --format

可用格式的列表

  • checkstyle
  • gitlab
  • json
  • junit
  • txt
  • xml

帮助命令

要查看可用命令的列表,可以调用控制台命令

codestyle list

要查看关于命令的扩展信息,可以使用 help 选项。例如,

codestyle --help
codestyle dependabot --help
codestyle editorconfig --help

GitHub 动作

注意

从代码风格器版本 4.2.0 开始,我们将不再支持 GitHub Actions 的 容器

相反,请使用下面的示例直接使用依赖项安装。

创建一个新的 .github/workflows/code-style.yml 文件并将内容添加到其中

name: Code Style

on: [ push, pull_request ]

permissions: write-all

jobs:
    check:
        runs-on: ubuntu-latest

        if: ${{ github.event_name != 'push' || github.ref != 'refs/heads/main' }}

        steps:
            -   name: Checkout code
                uses: actions/checkout@v4

            -   name: Setup PHP
                uses: shivammathur/setup-php@v2
                with:
                    extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv, json
                    coverage: none

            -   name: Install dependency
                run: composer global require dragon-code/codestyler

            -   name: Check the code-style
                run: codestyle --test

    fix:
        runs-on: ubuntu-latest

        if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}

        steps:
            -   name: Checkout code
                uses: actions/checkout@v4

            -   name: Setup PHP
                uses: shivammathur/setup-php@v2
                with:
                    extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv, json
                    coverage: none

            -   name: Setup Composer
                run: |
                    composer global config --no-plugins allow-plugins.dragon-code/codestyler true
                    composer global config --no-plugins allow-plugins.ergebnis/composer-normalize true

                    composer config --no-plugins allow-plugins.dragon-code/codestyler true
                    composer config --no-plugins allow-plugins.ergebnis/composer-normalize true

            -   name: Install dependencies
                run: |
                    composer global require dragon-code/codestyler
                    composer global require ergebnis/composer-normalize

            -   name: Fix the code-style
                run: |
                    # Copies the `.editorconfig` file to the folder from which the command is run.
                    # The file contains a complete set of instructions for the IDE that supports EditorConfig.
                    codestyle editorconfig

                    # Copies the `The_Dragon_Code_phpStorm.xml` file to the folder from which the command is run.
                    # The file contains a complete set of instructions for JetBrains PhpStorm.
                    codestyle phpstorm

                    # Creates or updates the `dependabot.yml` file for GitHub Actions.
                    codestyle dependabot

                    # The main script for fixing the project code style
                    codestyle

                    # Provides a composer plugin for normalizing `composer.json`.
                    composer normalize

            -   name: Create a Pull Request
                uses: peter-evans/create-pull-request@v6
                with:
                    branch: code-style
                    branch-suffix: random
                    delete-branch: true
                    title: "🦋 The code style has been fixed"
                    commit-message: 🦋 The code style has been fixed
                    body: The code style has been fixed
                    labels: code-style

您还可以通过链接到我们的设置来使用简化的配置。

在这种情况下,将应用以下设置

  • 始终检查事件是否不等于 push 或分支是否不等于 main
  • 纠正代码风格将采取以下步骤
    • 将添加以下插件到您的 composer.json 文件中允许的插件列表 允许的插件
      • dragon-code/codestyler
      • ergebnis/composer-normalize
    • 更新 .github/dependabot.yml 文件
    • 更新 .editorconfig 文件
    • 将纠正 composer.json 文件中元素的顺序以匹配 官方模式
    • 纠正您的项目的代码风格。
name: Code Style

on: [ push, pull_request ]

permissions: write-all

jobs:
    style:
        name: Code Style
        uses: TheDragonCode/.github/.github/workflows/code-style.yml@main

其他 CI/CD

composer global require dragon-code/codestyler

codestyle <command>

IDE

在执行 codestyle editorconfig 控制台命令后,将添加一个 .editorconfig 文件到您的应用程序中。如果文件已存在,它将被替换。

为了使您的 IDE 从该文件中读取代码风格设置,请确保在设置中已启用其支持。

例如,在 phpStorm 中,设置在 文件 | 设置 | 编辑器 | 代码风格

image

您还可以使用 codestyle phpstorm 控制台命令将模式 xml 文件发布到 phpStorm。您可以将此文件导入到 IDE 中。

许可证

此软件包采用 MIT 许可证 许可。