How to Use 'Pint' for PHP Code Style Fixing (with Examples)
Pint is an opinionated PHP code style fixer developed as part of the Laravel ecosystem. It is based on PHP-CS-Fixer and offers a streamlined way to ensure PHP code adheres to consistent styling practices. Pint allows developers to automate code formatting, making it easier to maintain readability and reduce manual code review efforts. It supports various configurations and presets, enabling customization to different coding standards.
Use Case 1: Execute Code Style Fixing
Code:
pint
Motivation: Running the pint
command without any additional options quickly and automatically applies styling fixes across your PHP codebase. This is particularly useful for developers who want a fast way to ensure their code conforms to a specified style without manually reviewing and editing each file.
Explanation: The pint
command, by default, analyzes all PHP files in the current directory and its subdirectories. It checks for any discrepancies against the default or previously specified coding standards, rectifying issues such as improper spacing, incorrect indentation, or inconsistent line breaks.
Example Output:
Pint 1.0.0 by Laravel
Fixed files
1) src/ExampleClass.php
2) tests/ExampleTest.php
All tasks completed in 1.254s
Use Case 2: Display All Files That Are Changed
Code:
pint -v
Motivation: By using the verbose option (-v
), this command provides users with detailed feedback about the changes being made during the code fix process. This is valuable for developers who need to verify the changes that Pint is making, ensuring that formatting choices align with team or project standards before they are accepted into the codebase.
Explanation: The -v
(verbose) flag enables the command to output more detailed information. Instead of only showing fixed files, it lists every file checked and what changes (if any) were applied to each. This aids in transparency and helps developers understand how their code is being altered by the fixer.
Example Output:
Pint 1.0.0 by Laravel
Checking src/ExampleClass.php
- [CHANGED] Indentation fixed
Checking tests/ExampleTest.php
- [UNCHANGED]
All tasks completed in 1.678s
Use Case 3: Execute Code Style Linting Without Applying Changes
Code:
pint --test
Motivation: The --test
option allows developers to perform a dry run of the Pint command, identifying style violations without applying any changes. This is particularly useful in a continuous integration setup, where you want to ensure code quality without modifying the code automatically during the build or test processes.
Explanation: The --test
argument instructs Pint to check the PHP files against the style rules but not to change them. This non-intrusive check is crucial for identifying areas for improvement while maintaining full manual control over what gets altered.
Example Output:
Pint 1.0.0 by Laravel
Testing src/ExampleClass.php
- [ISSUES DETECTED] Indentation
Testing tests/ExampleTest.php
- [CLEAN]
Some tasks found issues in your code!
Use Case 4: Execute Code Style Fixes Using a Specific Configuration File
Code:
pint --config path/to/pint.json
Motivation: Using a custom configuration file is ideal for teams with specific coding standards that differ from the default ruleset. This allows development teams to define a tailored set of rules that align with their specific project or organizational guidelines.
Explanation: The --config
option lets developers specify a path to a JSON configuration file (pint.json
) that defines the styling rules Pint should use. This customization capability ensures your project adheres to specified conventions, enhancing maintainability and cohesion throughout the codebase.
Example Output:
Pint 1.0.0 by Laravel
Using configuration from path/to/pint.json
Fixed files
1) app/Http/Controllers/Controller.php
2) resources/views/view.blade.php
All tasks completed in 2.416s
Use Case 5: Execute Code Style Fixes Using a Specific Preset
Code:
pint --preset psr12
Motivation: This command enables developers to apply predefined coding standards, such as the PSR-12 standard, which is widely recognized across PHP projects. It’s an excellent option for developers seeking broader compliance and who might not need or want detailed custom configurations.
Explanation: The --preset
flag specifies an existing code style preset. In this case, psr12
is utilized, which adheres to the PHP Standards Recommendations made for clean, consistent coding practices. This preset ensures the code matches a commonly adopted style guide.
Example Output:
Pint 1.0.0 by Laravel
Applying preset: PSR-12
Fixed files
1) src/Service/Service.php
All tasks completed in 1.149s
Conclusion
Pint provides developers with a powerful tool to automate code styling, ensuring consistent and readable PHP code. Whether using default settings, custom configurations, or recognized style presets, Pint efficiently applies style rules across a codebase, supporting both individual developers and collaborative teams in maintaining high code quality. With options for dry runs and detailed reporting, it integrates seamlessly into a variety of development workflows.