#PHPWorld @salcode

Enforcing Coding Standards with PHP_CodeSniffer

Sal Ferrarello / @salcode

salcode.com/phpworld-sniff

Jump to resources

Laminate Flooring

Two floors where the planks do not line up.

myfile.php

function getLength() {
    return $this->length;
}
 

myfile.php

function getLength()
{
    return $this->length;
}

PHP CodeSniffer

Command Line Tools

  • phpcs
  • phpcbf

phpcs

PHP Code Sniffer - find mistakes

phpcs --standard=<standard> <file>
phpcs --standard=PSR12 myfile.php
function getLength() {
    return $this->length;
}
$ phpcs --standard=PSR12 myfile.php

-------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------
 3 | ERROR | [x] Opening brace should be on a new
line
FILE: myfile.php
-------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------
 3 | ERROR | [x] Opening brace should be on a new
 line
-------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS
AUTOMATICALLY
-------------------------------------------------
Time: 402ms; Memory: 6Mb
 
 
 
 
 
 
-------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS
AUTOMATICALLY
-------------------------------------------------
 

phpcbf

PHP Code Beautifier and Fixer - fix mistakes

phpcbf --standard=<standard> <file>
phpcbf --standard=PSR12 myfile.php
PHPCBF RESULT SUMMARY
---------------------------------------
FILE           FIXED  REMAINING
---------------------------------------
myfile.php     1      0
---------------------------------------
A TOTAL OF 1 ERROR WERE FIXED IN 1 FILE
---------------------------------------

Time: 98ms; Memory: 6Mb

Fixed Automatically!

function getLength()
{
    return $this->length;
}

Terminology

Single Rule

a.k.a. sniff

Ruleset

set of rules, when given a name is a "standard"

hello.php

$name = 'php[world]';
echo 'hello ' .$name;
$ phpcs --standard=PSR12 hello.php

-------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
-------------------------------------------------
 2 | ERROR | [x] Expected at least 1 space after
 "."; 0 found

Directory

 
	|
	|__hello.php
	|
	|__myfile.php
	|
	|__src
	   |__MyClass.php
 

Sniff Directory Recursively

$ phpcs --standard=PSR12 .
FILE: hello.php
-------------------------------------------------
 2 | ERROR | [x] Expected at least 1 space after
 "."; 0 found

FILE: src/MyClass.php
-------------------------------------------------
 5 | ERROR | [x] Opening brace of a class must be
 on the line after the definition
 

Repeat & Modify Command

$ phpcs --standard=PSR12 .

to

$ phpcbf --standard=PSR12 .

Repeat & Modify Command

$ phpcs --standard=PSR12 .

to

$ ^phpcs^phpcbf
$ phpcbf --standard=PSR12 .

PHPCBF RESULT SUMMARY
-----------------------------------------
FILE              FIXED  REMAINING
-----------------------------------------
hello.php         1      0
src/MyClass.php   1      0
-----------------------------------------
A TOTAL OF 2 ERRORS WERE FIXED IN 2 FILES
-----------------------------------------

Available Code Standards?

$ phpcs -i
The installed coding standards are PEAR, Zend,
PSR2, MySource, Squiz, PSR1, and PSR12

Install PHP CodeSniffer

with Composer

composer require --dev \
squizlabs/php_codesniffer ^3.5

composer.json


	"require-dev": {
		"squizlabs/php_codesniffer": "^3.5"
	}
 

PHP CodeSniffer

Installed locally to your project.

./vendor/bin/phpcs
 

PHP CodeSniffer

Installed locally to your project.

./vendor/bin/phpcs --standard=PSR12 \
--ignore=/vendor/* .

.phpcs.xml.dist

<?xml version="1.0"?>
<ruleset>
    <!-- Scan everything -->
    <file>.</file>

    <!-- Exclude the Composer vendor directory.-->
    <exclude-pattern>/vendor/*</exclude-pattern>

    <!-- Use standard PSR12 -->
    <rule ref="PSR12" />
</ruleset>

.phpcs.xml.dist



    <!-- Scan everything -->
    <file>.</file>

    <!-- Exclude the Composer vendor directory.-->
    <exclude-pattern>/vendor/*</exclude-pattern>

    <!-- Use standard PSR12 -->
    <rule ref="PSR12" />
 

With Config File

./vendor/bin/phpcs

Version Control

  • Easy to recreate
  • Continuous Integration and Delivery
Man sitting on box with chair inside it instead of assembling chair.

In Your Editor

PHPCS message in Visual Studio Code.

Visual Studio Code

PHPCS one error in Visual Studio Code.

Fixed

PHPCS one error fixed in Visual Studio Code.

PHPCS in Editors

Visual Studio Code

Visual Studio Code Extensions menu item.

Extensions

Visual Studio Code Extensions phpcs.
PHPCS one error in Visual Studio Code.

Tabs vs Spaces

Man and woman, one labeled tabs, the other spaces.

.editorconfig

root = true

[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[*.md]
trim_trailing_whitespace = false

With Plugin

Editors that support .editorconfig with a plugin.

VS Code

Installing .editorconfig extension in Visual Studio Code.

Breaking the Rules

Pulling paper towel with one hand when it has pull with two hands.

Ignore a Line

$name = 'php[world]';

echo 'hello ' .$name;

Ignore a Line

$name = 'php[world]';

echo 'hello ' .$name; // phpcs:ignore

Ignore a Line

$name = 'php[world]';
// phpcs:ignore
echo 'hello ' .$name;

Find a Sniff Name

$ phpcs -s
3 | ERROR | [x] Expected at least 1 space after
"."; 0 found
(PSR12.Operators.OperatorSpacing.NoSpaceAfter)

Ignore a Sniff (one line)

$name = 'php[world]';


echo 'hello ' .$name; // phpcs:ignore PSR12
.Operators.OperatorSpacing.NoSpaceAfter

Ignore a Sniff (one line)

$name = 'php[world]';
// phpcs:ignore PSR12.Operators
.OperatorSpacing.NoSpaceAfter
echo 'hello ' .$name;
 

Ignore a Sniff Globally

.phpcs.xml.dist


	<rule ref="PSR12" />



 

Ignore a Sniff Globally

.phpcs.xml.dist


	<rule ref="PSR12">
		<exclude name="PSR12.Operators
		.OperatorSpacing.NoSpaceAfter" />
	</rule>
	 

Adding a Standard

composer require --dev \
wp-coding-standards/wpcs ^1.1

Wire It Up

composer require --dev \
dealerdirect/phpcodesniffer-composer-installer \
'^0.5'

List Standards

$ ./vendor/bin/phpcs -i
The installed coding standards are PEAR, Zend,
PSR2, MySource, Squiz, PSR1, PSR12, WordPress-VIP,
WordPress, WordPress-Extra, WordPress-Docs and
WordPress-Core

.phpcs.xml.dist


<rule ref="PSR12" />


 

.phpcs.xml.dist


<rule ref="WordPress-Extra" />


 

.phpcs.xml.dist


<rule ref="WordPress-Extra" />
<rule ref="WordPress-Docs" />

 

.phpcs.xml.dist


<rule ref="WordPress-Extra" />
<rule ref="WordPress-Docs" />
<rule ref="Generic.Commenting.Todo" />
 

Summary

  • Use phpcs
  • Version control setup/config
  • Use .editorconfig
  • Add editor plugins
  • Add to CI/CD Pipeline

WebDevStudios

Sal Ferrarello

salcode.com/phpworld-sniff

Resources

Image Credits