pmjones/php-styler
PHP-Styler is a PHP 8.1+ code formatter that completely rewrites formatting for consistent spacing, indentation, and line wrapping. It preserves program logic and comments, aims for diff-friendly output, and supports customizable styles, rules, and parses via a token-based pipeline.
Enhanced Class Structure Standardization
The new NormalizeMemberOrder rule enables teams to enforce canonical class member ordering (e.g., traits → constants → properties → methods), reducing cognitive load when navigating codebases. This aligns with initiatives to improve code readability and maintainability, especially for large or multi-contributor projects.
Example: A team adopting Symfony’s coding standards can now enforce use statements before constants via NormalizeMemberOrder, while overriding the sequence for legacy codebases.
Refined Developer Experience (DX) with Context-Aware Formatting
__construct, __toString), enabling targeted formatting rules (e.g., NormalizeMemberSpacing for magic methods).Foo::match()) ensure consistent parsing across edge cases, reducing false positives in formatting.NormalizeMemberOrder rule’s diff-friendly output (preserving blank lines only when needed) makes it ideal for on-save formatting in tools like PHPStorm or VSCode.Performance and Scalability Improvements
NestingStack and optimizing token operations (e.g., replaceLastSplit() O(1)) improves throughput for large codebases (e.g., monorepos).--workers flag (from prior versions) remains viable for CI/CD pipelines, ensuring fast formatting even with 10K+ files.Legacy Code Modernization with Precision
array() to []).InsertPublicVisibility prevent incomplete method declarations, critical for refactoring legacy code.CI/CD and Automated Compliance
--force Flag: Ensures deterministic formatting in CI (no cached outputs), aligning with immutable build pipelines.DeclarationFormat: By default, classes are now reordered on every run, reducing drift in PR diffs and enforcing consistency.Opinionated Yet Extensible Formatting
NormalizeMemberSpacing now runs after assembly, giving teams finer control over inter-member spacing (e.g., collapsing blank lines between constants).order parameter in NormalizeMemberOrder allows teams to define vendor-specific or project-specific sequences (e.g., ENUM_CASE before PROPERTY).Avoid If:
php-styler explicitly excludes these (use PHP-CS-Fixer or ECS instead).--force and assumes PHP 8.4+).--force flag is removed; formatting is now always applied).--force for selective runs)—this version enforces full scans.Consider If:
use → constant → property → method) with configurable sequences.public insertion).NestingStack optimizations and O(1) token operations.formatOnSave) and want diff-friendly member reordering.Alternatives to Evaluate:
| Tool | Strengths | Weaknesses |
|---|---|---|
| PHP-CS-Fixer | Extensive rules, active community | Slower for large files, rigid config |
| ECS | Plugin ecosystem, rule-based | Steeper learning curve |
| Prettier PHP | Fast, opinionated | Less PHP-native, fewer rules |
| PHP_Beautifier | Lightweight | Outdated, limited features |
"We’re upgrading to PHP-Styler 0.20.0 to standardize class structures and accelerate code reviews. Key benefits:
use statements before constants).*"PHP-Styler 0.20.0 elevates class structure standardization with:
NormalizeMemberOrder: Enforce canonical sequences (e.g., trait → constant → property) with configurable overrides.__construct, __toString, etc., via TMagicMethod tokens.NestingStack refactoring.--force ensures deterministic formatting—no cached outputs, only fresh scans.
Trade-off: Less granular control than PHP-CS-Fixer, but 90% of our needs are covered with minimal maintenance."**"This update automates class organization and fixes edge cases:
use statements first) with configurable rules.__destruct) now format correctly—no more manual fixes.formatOnSave for real-time consistency.public).
Try it: Run composer require pmjones/php-styler:^0.20.0 and ./vendor/bin/php-styler apply src/ to see your classes automatically organized!"*How can I help you explore Laravel packages today?