PHP Insights

repository·master·Indexed 26 days ago

https://github.com/nunomaduro/phpinsights

A tool for simplifying code quality analysis for PHP projects directly from the terminal. It analyzes code quality, coding style, architecture, and complexity. It supports presets for Laravel, Symfony, Magento2, and Drupal, and can be integrated into CI pipelines via GitHub Actions and GitLab Code Quality.

Tokens
15K
Snippets
75
Records
100
Agent score
90%

What's inside phpinsights

  1. Understand Code Metric Categories

    master

    PHP Insights organizes its code analysis into several distinct metric domains. When reviewing reports or configuring specific rules, you can categorize them by these domains:

    • NunoMaduro\PhpInsights\Domain\Metrics\Code\Classes: Rules related to class structure and properties.
    • NunoMaduro\PhpInsights\Domain\Metrics\Code\Code: Rules related to general code syntax, logic, and formatting.
    • NunoMaduro\PhpInsights\Domain\Metrics\Code\Comments: Rules related to documentation and comments.
    • NunoMaduro\PhpInsights\Domain\Metrics\Code\Functions: Rules related to function and method usage.
    • NunoMaduro\PhpInsights\Domain\Metrics\Code\Globally: Rules applied globally across the codebase.
  2. Create a phpinsights.php configuration file

    master

    Before configuring PHP Insights, create a phpinsights.php file in your project root. The file should return an array with the following structure:

    <?php
    
    declare(strict_types=1);
    
    return [
        'preset' => 'default',
        'exclude' => [
            // 'path/to/directory-or-file'
        ],
        'add' => [
            // ExampleMetric::class => [
            //     ExampleInsight::class,
            // ]
        ],
        'remove' => [
            // ExampleInsight::class,
        ],
        'config' => [
            // ExampleInsight::class => [
            //     'key' => 'value',
            // ],
        ],
    ];
    <?php
    
    declare(strict_types=1);
    
    return [
        'preset' => 'default',
        'exclude' => [
            //  'path/to/directory-or-file'
        ],
        'add' => [
            //  ExampleMetric::class => [
            //      ExampleInsight::class,
            //  ]
        ],
        'remove' => [
            //  ExampleInsight::class,
        ],
        'config' => [
            //  ExampleInsight::class => [
            //      'key' => 'value',
            //  ],
        ],
    ];
  3. Integrate with GitLab Code Quality

    master

    To use PHP Insights with GitLab Code Quality, use the --format=codeclimate option and redirect the output to a JSON file. You must then define this file as a codequality report artifact in your .gitlab-ci.yml.

    #.gitlab-ci.yml
    insights:
      script:
        - vendor/bin/phpinsights -n --ansi --format=codeclimate > codeclimate-report.json
      artifacts:
        reports:
          codequality: codeclimate-report.json
  4. Configure IDE integration for clickable file links

    master

    Since version 1.10, PhpInsights can include clickable file links in its output. To enable this, add the ide key to your phpinsights.php configuration file with the name of your editor or a custom URL template.

    Supported IDE values:

    • phpstorm
    • sublime
    • textmate
    • macvim
    • emacs
    • atom
    • vscode
    <?php
    
    return [
        // ...
        'ide' => 'vscode',
        // ...
    ];
  5. Add a PHP CS Sniff as an Insight

    master

    To include an existing PHP CS Sniff as an insight, identify the appropriate metric class in src/Domain/Metrics and add the sniff's class name to its getInsights() array.

    final class Classes implements HasInsights
    {
        public function getInsights(): array
        {
            return [
                UnusedPropertySniff::class,
            ];
        }
    }