2017 © Pedro Pelรกez
 

phpcodesniffer-standard php-coding-standards

PHP 7+ coding standards for Inpsyde WordPress projects.

image

inpsyde/php-coding-standards

PHP 7+ coding standards for Inpsyde WordPress projects.

  • Monday, May 21, 2018
  • by inpsyde
  • Repository
  • 7 Watchers
  • 16 Stars
  • 3,064 Installations
  • PHP
  • 13 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 22 Versions
  • 35 % Grown

The README.md

Syde PHP Coding Standards

PHP 7.4+ coding standards for Syde WordPress projects., (*1)

PHP Quality Assurance, (*2)


Usage

When the package is installed via Composer, and dependencies are updated, everything is ready and the coding standards can be checked via:, (*3)

vendor/bin/phpcs --standard="Inpsyde" <path>

Here, <path> is at least one file or directory to check, for example:, (*4)

vendor/bin/phpcs --standard="Inpsyde" ./src/ ./my-plugin.php

There are many options that can be used to customise the behavior of the command, to get documentation use:, (*5)

vendor/bin/phpcs --help

Configuration File

A phpcs.xml.dist file can be used to avoid passing many arguments via the command line. For example:, (*6)


<ruleset name="MyProjectCodingStandard">

    <description>My Project coding standard.</description>

    <file>./src</file>
    <file>./tests/src</file>

    <arg value="sp"/>
    <arg name="colors"/>

    <config name="testVersion" value="7.4-"/>
    <config name="text_domain" value="my-project"/>

    <rule ref="Inpsyde">
        <exclude name="WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize"/>
    </rule>

    <rule ref="Inpsyde.CodeQuality.Psr4">
        <properties>
            <property
                name="psr4"
                type="array"
                value="
                    Inpsyde\MyProject=>src,
                    Inpsyde\MyProject\Tests=>tests/src|tests/unit
                "/>
        </properties>
    </rule>

</ruleset>

Such a configuration allows to run the code style check like so:, (*7)

vendor/bin/phpcs

Moreover, thanks to the text_domain setting, PHP_CodeSniffer will also check that all WordPress internationalization functions are called with the proper text domain., (*8)


Included rules

For the detailed lists of included rules, refer to ruleset.xml., (*9)

PSR-1, PSR-2, PSR-12

For more information about included rules from PHP Standards Recommendations (PSR), refer to the official documentation:, (*10)

WordPress Coding Standards

To ensure code quality, and compatibility with WordPress VIP, some rules have been included from:, (*11)

Slevomat

A few rules have been included from the Slevomat Coding Standard., (*12)

PHPCompatibility

For PHP cross-version compatibility checks, the full PHP Compatibility Coding Standard for PHP CodeSniffer standard has been included., (*13)

The target PHP version (range) can be changed via a custom phpcs.xml file., (*14)

Generic Rules

Some rules are also included from PHP_CodeSniffer itself, as well as PHPCSExtra., (*15)

Custom Rules

The following custom rules are in use:, (*16)

Sniff Name Description Has Config Auto-Fixable
ArgumentTypeDeclaration Enforce argument type declaration.
DisableCallUserFunc Disable usage of call_user_func.
DisableMagicSerialize Disable usage of __sleep, __wakeup.
DisableSerializeInterface Disable usage of Serializable interface.
DisallowShortOpenTag Disallow short open PHP tag (short echo tag allowed).
ElementNameMinimalLength Use minimum 3 chars for names (with a few exclusions) โœ“
EncodingComment Detect usage of opening -*- coding: utf-8 -*- โœ“ โœ“
ForbiddenPublicProperty No public class properties
FunctionBodyStart Handle blank line at start of function body. โœ“
FunctionLength Max 50 lines per function/method, excluding blank lines and comments-only lines. โœ“
HookClosureReturn Ensure that actions callbacks do not return anything, while filter callbacks return something.
HookPriority Report usage of PHP_INT_MAX and PHP_INT_MIN as hook priority.
LineLength Max 100 chars per line โœ“
NestingLevel Max indent level of 3 inside functions โœ“
NoAccessors Discourage usage of getters and setters.
NoElse Discourage usage of else.
NoRootNamespaceFunctions Report usage of global functions in the root namespace.
NoTopLevelDefine Discourage usage of define where const is preferable.
PropertyPerClassLimit Discourage usage of more than 10 properties per class. โœ“
Psr4 Check PSR-4 compliance โœ“
ReturnTypeDeclaration Enforce return type declaration
StaticClosure Points closures that can be static. โœ“
VariablesName Check variable (and properties) names โœ“

For notes and configuration, refer to the inpsyde-custom-sniffs.md file in this repository., (*17)


Template Rules

The InpsydeTemplates ruleset extends the standard Inpsyde ruleset with some template-specific sniffs., (*18)

The recommended way to use the InpsydeTemplates ruleset is as follows:, (*19)

