Home

Awesome

PHP_CodeSniffer VariableAnalysis

CircleCI

Plugin for PHP_CodeSniffer static analysis tool that adds analysis of problematic variable use.

Please note that this README is for VariableAnalysis v3. For documentation about v2, see this page.

Installation

Requirements

VariableAnalysis requires PHP 5.4 or higher and PHP CodeSniffer version 3.5.0 or higher.

It also requires PHPCSUtils which must be installed as a PHPCS standard. If you are using composer, this will be done automatically (see below).

With PHPCS Composer Installer

This is the easiest method.

First, install phpcodesniffer-composer-installer for your project if you have not already. This will also install PHPCS.

composer require --dev dealerdirect/phpcodesniffer-composer-installer

Then install these standards.

composer require --dev sirbrillig/phpcs-variable-analysis

You can then include the sniffs by adding a line like the following to your phpcs.xml file.

<rule ref="VariableAnalysis"/>

It should just work after that!

Standalone

  1. Install PHP_CodeSniffer (PHPCS) by following its installation instructions (via Composer, Phar file, PEAR, or Git checkout).

    Do ensure that PHP_CodeSniffer's version matches our requirements.

  2. Install PHPCSUtils (required by this sniff). Download either the zip or tar.gz file from the PHPCSUtils latest release page. Expand the file and rename the resulting directory to phpcsutils. Move the directory to a place where you'd like to keep all your PHPCS standards.

  3. Install VariableAnalysis. Download either the zip or tar.gz file from the VariableAnalysis latest release page. Expand the file and rename the resulting directory to phpcs-variable-analysis. Move the directory to a place where you'd like to keep all your PHPCS standards.

  4. Add the paths of the newly installed standards to the PHP_CodeSniffer installed_paths configuration. The following command should append the new standards to your existing standards (be sure to supply the actual paths to the directories you created above).

     phpcs --config-set installed_paths "$(phpcs --config-show|grep installed_paths|awk '{ print $2 }'),/path/to/phpcsutils,/path/to/phpcs-variable-analysis"
    

    If you do not have any other standards installed, you can do this more easily (again, be sure to supply the actual paths):

     phpcs --config-set installed_paths /path/to/phpcsutils,/path/to/phpcs-variable-analysis
    

Customization

There's a variety of options to customize the behaviour of VariableAnalysis, take a look at the included ruleset.xml.example for commented examples of a configuration.

The available options are as follows:

To set these these options, you must use XML in your ruleset. For details, see the phpcs customizable sniff properties page. Here is an example that ignores all variables that start with an underscore:

<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis">
    <properties>
        <property name="ignoreUnusedRegexp" value="/^_/"/>
    </properties>
</rule>

See Also

Original

This was forked from the excellent work in https://github.com/illusori/PHP_Codesniffer-VariableAnalysis

Contributing

Please open issues or PRs on this repository.

Any changes should be accompanied by tests and should pass linting and static analysis. Please use phpdoc (rather than actual types) for declaring types since this must run in PHP 5.4.

To run tests, make sure composer is installed, then run:

composer install # you only need to do this once
composer test

To run linting, use:

composer lint

To run static analysis, use:

composer phpstan