PHP_CodeSniffer detects violations of a defined set of coding standards.
You can use it to prepare for PHP version upgrades. There is a set of sniffs that checks for PHP cross-version compatibility which allows you to analyze your code for compatibility with higher and lower versions of PHP.
To check your Drupal codebase, first clone it into a clean directory, and check out the correct branch (e.g. git checkout develop
).
Create a directory on your computer e.g. /Users/mark.felton/Tools
and in there download the latest library for PHPCompatitbiliy, https://github.com/PHPCompatibility/PHPCompatibility/releases
wget https://github.com/PHPCompatibility/PHPCompatibility/archive/9.3.5.zip
unzip 9.3.5.zip
Then run composer update
to install the /vendor/
dependencies...
Now see if phpcs works...
$ /Users/mark.felton/Tools/PHPCompatibility-9.3.5/vendor/squizlabs/php_codesniffer/bin/phpcs
ERROR: You must supply at least one file or directory to process.
Run "phpcs --help" for usage information
Great, it works!!
You'll need to specify the --config-set installed_paths
$ /Users/mark.felton/Tools/PHPCompatibility-9.3.5/vendor/squizlabs/php_codesniffer/bin/phpcs --config-set installed_paths /Users/mark.felton/Tools/PHPCompatibility-9.3.5/PHPCompatibility/
You should see:
$ /Users/mark.felton/Tools/PHPCompatibility-9.3.5/vendor/squizlabs/php_codesniffer/bin/phpcs --config-set installed_paths /Users/mark.felton/Tools/PHPCompatibility-9.3.5/PHPCompatibility/
Using config file: /Users/mark.felton/Tools/PHPCompatibility-9.3.5/vendor/squizlabs/php_codesniffer/CodeSniffer.conf
Config value "installed_paths" updated successfully; old value was ...
Now run the tool with the --standard=PHPCompatibility
flag:
$ /Users/mark.felton/Tools/PHPCompatibility-9.3.5/vendor/squizlabs/php_codesniffer/bin/phpcs /Users/mark.felton/Sites/my-project-folder/docroot/ --standard=PHPCompatibility --runtime-set testVersion 7.3 --report-full=/Users/mark.felton/Sites/my-project-folder/docroot/my-report.txt
It might take 20 mins or more to scan a project.
You can optionally included -n
to omit WARNINGS and only show ERRORS.
-
Check for phpcs alias conflicts in your $PATH or
.bash_profile
-
Check your system's php (these days it should be 7.2 or 7.3...)
$ php --version
PHP 7.1.30 (cli) (built: Jun 1 2019 00:27:45) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.1.30, Copyright (c) 1999-2018, by Zend Technologies
$ php --ini
Configuration File (php.ini) Path: /usr/local/etc/php/7.1
Loaded Configuration File: /usr/local/etc/php/7.1/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.1/conf.d
Additional .ini files parsed: /usr/local/etc/php/7.1/conf.d/ext-opcache.ini
Set the memory_limit
in the Loaded Configuration File to -1
if you need to...
Another resource: https://blog.fortrabbit.com/php-testing