mmoreram/php-formatter
PHP code formatter with CLI and configurable rules to standardize style across a project. Formats files or entire directories, helps enforce clean, consistent code, and integrates easily into development workflows and CI.
Installation Add the package via Composer:
composer require mmoreram/php-formatter
Require the autoloader in your project:
require __DIR__ . '/vendor/autoload.php';
First Use Case Format a single PHP file:
use Mmore\PhpFormatter\PhpFormatter;
$formatter = new PhpFormatter();
$formattedCode = $formatter->formatFile('path/to/your/file.php');
file_put_contents('path/to/your/file.php', $formattedCode);
Where to Look First
src/PhpFormatter.php for core functionality and src/PhpFormatterException.php for error handling.tests/ for real-world usage patterns.Formatting a Single File
$formatter = new PhpFormatter();
$formattedCode = $formatter->formatFile('app/Controller.php');
Formatting a Directory Recursively
$formatter = new PhpFormatter();
$formatter->formatDirectory('src/', ['.php']);
Customizing Formatting Rules Override default rules (e.g., indentation, line breaks):
$formatter = new PhpFormatter();
$formatter->setIndentation(4); // Use 4 spaces instead of tabs
$formatter->setLineBreak("\n"); // Force LF line endings
Integration with Laravel Artisan Create a custom Artisan command:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Mmore\PhpFormatter\PhpFormatter;
class FormatCode extends Command
{
protected $signature = 'code:format {--directory= : Target directory}';
protected $description = 'Format PHP code using php-formatter';
public function handle()
{
$formatter = new PhpFormatter();
$directory = $this->option('directory') ?: app_path();
$formatter->formatDirectory($directory, ['.php']);
$this->info('Code formatted successfully!');
}
}
Register the command in app/Console/Kernel.php:
protected $commands = [
\App\Console\Commands\FormatCode::class,
];
Pre-Commit Hook Use the package in a Git pre-commit hook to enforce formatting:
# .git/hooks/pre-commit
#!/bin/bash
php artisan code:format --directory=src/
CI/CD Pipeline Add a step to format code before running tests:
# .github/workflows/ci.yml
jobs:
test:
steps:
- run: composer require mmoreram/php-formatter
- run: php artisan code:format --directory=src/
- run: php artisan test
Archived Package
No Laravel-Specific Features
app_path(), base_path()) manually.Performance on Large Codebases
vendor/, node_modules/, and generated files (e.g., bootstrap/cache/).Edge Cases in Formatting
<<<EOF blocks might break.<?php ?> snippets in Blade templates.Configuration Overrides
config/php-formatter.php) and hydrate the formatter dynamically:
$formatter = new PhpFormatter();
$formatter->setIndentation(config('php-formatter.indentation'));
Error Handling
PhpFormatterException for issues like unreadable files.try {
$formatter->formatFile('unreadable-file.php');
} catch (\Mmore\PhpFormatter\PhpFormatterException $e) {
$this->error($e->getMessage());
}
Dry Run Mode Log changes before applying them:
$formatter->setDryRun(true); // Logs changes without writing files
$formatter->formatDirectory('src/');
Verbose Output Enable debug mode to see skipped files or errors:
$formatter->setVerbose(true);
Check File Permissions Ensure the script has write access to target files/directories:
chmod -R 775 src/
Custom Rules
Extend PhpFormatter by overriding methods like formatFile() or formatLine():
class CustomFormatter extends \Mmore\PhpFormatter\PhpFormatter
{
protected function formatLine($line)
{
// Add custom logic (e.g., trim whitespace)
return trim($line);
}
}
Pre/Post-Processing Hook into the formatting pipeline:
$formatter->setPreProcessor(function ($code) {
return str_replace('// OLD_COMMENT', '', $code);
});
$formatter->setPostProcessor(function ($code) {
return '<?php ' . $code; // Ensure opening tag
});
Integration with PHP-CS-Fixer
Combine with friendsofphp/php-cs-fixer for advanced formatting:
$formatter = new PhpFormatter();
$csFixer = new \PhpCsFixer\Config();
$code = $formatter->formatFile('file.php');
$code = $csFixer->fix($code);
How can I help you explore Laravel packages today?