ibexa/code-style
Ibexa coding standards bundle: PHP-CS-Fixer internal config factory, rulesets, CI/IDE helpers, hooks and contribution templates. Install as a dev dependency and add a .php-cs-fixer.php to apply Ibexa style, with optional parallel runs.
Start by installing the package in your Laravel project’s dev dependencies:
composer require --dev ibexa/code-style:~2.2.0
.php-cs-fixer.php file in your project root with the minimal Ibexa config:
<?php
return Ibexa\CodeStyle\PhpCsFixer\InternalConfigFactory::build()
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->files()->name('*.php')
);
./vendor/bin/php-cs-fixer fix
- name: PHP CS Fixer
run: ./vendor/bin/php-cs-fixer fix --dry-run --diff
Use it to standardize a new Laravel project or align an existing codebase with Ibexa’s conventions before onboarding new developers. Run php-cs-fixer fix --diff to preview changes.
Local Development:
husky or pre-commit) to run:
./vendor/bin/php-cs-fixer fix --allow-risky=yes
.php-cs-fixer.dist.php for real-time feedback.CI/CD Pipeline:
- name: PHP CS Fixer (Fix)
run: ./vendor/bin/php-cs-fixer fix --dry-run --diff
- name: PHP CS Fixer (Validate)
run: ./vendor/bin/php-cs-fixer fix --stop-on-failure
php-cs-fixer:
script:
- ./vendor/bin/php-cs-fixer fix --dry-run --diff
- ./vendor/bin/php-cs-fixer fix --stop-on-failure
Custom Rulesets: Extend Ibexa’s defaults for Laravel-specific needs:
$factory = new Ibexa\CodeStyle\PhpCsFixer\InternalConfigFactory();
$factory->withRules([
'@PSR12' => true, // Merge with PSR-12 for Laravel compatibility
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
]);
->exclude([
'vendor/',
'node_modules/',
])
->files()->name('*.php')->ignoreDotFiles(true)->notPath('resources/views');
$factory->runInParallel(); // or `InternalConfigFactory::build(runInParallel: true)`
composer.json:
"scripts": {
"test": "phpstan analyse --level=5 && php-cs-fixer fix"
}
~/.valet/Laravel/valet.php for global projects:
$this->after('install', function () {
if (file_exists(__DIR__ . '/../.php-cs-fixer.php')) {
exec('composer require --dev ibexa/code-style');
}
});
Rule Conflicts:
import_order may conflict with Laravel’s use statements (e.g., use Illuminate\Support\Facades\*).withRules():
$factory->withRules([
'ordered_imports' => false, // Disable if needed
]);
Performance:
runInParallel) can overload CI systems. Test locally first:
./vendor/bin/php-cs-fixer fix --parallel --verbose
False Positives:
no_unused_imports may flag Laravel’s dynamic use statements (e.g., use \App\{$model}).$factory->withRules([
'no_unused_imports' => true,
'no_unused_imports' => ['ignore_relative_paths' => true],
]);
Version Locking:
~2.2.3) to avoid surprises.--dry-run first to preview changes:
./vendor/bin/php-cs-fixer fix --dry-run --diff
./vendor/bin/php-cs-fixer fix --verbose
$factory->withRules([
'phpdoc_align' => false, // Disable problematic rules
]);
Custom Rulesets:
Create a CustomRuleset class to encapsulate Laravel-specific overrides:
class LaravelRuleset extends Ibexa\CodeStyle\PhpCsFixer\InternalConfigFactory {
public function __construct() {
$this->withRules([
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
]);
}
}
Git Hooks:
Add to .git/hooks/pre-commit:
#!/bin/sh
./vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run || exit 1
CI Feedback:
Use --format=checkstyle for CI tools (e.g., Jenkins, GitLab) to parse violations:
./vendor/bin/php-cs-fixer fix --format=checkstyle > php-cs-fixer.xml
bootstrap/cache/ and storage/ from analysis:
->exclude([
'bootstrap/cache/',
'storage/',
])
CONTRIBUTING.md snippet:
## Code Style
Run `composer cs-fix` to auto-format your code before committing.
time ./vendor/bin/php-cs-fixer fix
time ./vendor/bin/php-cs-fixer fix --parallel
How can I help you explore Laravel packages today?