etsglobal/php-static-analysis

Static analysis tools configuration for PHP projects

Installs: 17 940

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 15

Forks: 1

Open Issues: 0

Type:phpcodesniffer-standard

v3.1.0 2024-04-10 09:58 UTC

README

This repository contains the base configuration for static analysis tools used by ETSGlobal PHP applications.

It includes:

  • PHP_CodeSniffer configuration and custom sniffs
  • PHP Mess Detector configuration
  • PHPStan configuration

Installation

Using composer:

composer require etsglobal/php-static-analysis --dev

It will install squizlabs/php_codesniffer by default, but you may install other optional tools manually:

composer require phpmd/phpmd phpstan/phpstan --dev

Usage

PHP_CodeSniffer

In your project root directory, add a phpcs.xml file with the following contents:

<?xml version="1.0"?>
<ruleset name="ETSGlobal Coding Standard" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="../../vendor/squizlabs/php_codesniffer/phpcs.xsd">
    <rule ref="./vendor/etsglobal/php-static-analysis/lib/phpcs/phpcs.xml" />
</ruleset>

Then, run it with:

vendor/bin/phpcs src

If needed, you can exclude some rules by adding exceptions to your phpcs.xml:

    ...
    <rule ref="SlevomatCodingStandard.Functions.TrailingCommaInCall">
        <!-- Ignore missing trailing commas in multiline function calls -->
        <exclude name="SlevomatCodingStandard.Functions.TrailingCommaInCall.MissingTrailingComma"/>
    </rule>
    ...

PHP Mess Detector

First, make sure you have the phpmd/phpmd package installed:

composer require phpmd/phpmd --dev

Then, in your project root directory, add a phpmd.xml file with the following contents:

<?xml version="1.0"?>
<ruleset name="ETSGlobal ruleset"
         xmlns="http://pmd.sf.net/ruleset/1.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
         xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
    <description>
        ETSGlobal ruleset
    </description>

    <rule ref="./vendor/etsglobal/php-static-analysis/lib/phpmd/phpmd.xml"/>
</ruleset>

Now run phpmd:

vendor/bin/phpmd src text phpmd.xml

PHPStan

First, make sure you have the phpstan/phpstan package installed:

composer require phpstan/phpstan --dev

Then, in your project root directory, add a phpstan.neon file with the following contents:

includes:
    - vendor/etsglobal/php-static-analysis/lib/phpstan/phpstan.neon

You can now run phpstan:

vendor/bin/phpstan analyse --level=max src