<ruleset>
    <file>./src</file>
    <file>./templates</file>
    <file>./tests</file>
    <file>./views</file>

    <rule ref="Inpsyde"/>

    <rule ref="InpsydeTemplates">
        <include-pattern>*/templates/*</include-pattern>
        <include-pattern>*/views/*</include-pattern>
    </rule>
</ruleset>

The following template-specific rules are available:, (*20)

Sniff Name Description Has Config Auto-Fixable
AlternativeControlStructure Encourage usage of alternative syntax with inline HTML.
ShortEchoTag Replace echo with short echo tag in single-line statements. โœ“
TrailingSemicolon Remove trailing semicolon before closing PHP tag. โœ“

Removing or Disabling Rules

Rules Tree

Sometimes it is necessary not to follow some rules. To avoid error reporting, it is possible to:, (*21)

  • remove rules for an entire project via configuration;
  • disable rules from code, only is specific places.

In both cases, it is possible to remove or disable:, (*22)

  • a complete standard;
  • a standard subset;
  • a single sniff;
  • a single rule.

These things are in a hierarchical relationship: standards are made of one or more subsets, which contain one or more sniffs, which in turn contain one or more rules., (*23)

Removing Rules via Configuration File

Rules can be removed for the entire project by using a custom phpcs.xml file, like this:, (*24)


<ruleset name="MyProjectCodingStandard">

    <rule ref="Inpsyde">
        <exclude name="PSR1.Classes.ClassDeclaration"/>
    </rule>

</ruleset>

In the example above, the PSR1.Classes.ClassDeclaration sniff (and all the rules it contains) has been removed., (*25)

By using PSR1 instead of PSR1.Classes.ClassDeclaration, one would remove the entire PSR1 standard, whereas using PSR1.Classes.ClassDeclaration.MultipleClasses would remove this one rule only, but no other rules in the PSR1.Classes.ClassDeclaration sniff., (*26)

Removing Rules via Code Comments

Removing a rule/sniff/subset/standard only for a specific file or a part of it can be done by using special phpcs annotations/comments, for example, // phpcs:disable followed by an optional name of a standard/subset/sniff/rule. Like so:, (*27)

// phpcs:disable PSR1.Classes.ClassDeclaration

For more information about ignoring files, please refer to the official PHP_CodeSniffer Wiki., (*28)


IDE Integration

PhpStorm

After installing the package as explained above, open PhpStorm settings, and navigate to, (*29)

Language & Frameworks -> PHP -> Quality Tools -> PHP_CodeSniffer, (*30)

Choose "Local" in the "Configuration" dropdown., (*31)

Click the "..." button next to the dropdown. It will show a dialog where you need to specify the path for the PHP_CodeSniffer executable., (*32)

Open the file selection dialog, navigate to vendor/bin/ in your project, and select phpcs. On Windows, choose phpcs.bat., (*33)

Click the "Validate" button next to the path input field. If everything is working fine, a success message will be shown at the bottom of the window., (*34)

Still in the PhpStorm settings, navigate to:, (*35)

Editor -> Inspections, (*36)

Type codesniffer in the search field before the list of inspections, then select:, (*37)

PHP -> Quality Tools -> PHP_CodeSniffer validation, (*38)

Enable it using the checkbox in the list, press "Apply"., (*39)

Select "PHP_CodeSniffer validation", click the refresh icon next to the "Coding standard" dropdown on the right, and choose Inpsyde., (*40)

If you don't see Inpsyde here, you may need to specify the phpcs.xml file by selecting "Custom" as standard and then use the "..." button next to the dropdown., (*41)

Once the PhpStorm integration is complete, warnings and errors in your code will automatically be shown in your IDE editor., (*42)


Installation

Via Composer, require as development dependency:, (*43)

composer require "inpsyde/php-coding-standards:^2@dev" --dev

(Please note that @dev can be removed as soon as a stable 2.0.0 version has been released, or if your root package minimum stability is dev)., (*44)

The Versions

21/05 2018

dev-master

9999999-dev

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

21/05 2018

0.13.1

0.13.1.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

21/05 2018

0.13.0

0.13.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

15/04 2018

0.12.0

0.12.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

19/03 2018

0.11.0

0.11.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

09/03 2018

dev-issue-8

dev-issue-8

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

26/02 2018

0.10.0

0.10.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

22/02 2018

0.9.0

0.9.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

13/02 2018

0.8.0

0.8.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

13/02 2018

0.7.2

0.7.2.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

12/02 2018

0.7.1

0.7.1.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

12/02 2018

0.7.0

0.7.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

12/02 2018

0.6.0

0.6.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

05/02 2018

0.5.1

0.5.1.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

05/02 2018

0.5.0

0.5.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

05/02 2018

0.4.2

0.4.2.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

01/02 2018

0.4.1

0.4.1.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

31/01 2018

0.4.0

0.4.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

29/01 2018

0.3.0

0.3.0.0

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

29/01 2018

dev-psr

dev-psr

PHP 7+ coding standards for Inpsyde WordPress projects.

  Sources   Download

MIT

The Requires

 

wordpress phpcs standards coding standards code style psr2 coding style code standards object calisthenics php standards wordpress standards inpsyde

08/11 2017

0.2.0

0.2.0.0

PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions

  Sources   Download

MIT

The Requires

 

wordpress phpcs standards

21/06 2017

0.1.0

0.1.0.0

PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions

  Sources   Download

MIT

The Requires

 

wordpress phpcs standards