Closed dotherightthing closed 6 years ago
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
$ ./vendor/bin/phpcs --extensions=php --ignore=/docs/,/node_modules/,/vendor/ --standard=WordPress-VIP .
WordPress-VIP is the expensive serviced WordPress hosting.
// phpcs:disable
...
// phpcs:enable
By default, PHP_CodeSniffer will check any file it finds with a .inc, .php, .js or .css extension,
Limit checks to .php
files:
--extensions=php
--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/.
http://www.thisprogrammingthing.com/2015/creating-your-own-standard-in-phpcs/
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'));
});
require-dev
Composer packages and runs the gulp php_codesniffer
task.// SublimeLinter Settings - User
{
"linters": {
"phpcs": {
"standard": "WordPress-VIP"
}
}
}
// phpcs:disable
...
// phpcs:enable
By default, PHP_CodeSniffer will check any file it finds with a .inc, .php, .js or .css extension,
Limit checks to .php
files:
--extensions=php
--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/.
http://www.thisprogrammingthing.com/2015/creating-your-own-standard-in-phpcs/
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.
TGMPA also clashes with DealerDirect/phpcodesniffer-composer-installer
To resolve:
composer.json
vendor
foldercomposer 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
Updated instructions:
https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/wiki/Linting
https://www.silverstripe.org/blog/open-source-converting-a-silverstripe-codebase-to-psr-2/