dotherightthing / wpdtrt-plugin-boilerplate

Boilerplate for WordPress plugin development. Formerly named wpdtrt-plugin.
2 stars 2 forks source link

Add PHP linting (PSR-2) #89

Closed dotherightthing closed 6 years ago

dotherightthing commented 6 years ago

https://www.silverstripe.org/blog/open-source-converting-a-silverstripe-codebase-to-psr-2/

dotherightthing commented 6 years ago

Installation via Composer

First, you don't need to require PHP CodeSniffer explicitly, because wp-coding-standards/wpcs pulls it automatically. Source: PHP CodeSniffer WordPress via Composer

composer require wp-coding-standards/wpcs --dev 

This project is a collection of PHP_CodeSniffer rules (sniffs) to validate code developed for WordPress. It ensures code quality and adherence to coding conventions, especially the official WordPress Coding Standards. Source: WordPress-Coding-Standards

Sniffs are in the form of ruleset.xml files.

There are two actively maintained Composer plugins which can handle the registration of standards with PHP_CodeSniffer for you: ... It is strongly suggested to require one of these plugins in your project to handle the registration of external standards with PHPCS for you. Source: WordPress-Coding-Standards - Installation

composer require DealerDirect/phpcodesniffer-composer-installer --dev

You should then see WordPress-Core et al listed when you run phpcs -i. Source: WordPress-Coding-Standards - Installation

$ vendor/bin/phpcs -i                                                                                                       15:57:02  ☁  
The installed coding standards are MySource, PEAR, PSR1, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress-VIP

Running PHP Code Sniffer

Terminal

$ ./vendor/bin/phpcs --extensions=php --ignore=/docs/,/node_modules/,/vendor/ --standard=WordPress-VIP .

WordPress-VIP is the expensive serviced WordPress hosting.

Ignoring Parts of a File

// phpcs:disable
...
// phpcs:enable

Specifying Valid File Extensions

By default, PHP_CodeSniffer will check any file it finds with a .inc, .php, .js or .css extension,

Limit checks to .phpfiles:

--extensions=php

Ignoring Files and Folders

--ignore=/docs/,/node_modules/,/vendor/

This is crucial to avoid fatal errors:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in /Volumes/DanBackup/Websites/wpdtrt-plugin/vendor/squizlabs/php_codesniffer/src/Tokenizers/Tokenizer.php on line 1298
[16:49:50] 'phpcs' errored after 9.82 s

See also https://tommcfarlin.com/exclude-files-from-php-codesniffer/.

Excluding rule sets

http://www.thisprogrammingthing.com/2015/creating-your-own-standard-in-phpcs/

Gulp

yarn add 'gulp-phpcs' --dev
var phpcs = require('gulp-phpcs');

gulp.task('php_codesniffer', function () {

    taskheader(this);

    return gulp.src(['**/*.php', '!docs/**/*.php', '!node_modules/**/*.php', '!vendor/**/*.php'])
        // Validate files using PHP Code Sniffer
        .pipe(phpcs({
            bin: 'vendor/bin/phpcs',
            standard: 'WordPress-VIP', // 'PSR2'
            warningSeverity: 0
        }))
        // Log all problems that were found
        .pipe(phpcs.reporter('log'));
});

Travis

Sublime

// SublimeLinter Settings - User
{
    "linters": {
        "phpcs": {
            "standard": "WordPress-VIP"
        }
    }
}
dotherightthing commented 6 years ago

Ignoring Parts of a File

// phpcs:disable
...
// phpcs:enable

Specifying Valid File Extensions

By default, PHP_CodeSniffer will check any file it finds with a .inc, .php, .js or .css extension,

Limit checks to .phpfiles:

--extensions=php

Ignoring Files and Folders

--ignore=/docs/,/node_modules/,/vendor/

This is crucial to avoid fatal errors:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in /Volumes/DanBackup/Websites/wpdtrt-plugin/vendor/squizlabs/php_codesniffer/src/Tokenizers/Tokenizer.php on line 1298
[16:49:50] 'phpcs' errored after 9.82 s

See also https://tommcfarlin.com/exclude-files-from-php-codesniffer/.

Excluding rule sets

http://www.thisprogrammingthing.com/2015/creating-your-own-standard-in-phpcs/

dotherightthing commented 6 years ago

If SublimeLint does not appear to be working, it may be failing.

View > Show Console

SublimeLinter: WARNING: phpcs cannot locate 'phpcs'
WARNING:SublimeLinter.lint.base_linter.composer_linter:phpcs cannot locate 'phpcs'
SublimeLinter: WARNING: phplint cannot locate 'phpl'
WARNING:SublimeLinter.lint.linter:phplint cannot locate 'phpl'

The issue here is that PHPCS is not installed globally. Sowpdtrt-plugin will need to have these Composer dependencies installed, before they will be available to a child plugin, and thence to Sublime when it is editing that child's code.

For now, adding these dependencies to a child's require-dev is one option.

dotherightthing commented 6 years ago

TGMPA also clashes with DealerDirect/phpcodesniffer-composer-installer

To resolve:

  1. Remove TGMPA from composer.json
  2. Delete the vendor folder
  3. composer install

Sublime's PHPCS linter will automatically start working.

See similar issue, https://github.com/dotherightthing/wpdtrt-plugin/wiki/Troubleshooting:-TGMPA-causes-gulp-install-task-to-fail#solution

dotherightthing commented 6 years ago

Updated instructions:

https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/wiki/Linting