PHP Code Style Action
Overview
The actions/php/codeStyle action applies comprehensive code style fixes and refactoring to PHP projects by combining both Rector (automated refactoring) and Duster
(Laravel Pint code style fixes). It automatically commits changes and manages git blame history.
Language/Tool Support
- PHP: All PHP projects
- Laravel: Full support with Laravel Pint integration
- Rector: Automated refactoring and modernization
- Duster: Laravel Pint-based code style enforcement
Features
- Dual Tool Integration: Combines Rector and Duster for comprehensive code improvement
- Automatic Commits: Commits changes separately for each tool with descriptive messages
- Git Blame Management: Automatically adds commit hashes to
.git-blame-ignore-revs - Directory Support: Can target subdirectories within repositories
- Composer Integration: Handles dependency caching
- Force Push: Ensures changes are pushed to the target branch
Usage
- name: Apply Code Style
uses: ./actions/php/codeStyle
with:
repository: ${{ github.repository }}
branch: ${{ github.head_ref }}
phpVersion: '8.3'
directory: '.'
Inputs
| Input | Type | Required | Default | Description |
|---|---|---|---|---|
repository |
string | ✅ | - | GitHub repository in format "owner/repo" |
branch |
string | ❌ | main |
Target branch for applying code style changes |
phpVersion |
string | ❌ | 8.3 |
PHP version to use for code style operations |
directory |
string | ❌ | ${{ github.workspace }} |
Directory path relative to workspace |
Required Dependencies
Your project must include both tools in composer.json:
{
"require-dev": {
"tightenco/duster": "^2.0",
"rector/rector": "^1.0"
}
}
Action Steps
- Setup PHP: Configures the specified PHP version
- Checkout Repository: Retrieves code from the specified repository and branch
- Set Permissions: Makes the entrypoint script executable
- Determine Working Directory: Handles workspace vs subdirectory configuration
- Cache Dependencies: Caches Composer dependencies for performance
- Apply Code Style: Runs the entrypoint script that executes both tools
- Force Cache: Saves cache if it wasn't hit
Code Style Process
Phase 1: Rector (Automated Refactoring)
- Runs
rector processto apply automated refactoring rules - Commits changes with message "Rectifying"
- Adds commit hash to
.git-blame-ignore-revs - Creates additional commit to ignore Rector changes in git blame
- Force pushes changes to origin
Phase 2: Duster (Code Style Fixes)
- Runs
duster fixto apply Laravel Pint code style rules - Commits changes with message "Dusting"
- Adds commit hash to
.git-blame-ignore-revs - Creates additional commit to ignore Duster changes in git blame
- Force pushes changes to origin
Configuration Files
Rector Configuration (rector.php)
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/app',
__DIR__ . '/src',
]);
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_83,
]);
};
Duster Configuration (duster.json or pint.json)
{
"preset": "laravel",
"rules": {
"simplified_null_return": true,
"not_operator_with_successor_space": true
}
}
Usage Examples
Basic Usage in Workflow
name: Code Style
on:
pull_request:
branches: [ main ]
jobs:
code-style:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Apply Code Style
uses: ./actions/php/codeStyle
with:
repository: ${{ github.repository }}
branch: ${{ github.head_ref }}
Subdirectory Usage
- name: Apply Code Style to Backend
uses: ./actions/php/codeStyle
with:
repository: ${{ github.repository }}
branch: ${{ github.head_ref }}
directory: 'backend'
phpVersion: '8.2'
Multiple PHP Versions
strategy:
matrix:
php-version: ['8.2', '8.3']
directory: ['api', 'admin', 'frontend']
steps:
- name: Apply Code Style
uses: ./actions/php/codeStyle
with:
repository: ${{ github.repository }}
branch: ${{ github.head_ref }}
phpVersion: ${{ matrix.php-version }}
directory: ${{ matrix.directory }}
Required Permissions
The workflow using this action must have:
permissions:
contents: write # Required for committing and pushing changes
Git Blame Integration
The action automatically manages git blame history by:
- Creating
.git-blame-ignore-revs: File containing commit hashes to ignore - Adding Commit Hashes: Each code style commit is added to the ignore file
- Separate Ignore Commits: Creates dedicated commits for updating the ignore file
- Git Configuration: Teams can configure git to use this file:
git config blame.ignoreRevsFile .git-blame-ignore-revs
Troubleshooting
Common Issues
Rector Failures
- Ensure
rector.phpconfiguration is valid - Check that Rector rules are compatible with your PHP version
- Verify project structure matches configured paths
Duster Failures
- Confirm Duster/Pint configuration is correct
- Check for syntax errors that prevent code style fixes
- Ensure proper Laravel Pint preset is specified
Git Push Failures
- Confirm workflow has
contents: writepermission - Check that the target branch allows force pushes
- Verify branch protection rules don't prevent automated commits
Directory Issues
- Ensure the specified directory exists in the repository
- Check that
composer.jsonexists in the target directory - Verify relative paths are correct
Performance Optimization
Caching
- The action automatically caches Composer dependencies
- Cache keys are based on
composer.lockfile hash - Force cache save ensures cache is updated when needed
Dependency Installation
- Uses
--no-ansi --no-interaction --no-progressfor faster installation - Includes
--prefer-dist --ignore-platform-reqsfor reliability
Security Considerations
- Force Push: Action performs force pushes; ensure branch protection is configured appropriately
- Automated Commits: Review automated commits in sensitive repositories
- Token Permissions: Use minimal required permissions for Composer authentication
Related Actions
- actions/php/rector: Standalone Rector refactoring
- actions/php/duster: Standalone Duster code style fixes
- php_staticAnalysis.yml: Code quality analysis workflow
- php_test.yml: Unit testing workflow
Best Practices
- Run on Pull Requests: Apply code style fixes before merging
- Review Changes: Always review automated code style changes
- Configure Rules: Customize Rector and Duster rules for your project
- Test After Changes: Run tests after code style changes
- Branch Protection: Configure appropriate branch protection rules
- Regular Updates: Keep Rector and Duster dependencies updated
Migration from php_dusterFix.yml
This action replaces the deprecated php_dusterFix.yml workflow with enhanced features:
- Added Rector: Now includes automated refactoring
- Better Git Blame: Improved git blame ignore functionality
- Enhanced Caching: More efficient dependency caching
- Directory Support: Better handling of subdirectories
- Separate Commits: Individual commits for each tool