Mastering Code Quality in Laravel: Pint with Git Hooks and Docker
(image by
Introduction
As developers, we all know how important clean, consistent code is for maintaining a large-scale Laravel application. Not only does well-formatted code boost readability, but it also ensures team-wide consistency and helps avoid pesky formatting debates during code reviews.
Enter into your Laravel application and take it a step further by running Pint inside a Docker container using Docker Compose.
This approach ensures that code formatting is consistent across environments and makes it easier for developers using different setups to maintain uniformity in their codebase.
Let's dive into the details! 🚀
, which is a tool that makes it easy to manage Git hooks in npm-based projects. Here's how to set it up in your Laravel project:
Install Husky: First, install Husky as a dev dependency:
npm install husky --save-dev
Set up Husky: Add the following script to yourpackage.jsonto automatically set up Husky duringnpm install:
{
"scripts": {
"prepare": "husky install"
}
}
Then, run npm install to initialize Husky:
npm install
Create a Pre-commit Hook: Set up a Git pre-commit hook that runs Pint before every commit:
npx husky add .husky/pre-commit "vendor/bin/pint"
chmod +x .husky/pre-commit
This command creates a pre-commit hook in the .husky directory that ensures Pint runs whenever a developer attempts to commit code.
Using Pint Inside Docker
In a modern development environment, many teams rely on Docker to ensure consistency across different machines. Running Pint inside Docker ensures that your development environment is identical, regardless of your operating system or local setup.
Step 4: Setting Up Docker for Pint
You can run Pint inside a Docker container using Docker Compose. Here's how to set it up:
- First, add a
docker-compose.ymlfile to your project if you don’t have one. Here’s an example configuration for a typical Laravel app:
version: '3.8'
services:
app:
image: php:8.1-cli
volumes:
- .:/var/www
- ./vendor:/var/www/vendor
working_dir: /var/www
command: bash -c "while true; do sleep 1; done"
- Modify your
pre-commithook to run Pint inside the Docker container. Open the.husky/pre-commitfile and update it as follows:
# Add docker compose to PATH if needed
export PATH=$PATH:/usr/local/bin # Adjust this path as needed
if [ "$USE_DOCKER" = "true" ]; then
echo "Running Pint in Docker..."
docker compose exec -T app vendor/bin/pint
else
echo "Running Pint locally..."
vendor/bin/pint
fi
git add .
This script checks if the USE_DOCKER environment variable (in your Laravel .env file) is set to "true". If it is, it runs Pint inside the Docker container. Otherwise, it runs Pint locally.
This setup gives flexibility to your team, as developers can choose whether they want to run Pint locally or in Docker.
Step 5: Running Docker Compose
To run Pint in Docker, make sure you have your containers up and running. You can start Docker Compose with:
docker compose up -d
Bonus: Best Practices for Running Pint in Laravel Applications
Here are a few tips and guidelines for fellow developers working with Pint and Docker:
Use Pre-commit Hooks: Automate Pint formatting with pre-commit hooks to enforce consistency.
Use Docker for Consistency: By running Pint inside a Docker container, you ensure that all team members have the same environment.
Customize Pint: Laravel Pint comes pre-configured, but you can customize its rules by adding a pint.json configuration file if you need to enforce specific styles.
Keep Docker Lightweight: In yourdocker-compose.ymlfile, mount only the necessary volumes (such as the application andvendordirectories) to avoid performance issues.
Conclusion
Using Pint in your Laravel application is a simple yet powerful way to ensure consistent code quality and formatting.
By integrating it with Git hooks, Husky, and Docker, you can automate this process, making it part of your daily development workflow. Whether you run Pint locally or inside a Docker container, it can save time, prevent common formatting errors, and make your codebase cleaner and more maintainable!
SOCIAL SHARE CARD GENERATOR