Add PHPCS

This commit is contained in:
Ben Dwyer 2020-09-25 20:09:40 +01:00
parent adddd944a1
commit d905406a00
6 changed files with 635 additions and 1 deletions

2
.gitignore vendored
View file

@ -2,4 +2,6 @@ node_modules
twentynineteen/
theme-dev-utils/
theme-dev-utils
vendor/
*.DS_Store

15
composer.json Normal file
View file

@ -0,0 +1,15 @@
{
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
"phpcompatibility/phpcompatibility-wp": "2.1.0",
"sirbrillig/phpcs-changed": "^2.5.1",
"sirbrillig/phpcs-variable-analysis": "^2.7.0",
"wp-coding-standards/wpcs": "^2.1.1"
},
"scripts": {
"php:lint": "vendor/bin/phpcs -p -s",
"php:lint:errors": "vendor/bin/phpcs -p -s --runtime-set ignore_warnings_on_exit 1",
"php:lint:autofix": "vendor/bin/phpcbf",
"php:lint:changed": "vendor/bin/phpcs-changed --git --git-unstaged"
}
}

443
composer.lock generated Normal file
View file

@ -0,0 +1,443 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b6f63ee6bbf9e80dcf77f67f5bd61c45",
"packages": [],
"packages-dev": [
{
"name": "dealerdirect/phpcodesniffer-composer-installer",
"version": "v0.5.0",
"source": {
"type": "git",
"url": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer.git",
"reference": "e749410375ff6fb7a040a68878c656c2e610b132"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/e749410375ff6fb7a040a68878c656c2e610b132",
"reference": "e749410375ff6fb7a040a68878c656c2e610b132",
"shasum": ""
},
"require": {
"composer-plugin-api": "^1.0",
"php": "^5.3|^7",
"squizlabs/php_codesniffer": "^2|^3"
},
"require-dev": {
"composer/composer": "*",
"phpcompatibility/php-compatibility": "^9.0",
"sensiolabs/security-checker": "^4.1.0"
},
"type": "composer-plugin",
"extra": {
"class": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin"
},
"autoload": {
"psr-4": {
"Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Franck Nijhof",
"email": "franck.nijhof@dealerdirect.com",
"homepage": "http://www.frenck.nl",
"role": "Developer / IT Manager"
}
],
"description": "PHP_CodeSniffer Standards Composer Installer Plugin",
"homepage": "http://www.dealerdirect.com",
"keywords": [
"PHPCodeSniffer",
"PHP_CodeSniffer",
"code quality",
"codesniffer",
"composer",
"installer",
"phpcs",
"plugin",
"qa",
"quality",
"standard",
"standards",
"style guide",
"stylecheck",
"tests"
],
"time": "2018-10-26T13:21:45+00:00"
},
{
"name": "phpcompatibility/php-compatibility",
"version": "9.3.5",
"source": {
"type": "git",
"url": "https://github.com/PHPCompatibility/PHPCompatibility.git",
"reference": "9fb324479acf6f39452e0655d2429cc0d3914243"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/9fb324479acf6f39452e0655d2429cc0d3914243",
"reference": "9fb324479acf6f39452e0655d2429cc0d3914243",
"shasum": ""
},
"require": {
"php": ">=5.3",
"squizlabs/php_codesniffer": "^2.3 || ^3.0.2"
},
"conflict": {
"squizlabs/php_codesniffer": "2.6.2"
},
"require-dev": {
"phpunit/phpunit": "~4.5 || ^5.0 || ^6.0 || ^7.0"
},
"suggest": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.5 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.",
"roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues."
},
"type": "phpcodesniffer-standard",
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-3.0-or-later"
],
"authors": [
{
"name": "Wim Godden",
"homepage": "https://github.com/wimg",
"role": "lead"
},
{
"name": "Juliette Reinders Folmer",
"homepage": "https://github.com/jrfnl",
"role": "lead"
},
{
"name": "Contributors",
"homepage": "https://github.com/PHPCompatibility/PHPCompatibility/graphs/contributors"
}
],
"description": "A set of sniffs for PHP_CodeSniffer that checks for PHP cross-version compatibility.",
"homepage": "http://techblog.wimgodden.be/tag/codesniffer/",
"keywords": [
"compatibility",
"phpcs",
"standards"
],
"time": "2019-12-27T09:44:58+00:00"
},
{
"name": "phpcompatibility/phpcompatibility-paragonie",
"version": "1.3.0",
"source": {
"type": "git",
"url": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie.git",
"reference": "b862bc32f7e860d0b164b199bd995e690b4b191c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityParagonie/zipball/b862bc32f7e860d0b164b199bd995e690b4b191c",
"reference": "b862bc32f7e860d0b164b199bd995e690b4b191c",
"shasum": ""
},
"require": {
"phpcompatibility/php-compatibility": "^9.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.5",
"paragonie/random_compat": "dev-master",
"paragonie/sodium_compat": "dev-master"
},
"suggest": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.5 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.",
"roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues."
},
"type": "phpcodesniffer-standard",
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-3.0-or-later"
],
"authors": [
{
"name": "Wim Godden",
"role": "lead"
},
{
"name": "Juliette Reinders Folmer",
"role": "lead"
}
],
"description": "A set of rulesets for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by the Paragonie polyfill libraries.",
"homepage": "http://phpcompatibility.com/",
"keywords": [
"compatibility",
"paragonie",
"phpcs",
"polyfill",
"standards"
],
"time": "2019-11-04T15:17:54+00:00"
},
{
"name": "phpcompatibility/phpcompatibility-wp",
"version": "2.1.0",
"source": {
"type": "git",
"url": "https://github.com/PHPCompatibility/PHPCompatibilityWP.git",
"reference": "41bef18ba688af638b7310666db28e1ea9158b2f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityWP/zipball/41bef18ba688af638b7310666db28e1ea9158b2f",
"reference": "41bef18ba688af638b7310666db28e1ea9158b2f",
"shasum": ""
},
"require": {
"phpcompatibility/php-compatibility": "^9.0",
"phpcompatibility/phpcompatibility-paragonie": "^1.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.5"
},
"suggest": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.5 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.",
"roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues."
},
"type": "phpcodesniffer-standard",
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-3.0-or-later"
],
"authors": [
{
"name": "Wim Godden",
"role": "lead"
},
{
"name": "Juliette Reinders Folmer",
"role": "lead"
}
],
"description": "A ruleset for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by WordPress.",
"homepage": "http://phpcompatibility.com/",
"keywords": [
"compatibility",
"phpcs",
"standards",
"wordpress"
],
"time": "2019-08-28T14:22:28+00:00"
},
{
"name": "sirbrillig/phpcs-changed",
"version": "v2.5.1",
"source": {
"type": "git",
"url": "https://github.com/sirbrillig/phpcs-changed.git",
"reference": "735133374065f7541e9201c06893eec5ebbf53d3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sirbrillig/phpcs-changed/zipball/735133374065f7541e9201c06893eec5ebbf53d3",
"reference": "735133374065f7541e9201c06893eec5ebbf53d3",
"shasum": ""
},
"require": {
"php": "^7.1"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.4",
"limedeck/phpunit-detailed-printer": "^3.1",
"phpstan/phpstan": "^0.11.12",
"phpunit/phpunit": "^6.4",
"sirbrillig/phpcs-import-detection": "^1.1.1",
"sirbrillig/phpcs-variable-analysis": "^2.1.3",
"squizlabs/php_codesniffer": "^3.2.1"
},
"bin": [
"bin/phpcs-changed"
],
"type": "library",
"autoload": {
"psr-4": {
"PhpcsChanged\\": "PhpcsChanged/"
},
"files": [
"PhpcsChanged/Cli.php",
"PhpcsChanged/SvnWorkflow.php",
"PhpcsChanged/GitWorkflow.php",
"PhpcsChanged/functions.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Payton Swick",
"email": "payton@foolord.com"
}
],
"description": "Run phpcs on files, but only report warnings/errors from lines which were changed.",
"time": "2020-06-24T15:36:36+00:00"
},
{
"name": "sirbrillig/phpcs-variable-analysis",
"version": "v2.8.3",
"source": {
"type": "git",
"url": "https://github.com/sirbrillig/phpcs-variable-analysis.git",
"reference": "00b4fa3130faa26762c929989e3d958086c627f1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sirbrillig/phpcs-variable-analysis/zipball/00b4fa3130faa26762c929989e3d958086c627f1",
"reference": "00b4fa3130faa26762c929989e3d958086c627f1",
"shasum": ""
},
"require": {
"php": ">=5.4.0",
"squizlabs/php_codesniffer": "^3.1"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.4 || ^0.5 || ^0.6",
"limedeck/phpunit-detailed-printer": "^3.1 || ^4.0 || ^5.0",
"phpstan/phpstan": "^0.11.8",
"phpunit/phpunit": "^5.0 || ^6.5 || ^7.0 || ^8.0",
"sirbrillig/phpcs-import-detection": "^1.1"
},
"type": "phpcodesniffer-standard",
"autoload": {
"psr-4": {
"VariableAnalysis\\": "VariableAnalysis/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-2-Clause"
],
"authors": [
{
"name": "Sam Graham",
"email": "php-codesniffer-variableanalysis@illusori.co.uk"
},
{
"name": "Payton Swick",
"email": "payton@foolord.com"
}
],
"description": "A PHPCS sniff to detect problems with variables.",
"time": "2020-07-11T23:32:06+00:00"
},
{
"name": "squizlabs/php_codesniffer",
"version": "3.5.6",
"source": {
"type": "git",
"url": "https://github.com/squizlabs/PHP_CodeSniffer.git",
"reference": "e97627871a7eab2f70e59166072a6b767d5834e0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/e97627871a7eab2f70e59166072a6b767d5834e0",
"reference": "e97627871a7eab2f70e59166072a6b767d5834e0",
"shasum": ""
},
"require": {
"ext-simplexml": "*",
"ext-tokenizer": "*",
"ext-xmlwriter": "*",
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
},
"bin": [
"bin/phpcs",
"bin/phpcbf"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Greg Sherwood",
"role": "lead"
}
],
"description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.",
"homepage": "https://github.com/squizlabs/PHP_CodeSniffer",
"keywords": [
"phpcs",
"standards"
],
"time": "2020-08-10T04:50:15+00:00"
},
{
"name": "wp-coding-standards/wpcs",
"version": "2.3.0",
"source": {
"type": "git",
"url": "https://github.com/WordPress/WordPress-Coding-Standards.git",
"reference": "7da1894633f168fe244afc6de00d141f27517b62"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/7da1894633f168fe244afc6de00d141f27517b62",
"reference": "7da1894633f168fe244afc6de00d141f27517b62",
"shasum": ""
},
"require": {
"php": ">=5.4",
"squizlabs/php_codesniffer": "^3.3.1"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.5 || ^0.6",
"phpcompatibility/php-compatibility": "^9.0",
"phpcsstandards/phpcsdevtools": "^1.0",
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
},
"suggest": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.6 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically."
},
"type": "phpcodesniffer-standard",
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Contributors",
"homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors"
}
],
"description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions",
"keywords": [
"phpcs",
"standards",
"wordpress"
],
"time": "2020-05-13T23:57:56+00:00"
}
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}

