pluswerk/grumphp-bom-task
GrumPHP task that enforces files to be saved without a UTF-8 BOM. Install via Composer and enable the PLUS\GrumPHPBomTask\ExtensionLoader, then run the plus_bom_fixer task on selected file types (php, css, json, yml, etc.).
## Getting Started
### Minimal Setup
1. **Install the package** via Composer:
```bash
composer require --dev pluswerk/grumphp-bom-task
grumphp.yml):
parameters:
tasks:
plus_bom_fixer:
triggered_by: [php, json, yml, txt] # Customize file types as needed
extensions:
- PLUS\GrumPHPBomTask\ExtensionLoader
./vendor/bin/grumphp run
Prevent BOM-related issues in PHP/JSON/YAML files by failing the build if any file contains a UTF-8 BOM. Ideal for:
composer.json, package.json, or config files.Pre-commit Hook:
Integrate with Git hooks (e.g., pre-commit) to scan staged files before commit:
./vendor/bin/grumphp run --triggered-by="php,json,yml"
CI Pipeline Gate: Add as a blocking step in CI (e.g., GitHub Actions):
- name: Check for BOMs
run: ./vendor/bin/grumphp run
Combine with Other Tasks:
Pair with grumphp's built-in tasks (e.g., phpcs, phpstan) for a comprehensive pre-commit suite:
tasks:
plus_bom_fixer:
triggered_by: [php, json, yml]
phpcs:
triggered_by: [php]
Exclude Files/Directories:
Use ignore to skip vendor files or legacy systems:
plus_bom_fixer:
ignore:
- vendor/
- legacy/bom-required/
Auto-Fix (Advanced): While this package detects BOMs, you can extend it to remove them by:
grumphp's on_fail hooks to trigger a script.file_put_contents() with FILE_UTF8 flag:
file_put_contents($file, file_get_contents($file), FILE_UTF8);
Config Files:
Enforce BOM-free config/*.php and bootstrap/app.php:
plus_bom_fixer:
triggered_by: [php]
exclude_files: [~^/config/.*$~] # Optional: Exclude specific configs
Artisan Command: Add a custom Artisan command to run BOM checks:
// app/Console/Commands/CheckBOM.php
public function handle()
{
$exitCode = Artisan::call('grumphp', ['run', '--triggered-by=php,json,yml']);
if ($exitCode !== 0) {
$this->error('BOM detected in files!');
exit(1);
}
}
False Positives in Legacy Systems:
ignore to exclude these files.--verbose to see which files fail:
./vendor/bin/grumphp run --verbose
Editor/IDE Conflicts:
File > Save with Encoding > UTF-8 (ensure BOM is unchecked).File > Settings > Editor > File Encodings > Global Encoding = UTF-8 (W/o BOM).GrumPHP Version Mismatch:
Class 'PLUS\GrumPHPBomTask\ExtensionLoader' not found.grumphp is ≥0.16 or ≥1.0 and update dependencies:
composer update --dev grumphp pluswerk/grumphp-bom-task
Case-Sensitive File Paths:
plus_bom_fixer:
ignore:
- "Vendor/OldLibrary/" # Note: Capital 'V' matters!
Binary Files:
.png, .pdf). Exclude them explicitly:
plus_bom_fixer:
triggered_by: [php, json, yml, txt]
exclude_files: [~^\.(png|jpg|pdf|zip)$~]
Check File Contents:
Use xxd or hexdump to inspect files for BOMs:
xxd -c 4 problematic-file.json # Should NOT start with EF BB BF (BOM)
GrumPHP Logs:
Enable debug mode in grumphp.yml:
parameters:
grumphp:
process_timeout: 60
hide_circumvention_tips: false
stop_on_failure: false
Dry Run: Test without failing:
./vendor/bin/grumphp run --dry-run
Custom File Types:
Extend triggered_by for niche formats (e.g., .env, .md):
plus_bom_fixer:
triggered_by: [php, json, yml, txt, env, md]
Dynamic Ignore Rules: Use PHP to dynamically exclude files based on conditions (e.g., CI environment):
plus_bom_fixer:
ignore:
- "tests/BomTest.php" # Example: Skip a test file
Custom Error Messages: Override the default failure message by extending the task class:
// app/GrumPHP/Tasks/CustomBomTask.php
namespace App\GrumPHP\Tasks;
use PLUS\GrumPHPBomTask\BomFixerTask;
class CustomBomTask extends BomFixerTask
{
protected function getMessage(): string
{
return '❌ BOM detected in ' . $this->file . '. Remove it with: `dos2unix --keepdate ' . $this->file . '`';
}
}
Then update grumphp.yml:
extensions:
- App\GrumPHP\Tasks\CustomBomTask
Limit Scanned Files:
Restrict triggered_by to only necessary file types to speed up runs:
plus_bom_fixer:
triggered_by: [php, json] # Exclude slower-to-scan types like YML
Parallelize with GrumPHP:
Combine with parallel tasks in grumphp.yml:
tasks:
plus_bom_fixer:
triggered_by: [php]
phpcs:
triggered_by: [php]
parallel: true
Cached Configs:
bootstrap/cache/ files may regenerate with BOMs if created by tools like php artisan config:clear.plus_bom_fixer:
ignore:
- bootstrap/cache/
Vendor Files:
vendor/composer/autoload*.php) might contain BOMs.composer dump-autoload --optimize to regenerate them cleanly.Environment-Specific Checks:
plus_bom_fixer:
triggered_by: [php]
only_when: "%env('CI') == 'true'"
How can I help you explore Laravel packages today?