Mastering Code Quality in Laravel with PHP Insights: A Beginner’s Guide

Manish Kumar, Owner @Pixospace
2 min readJan 9, 2024

--

PHP Insights is a tool that enhances the reliability and cleanliness of your code. It’s especially key for Laravel projects, offering built-in checks that align with best coding practices. PHP Insights isn’t limited to Laravel, though; it extends its benefits to frameworks like Symfony and WordPress. This versatility makes it an invaluable asset beyond Laravel-focused development.

What is PHP Insights in Laravel?

In simple terms, PHP Insights is like a health check for your Laravel code. It scrutinizes your codebase, identifying areas for improvement to ensure your code is up to industry standards. Think of it as a mentor that guides you towards writing better, more efficient Laravel code.

Implementing PHP Insights in Laravel with Examples

To use PHP Insights, start by installing it using Composer:

composer require nunomaduro/phpinsights --dev

Then, publish the package’s config file:

php artisan vendor:publish --provider="NunoMaduro\PhpInsights\Application\Adapters\Laravel\InsightsServiceProvider"

Run an analysis with:

php artisan insights

or

# Mac & Linux
./vendor/bin/phpinsights

This command inspects your entire codebase.

Advanced Usage

  1. Analyzing a Specific Directory: For example, to analyze only the Controllers directory:
php artisan insights app/Http/Controllers

2. Analyzing a Specific File: To analyze a specific controller:

php artisan insights app/Http/Controllers/UserController.php

3. Automatically Fixing Issues:

php artisan insights --fix

This command will attempt to automatically fix the issues found by PHP Insights. Remember, while auto-fixes are helpful, manually reviewing the suggested changes is best practice.

Configuration Examples

Excluding Folders or Files: In the phpinsights.php config file:

'exclude' => [
'path/to/excluded/directory',
'path/to/excluded/file.php',
],

Customizing Insights: To customize the insights used, edit the phpinsights.php config file. For example, to add or remove insights:

'add' => [
// List of additional insights classes to include
],
'remove' => [
// List of insights classes to exclude
],

Configuring Categories: You can also configure the various categories like Code, Architecture, Complexity, and Style in the phpinsights.php file. For each category, you can specify which insights to apply or exclude.

'config' => [
\NunoMaduro\PhpInsights\Domain\Insights\ForbiddenDefineFunctions::class => [
'exclude' => ['some_global_function'],
],
// Other category configurations...
],

Benefits of PHP Insights

PHP Insights helps maintain code quality, ensuring your Laravel projects are not only functional but also clean and efficient. By regularly using PHP Insights, you’ll foster good coding habits, reduce technical debt, and enhance project maintainability.

Conclusion

PHP Insights is a vital tool for Laravel developers, offering insights into improving code quality. By embracing its recommendations, you can elevate the standard of your Laravel projects. We encourage you to integrate these practices and share your experiences. Feedback and comments are always welcome!

--

--

Manish Kumar, Owner @Pixospace
Manish Kumar, Owner @Pixospace

Written by Manish Kumar, Owner @Pixospace

12 yrs as a PHP dev, 7 in Laravel. Now a full-time freelancer writing about coding, freelancing, and tech fixes on Medium.

Responses (1)