67
phpcs.xml.dist Normal file
View file

@ -0,0 +1,67 @@
<?xml version="1.0"?>
<ruleset name="ZBS">
<description>Apply WordPress Coding Standards to all files</description>
<!-- Only scan PHP files. -->
<arg name="extensions" value="php"/>
<!-- Whenever possible, cache the scan results and re-use those for unchanged files on the next scan. -->
<arg name="cache"/>
<!-- Set the memory limit to 256M.
For most standard PHP configurations, this means the memory limit will temporarily be raised.
Ref: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#specifying-phpini-settings
-->
<ini name="memory_limit" value="256M"/>
<!-- Strip the filepaths down to the relevant bit. -->
<arg name="basepath" value="./"/>
<!-- Check up to 20 files simultaneously. -->
<arg name="parallel" value="20"/>
<!-- Show sniff codes in all reports. -->
<arg value="ps"/>
<file>.</file>
<rule ref="WordPress-Core"/>
<rule ref="WordPress.CodeAnalysis.EmptyStatement"/>
<!-- These rules are being set as warnings instead of errors, so we can error check the entire codebase. -->
<rule ref="WordPress.PHP.YodaConditions.NotYoda">
<type>warning</type>
</rule>
<rule ref="WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase">
<type>warning</type>
</rule>
<rule ref="WordPress.DB.PreparedSQL.InterpolatedNotPrepared">
<type>warning</type>
</rule>
<rule ref="WordPress.DB.PreparedSQL.NotPrepared">
<type>warning</type>
</rule>
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<type>warning</type>
</rule>
<!-- Set the correct textdomain for translation functions. -->
<rule ref="WordPress.WP.I18n">
<properties>
<property name="text_domain" type="array" value="zero-bs-crm" />
</properties>
</rule>
<!-- Directories and third party library exclusions. -->
<exclude-pattern>/vendor/*</exclude-pattern>
<!-- Assignments in while conditions are a valid method of looping over iterables. -->
<rule ref="WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition">
<exclude-pattern>*</exclude-pattern>
</rule>
<!-- We're not going to rename files. -->
<rule ref="WordPress.Files.FileName">
<exclude-pattern>*</exclude-pattern>
</rule>
</ruleset>

101
pre-commit-hook.js Normal file
View file

@ -0,0 +1,101 @@
/**
* External dependencies
*/
const execSync = require( 'child_process' ).execSync;
const spawnSync = require( 'child_process' ).spawnSync;
const existsSync = require( 'fs' ).existsSync;
const chalk = require( 'chalk' );
const path = require( 'path' );
const _ = require( 'lodash' );
/*
* A lot of this code has been liberally borrowed from the wp-calypso pre-commit script
* https://github.com/Automattic/wp-calypso/blob/master/bin/pre-commit-hook.js
*/
const phpcsChangedPath = getPathForCommand( 'phpcs-changed' );
const phpcsPath = getPathForCommand( 'phpcs' );
function quotedPath( pathToQuote ) {
if ( pathToQuote.includes( ' ' ) ) {
return `"${ pathToQuote }"`;
}
return pathToQuote;
}
/**
* Parses the output of a git diff command into file paths.
*
* @param {string} command Command to run. Expects output like `git diff --name-only […]`
* @returns {Array} Paths output from git command
*/
function parseGitDiffToPathArray( command ) {
return execSync( command, { encoding: 'utf8' } )
.split( '\n' )
.map( ( name ) => name.trim() )
.filter( ( name ) => /(?:\.json|\.[jt]sx?|\.scss|\.php)$/.test( name ) );
}
function getPathForCommand( command ) {
const composerBinDir = path.join( __dirname, '..', 'vendor', 'bin' );
let path_to_command;
try {
path_to_command = execSync( 'command -v ' + command, { encoding: 'utf8' } );
} catch ( e ) {
path_to_command = path.join( composerBinDir, command );
}
if ( typeof path_to_command === 'undefined' || ! path_to_command ) {
return false;
}
return _.trim( path_to_command );
}
function printPhpcsDocs() {
console.log(
chalk.red( 'COMMIT ABORTED:' ),
'Working with PHP files in this repository requires the PHP Code Sniffer and its dependencies to be installed. Make sure you have composer installed on your machine and from the root of this repository, run `composer install`.'
);
process.exit( 1 );
}
function phpcsInstalled() {
if ( existsSync( phpcsPath ) && existsSync( phpcsChangedPath ) ) {
return true;
}
return false;
}
function linterFailure() {
console.log(
chalk.red( 'COMMIT ABORTED:' ),
'The linter reported some problems. ' +
'If you are aware of them and it is OK, ' +
'repeat the commit command with --no-verify to avoid this check.'
);
process.exit( 1 );
}
// determine if PHPCS is available
const phpcs = phpcsInstalled();
// grab a list of all the files staged to commit
const files = parseGitDiffToPathArray( 'git diff --cached --name-only --diff-filter=ACM' ).filter( ( file ) => file.endsWith( '.php' ) );
if ( files.length ) {
if ( phpcs ) {
const lintResult = spawnSync(
`PHPCS=${ quotedPath( phpcsPath ) } ${ quotedPath( phpcsChangedPath ) }`,
[ '--git', ...files ],
{
shell: true,
stdio: 'inherit',
}
);
if ( lintResult.status ) {
linterFailure();
}
} else {
printPhpcsDocs();
}
}

View file

@ -10,6 +10,7 @@
"@wordpress/browserslist-config": "^2.2.2",
"autoprefixer": "^9.5.1",
"chokidar-cli": "^2.1.0",
"husky": "^4.2.5",
"minimist": "^1.2.2",
"node-sass": "^4.13.1",
"npm-run-all": "^4.1.5",
@ -42,5 +43,10 @@
"build": "run-s \"build:*\"",
"watch": "chokidar \"**/*.scss\" -c \"npm run build\" --initial",
"child-theme": "sh ../theme-dev-utils/build-child-theme.sh"
}
},
"husky": {
"hooks": {
"pre-commit": "node pre-commit-hook.js"
}
}
}