Consent

This site uses third party services that need your consent.

Skip to content
Steven Roland

Setting Up Laravel Pint in a Non-Laravel Project

Laravel Pint is a powerful tool designed to ensure consistent coding style across your PHP projects. While it is tailored for Laravel applications, it can be easily integrated into any PHP project. This blog post will guide you through the process of setting up Laravel Pint in a non-Laravel project, allowing you to leverage its capabilities to maintain clean and standardized code.

What is Laravel Pint?

Laravel Pint is a code style fixer that acts as a wrapper around PHP-CS-Fixer. It simplifies the process of enforcing a consistent coding style by providing a set of predefined rules, particularly those aligned with the Laravel framework. However, its utility extends beyond Laravel projects, making it a versatile tool for any PHP developer.

Step-by-Step Guide to Setting Up Laravel Pint

1. Install Composer

Before you can install Laravel Pint, ensure that you have Composer installed on your system. Composer is a dependency manager for PHP, and you can download it from the official Composer website.

2. Create a PHP Project

If you don't already have a PHP project, create a new directory for your project and navigate into it:

mkdir my-php-project
cd my-php-project

3. Initialize Composer

Initialize Composer in your project directory to create a composer.json file:

composer init

Follow the prompts to set up your project details.

4. Install Laravel Pint

With Composer set up, you can now install Laravel Pint as a development dependency:

composer require laravel/pint --dev

This command will add Laravel Pint to your composer.json file and download it into the vendor directory.

5. Run Laravel Pint

You can now use Laravel Pint to check and fix code style issues. Run the following command to analyze your project's code:

./vendor/bin/pint

This command will scan your project and automatically fix any code style issues it finds. You can specify particular files or directories if you prefer:

./vendor/bin/pint src/
./vendor/bin/pint src/Example.php

6. Customize Pint Configuration

While Laravel Pint works out-of-the-box with its default settings, you can customize its behavior by creating a pint.json configuration file in your project's root directory. This file allows you to define custom rules or presets. Here's an example configuration:

{
    "preset": "psr12",
    "exclude": [
        "vendor"
    ]
}

This configuration sets the coding standard to PSR-12 and excludes the vendor directory from being scanned.

7. Automate Pint with Git Hooks

To ensure code consistency, you can automate Laravel Pint using Git hooks. For example, you can run Pint before each commit by adding the following script to your .git/hooks/pre-commit file:

#!/bin/sh
./vendor/bin/pint --test

Make sure to give the script execution permissions:

chmod +x .git/hooks/pre-commit

Conclusion

Laravel Pint is an excellent tool for maintaining a consistent coding style in any PHP project, not just Laravel applications. By following the steps outlined in this guide, you can easily set up Laravel Pint in your non-Laravel projects and enjoy the benefits of clean, standardized code. Whether you're working on a small personal project or a large enterprise application, Laravel Pint can help you keep your codebase neat and professional.

More posts

Understanding the EOS Methodology

The EOS methodology streamlines web development by aligning team vision, clarifying roles, using data-driven decisions, optimizing processes, and enhancing accountability, leading to efficient project execution and improved collaboration.