phpyh/coding-standard
PHPyh Coding Standard provides a ready-to-use PHP CS Fixer configuration for consistent formatting across your project. Install via Composer and apply the PhpCsFixerCodingStandard to your .php-cs-fixer.dist.php, with support for overriding rules.
Purpose Alignment:
The phpyh/coding-standard package (v3.0.1) remains a static analysis tool for PHP coding standards, leveraging PHP_CodeSniffer. Its non-framework-specific nature persists as both an advantage (flexibility) and limitation (redundancy risk with Laravel tools like laravel/pint or rector/rector). The new explicit support for array_destructuring and match expressions in trailing_comma_in_multiline directly addresses Laravel 10+ adoption of modern PHP syntax, particularly in:
switch with match in business layers).
The deprecation fixes ensure long-term compatibility with PHP_CodeSniffer updates, critical for Laravel’s frequent dependency upgrades.New Features:
array_destructuring: Explicitly allows trailing commas in destructuring assignments (e.g., [...$items]), reducing false negatives in Laravel’s collection-based logic (e.g., collect($items)->map(fn($item) => [...$item])).match expressions: Aligns with Laravel’s PHP 8.1+ adoption (e.g., match ($request->input('status')) { ... } in controllers). The rule avoids flagging Laravel’s internal match usage (e.g., in Illuminate/Support/), a critical improvement over v3.0.0.allow_array_destructuring, match_expression_support) enable context-aware enforcement, ideal for Laravel’s layered architecture (e.g., strict rules for app/ but lenient for resources/views/).Leverage Points (Updated):
app/Exceptions/Handler.php)..blade.php files (via --extensions=php,blade) bridges the gap between PHP and Blade syntax.Misalignment Risks (Updated):
rector/rector: If rector is used for match expression refactoring, duplicate rules may emerge. Mitigation: Audit rector rules for @match annotations and disable overlapping phpyh rules.@php blocks or legacy Blade templates may trigger unintended exclusions. Mitigation: Test with --extensions=php,blade and exclude problematic paths.phpstan is recommended to avoid CI bottlenecks (e.g., 5–10s runtime for 10K+ files).Compatibility Improvements:
Illuminate/Support/) prevents false positives, a blocker in v3.0.0..blade.php files reduces pre-processing steps, simplifying CI pipelines.Customization:
phpcs.xml:
<rule ref="phpyh/trailing_comma_in_multiline">
<properties>
<property name="allow_array_destructuring" value="true"/> <!-- Laravel collections -->
<property name="match_expression_support" value="true"/> <!-- Controllers -->
<property name="exclude_match_in_illuminate" value="true"/> <!-- Skip Laravel internals -->
</properties>
<exclude name="resources/views/legacy/*.blade.php"/> <!-- Opt-out for old templates -->
</rule>
--runtime-set to toggle features:
./vendor/bin/phpcs --runtime-set testVersion=8.1 --runtime-set allowMatch=true app/
Potential Pitfalls:
match rules may over-enforce in projects using match expressions sparingly (e.g., experimental features). Solution: Start with --warning-severity=5.allow_array_destructuring). Solution: Enforce a centralized phpcs.xml template.@php blocks or nested directives may bypass rules. Solution: Test with --extensions=php,blade and adjust exclusions.Reduced Risks:
New Risks:
rector’s @match or phpstan’s type checks. Mitigation: Audit tools for redundant rules.--extensions=php,blade and explicit <exclude>.Critical Questions:
match_expression_support property is enabled and Laravel internals are excluded.rector or phpstan with overlapping match expression rules?
→ Audit for redundant checks and disable conflicting rules in phpyh.<property name="allow_array_destructuring">.--extensions=php,blade and exclude legacy templates if needed.phpcs.xml with new properties?
→ Document defaults and provide a template for onboarding.Best For:
switch with match in controllers).phpstan.Less Ideal For:
laravel/pint (formatting) + phpstan (typing), evaluate rule redundancy (e.g., trailing commas).match_expression_support) require phpcs.xml adjustments.Assessment Phase:
./vendor/bin/phpcs \
--standard=phpyh \
--runtime-set testVersion=8.1 \
--runtime-set allowMatch=true \
--extensions=php,blade \
--report=json app/ | jq '.files[] | select(.errors | length > 0)'
app/Http/Controllers/ or app/Exceptions/.app/Services/ or collection methods.Pilot:
How can I help you explore Laravel packages today?