sylius-labs/coding-standard
Battle-tested Sylius coding standard for PHP. Install via Composer and import the provided ecs.php into your EasyCodingStandard (ECS) config to apply consistent formatting and rules across your project. Includes guidance for migrating from YAML to PHP configs.
## Getting Started
### Minimal Steps
1. **Installation**
Run:
```bash
composer require --dev sylius-labs/coding-standard:^4.5
This updates the Sylius coding standard to the latest version (v4.5.1) as a dev dependency.
Basic Integration
In your ecs.php (EasyCodingStandard config file), import the Sylius rules:
$ecsConfig->import(__DIR__ . '/vendor/sylius-labs/coding-standard/ecs.php');
First Use Case Run the linter on your project:
vendor/bin/ecs check src tests
Fix issues automatically (where possible):
vendor/bin/ecs fix src tests
Where to Look First
vendor/sylius-labs/coding-standard/ecs.php) to understand rule priorities and overrides.Project-Wide Standardization Use the Sylius coding standard to enforce consistency across:
src/, app/).tests/).Custom Rule Overrides
Extend or override Sylius rules in your ecs.php:
$ecsConfig->import(__DIR__ . '/vendor/sylius-labs/coding-standard/ecs.php');
$ecsConfig->ruleWithConfiguration(Symplify\EasyCodingStandard\ValueObject\Option\SetList::class, [
SetList::SYLIUS => true,
SetList::PSR_12 => true, // Add PSR-12 for broader compatibility
]);
CI/CD Integration Add ECS checks to your pipeline (e.g., GitHub Actions):
- name: Run ECS
run: vendor/bin/ecs check src tests --ansi
Fail the build on violations:
- name: Enforce ECS
run: vendor/bin/ecs check src tests --ansi --no-interaction --error-format=github
Pre-Commit Hooks
Use ecs with tools like PHP-CS-Fixer or Husky to catch issues early:
composer require --dev php-cs-fixer
vendor/bin/php-cs-fixer fix --rules=@Sylius --dry-run
Team Onboarding
Document the coding standard in your CONTRIBUTING.md:
## Code Style
We use the [Sylius Coding Standard](https://github.com/SyliusLabs/CodingStandard) (v4.5.1).
Run `composer lint` to check your changes.
Daily Development
composer lint (alias for ecs check) before committing.ecs fix to auto-correct common issues (e.g., spacing, docblocks).Pull Request Reviews
Legacy Code Migration
ecs check to identify violations.--parallel for large codebases:
vendor/bin/ecs check src --parallel=4
Custom Rule Sets
$ecsConfig->ruleWithConfiguration(
Symplify\EasyCodingStandard\ValueObject\Option\ParallelOption::class,
true
);
$ecsConfig->ruleWithConfiguration(
Symplify\EasyCodingStandard\ValueObject\Option\PathsOption::class,
[__DIR__ . '/src', __DIR__ . '/tests']
);
ECS Version Conflict (NEW)
>=13.1.3, you must either:
<13.1.3:
composer require symplify/easy-coding-standard:^13.0
composer.json:
{
"require-dev": {
"symplify/easy-coding-standard": "^12.0"
}
}
Rule Conflicts
ecs.php:
$ecsConfig->ruleWithConfiguration(
Symplify\EasyCodingStandard\ValueObject\Option\ParallelOption::class,
true
)->ruleWithConfiguration(
Symplify\EasyCodingStandard\ValueObject\Option\SetList::class,
[SetList::SYLIUS => true, SetList::PSR_12 => false]
);
Performance with Large Codebases
src/ + tests/ can be slow.--parallel or exclude specific paths:
vendor/bin/ecs check src --exclude=src/Migrations
False Positives
$ecsConfig->ruleWithConfiguration(
Symplify\EasyCodingStandard\ValueObject\Option\Skip::class,
[__DIR__ . '/src/Some/Class.php']
);
YML to PHP Migration
.ecs.yml, use config-transformer:
composer require --dev symplify/config-transformer
vendor/bin/config-transformer transform .ecs.yml ecs.php
Verbose Output
Run ECS with --verbose to debug rule application:
vendor/bin/ecs check src --verbose
Dry Runs
Use --dry-run to preview changes:
vendor/bin/ecs fix src --dry-run
Rule-Specific Debugging Isolate rule issues by running a single rule set:
vendor/bin/ecs check src --config=vendor/sylius-labs/coding-standard/rules/arrays.php
ECS Version Debugging (NEW) If you encounter conflicts, check versions with:
composer show symplify/easy-coding-standard sylius-labs/coding-standard
Ensure compatibility by pinning versions in composer.json.
Custom Aliases
Add a composer.json script for quick access:
{
"scripts": {
"lint": "ecs check src tests",
"lint:fix": "ecs fix src tests"
}
}
Partial Adoption
Start with a subset of rules (e.g., only arrays, docblocks):
$ecsConfig->ruleWithConfiguration(
Symplify\EasyCodingStandard\ValueObject\Option\SetList::class,
[SetList::SYLIUS_ARRAYS => true]
);
Sylius-Specific Rules
Leverage Sylius’ Laravel-focused rules (e.g., laravel-controller):
$ecsConfig->ruleWithConfiguration(
Symplify\EasyCodingStandard\ValueObject\Option\ParallelOption::class,
true
)->ruleWithConfiguration(
Symplify\EasyCodingStandard\ValueObject\Option\SetList::class,
[SetList::SYLIUS_LARAVEL => true
How can I help you explore Laravel packages today?