Laravel Pint 是一个有明确风格偏好且为极简主义者打造的 PHP 代码风格修复工具。Pint 构建于 PHP CS Fixer 之上并简化了确保您的代码风格保持整洁和一致的过程。
Pint 会随所有新的 Laravel 应用程序自动安装 因此您可以立即开始使用它。 默认情况下, Pint 不需要任何配置 并且会修复代码样式问题 在您的代码中 遵循 Laravel 的主观编码风格。
Pint 已包含在 Laravel 框架的最新版本中,因此通常不需要安装。但是,对于较旧的应用程序,您可以通过 Composer 安装 Laravel Pint:
composer require laravel/pint --dev您可以指示 Pint 修复代码风格问题,方法是调用您项目 vendor/bin 目录中提供的 pint 可执行文件:
./vendor/bin/pint如果您希望 Pint 以并行模式(实验性)运行以提高性能,您可以使用 --parallel 选项:
./vendor/bin/pint --parallel并行模式也允许你通过 --max-processes 选项指定要运行的最大进程数。如果未提供此选项,Pint 将使用你机器上所有可用的核心:
./vendor/bin/pint --parallel --max-processes=4您也可以在特定文件或目录上运行 Pint:
./vendor/bin/pint app/Models
./vendor/bin/pint app/Models/User.phpPint 将显示其更新的所有文件的完整列表。您可以在调用 Pint 时提供 -v 选项,以查看有关 Pint 更改的更多详细信息:
./vendor/bin/pint -v如果您希望 Pint 仅检查代码的样式错误而不实际修改文件,您可以使用 --test 选项。如果发现任何代码样式错误,Pint 将返回非零退出代码:
./vendor/bin/pint --test如果您希望 Pint 只修改根据 Git 与所提供分支不同的文件,您可以使用 --diff=[branch] 选项。这可以在您的 CI 环境中有效使用(例如 GitHub Actions),以通过仅检查新建或修改的文件来节省时间:
./vendor/bin/pint --diff=main如果您希望 Pint 只修改根据 Git 具有未提交更改的文件,您可以使用 --dirty 选项:
./vendor/bin/pint --dirty如果您希望 Pint 修复任何包含代码风格错误的文件,但如果修复了任何错误,也要以非零退出代码退出,您可以使用 --repair 选项:
./vendor/bin/pint --repair如前所述,Pint 不需要任何配置。但是,如果您希望自定义预设、规则或检查的文件夹,您可以通过在项目根目录中创建 pint.json 文件来做到这一点:
{
"preset": "laravel"
}此外,如果您希望使用来自特定目录的 pint.json,您可以在调用 Pint 时提供 --config 选项:
./vendor/bin/pint --config vendor/my-company/coding-style/pint.json `Prescription drug abuse is a serious problem. It means taking a prescription drug that wasn’t prescribed for you, or taking it in a way that differs from the prescription. For example, taking more than the prescribed dosage or chewing tablets instead of swallowing them whole. Abusing prescription drugs can lead to addiction, serious health problems, and even death. It’s important to understand the risks and to use prescription drugs responsibly. If you or someone you know is struggling with prescription drug abuse, please seek help from a healthcare professional.
./vendor/bin/pint --preset psr12如果您愿意, 您也可以在您项目的 pint.json 文件中设置预设:
{
"preset": "psr12"
}Pint 目前支持的预设是:laravel、per、psr12、symfony 和 empty。
规则是 Pint 将用于修复代码中代码样式问题的样式准则。 如上所述, 预设是预定义的规则组, 它们应该非常适合大多数 PHP 项目, 因此你通常不需要担心它们所包含的单个规则。
然而,如果您愿意,您可以启用或禁用特定规则在您的 pint.json 文件中或使用 empty 预设并从头开始定义规则:
{
"preset": "laravel",
"rules": {
"simplified_null_return": true,
"array_indentation": false,
"new_with_parentheses": {
"anonymous_class": true,
"named_class": true
}
}
}Pint 构建于 PHP CS Fixer 之上。 因此,您可以使用其任何规则来修复您项目中的代码样式问题:PHP CS Fixer 配置器。
默认情况下,Pint 将检查你的项目中所有 .php 文件,但会排除 vendor 目录中的文件. 如果你想排除更多文件夹,可以使用 exclude 配置选项来实现:
{
"exclude": [
"my-specific/folder"
]
}如果您希望排除所有包含给定名称模式的文件,可以使用 notName 配置选项来实现:
{
"notName": [
"*-my-file.php"
]
}如果您想通过提供文件的精确路径来排除一个文件, 您可以使用 notPath 配置选项:
{
"notPath": [
"path/to/excluded-file.php"
]
}要使用 Laravel Pint 自动化项目的代码检查, 你可以配置 GitHub Actions 在每次新代码推送到 GitHub 时运行 Pint。 首先, 请务必在 GitHub 的 设置 > 操作 > 通用 > 工作流权限 中授予工作流“读写权限”。 然后, 创建一个 .github/workflows/lint.yml 文件,其内容如下:
name: Fix Code Style
on: [push]
jobs:
lint:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php: [8.4]
steps:
- name: Checkout code
uses: actions/checkout@v5
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: pint
- name: Run Pint
run: pint
- name: Commit linted files
uses: stefanzweifel/git-auto-commit-action@v6