2017 © Pedro Peláez
 

phpcodesniffer-standard coding-standards

Joomla Coding Standards

image

joomla/coding-standards

Joomla Coding Standards

  • Thursday, March 29, 2018
  • by mbabker
  • Repository
  • 40 Watchers
  • 107 Stars
  • 7,521 Installations
  • PHP
  • 50 Dependents
  • 0 Suggesters
  • 124 Forks
  • 38 Open issues
  • 7 Versions
  • 48 % Grown

The README.md

Since Joomla version 4.2, Joomla uses the PSR-12 coding standard., (*1)

We will update this soon to reflect this change, (*2)

All not php coding standards are still valid, (*3)


Joomla Coding Standards Build Status

Latest Stable Version Latest Unstable Version License, (*4)

This repository includes the Joomla coding standard definition for PHP Codesniffer along with a few other helpful resources. The PHP_CodeSniffer standard will never be 100% accurate, but should be viewed as a strong set of guidelines while writing software for Joomla., (*5)

See Joomla coding standards documentation at https://developer.joomla.org/coding-standards.html, (*6)

If you want to contribute and improve this documentation, you can find the source files in the manual section of the master branch https://github.com/joomla/coding-standards/tree/master/manual, (*7)

Requirements

Important note: currently the latest PHPCS is the 3.x series. There is a development branch for the Joomla custom sniffs Which is compatible with the PHPCS 3.x series. Although PEAR and composer give you the option to install PHPCS 3.x by default the Joomla custom sniffs releases are currently only compatible with PHPCS 2.x and will not work, so we remind you to always install the PHPCS 2.x series if you are using the Joomla custom sniffs. If you want to test the Joomla Code Standard development branch for PHPCS 3.x, please manually install the 3.x branch., (*8)

Installation via Composer

Add "joomla/coding-standards": "~2.0" to the require-dev block in your composer.json and then run composer install., (*9)

{
    "require-dev": {
        "joomla/coding-standards": "~2.0"
    }
}

Alternatively, you can simply run the following from the command line:, (*10)

composer global require squizlabs/php_codesniffer "~2.8"
composer require joomla/coding-standards "~2.0"

As stability of joomla/coding-standards 2.0.0 is currently alpha, make sure you allow usage of alpha software in Composer: In Composer json, (*11)

{
    "require-dev": {
        "joomla/coding-standards": "~2.0@alpha"
    }
}

or on the command line, (*12)

composer config minimum-stability "alpha"

If you want to install the coding-standards globally, edit your Composer global configuration by running:, (*13)

composer global config -e

and add the following: "minimum-stability": "alpha", (*14)

Once you have the coding standard files you can tell PHPCS where the standard folder is (i.e. install them in PHPCS), (*15)

phpcs --config-set installed_paths /path/to/joomla-coding-standards

Note: the composer scripts will run when the standard is installed globally, but not when it's a dependency. As such, you may want to run PHPCS config-set. When you run PHPCS config-set it will always overwrite the previous values. Use --config-show to check previous values before using --config-set So instead of overwriting the existing paths you should copy the existing paths revealed with --config-show and add each one separated by a comma: phpcs --config-set installed_paths [path_1],[path_2],[/path/to/joomla-coding-standards], (*16)

Running

You can use the installed Joomla standard like:, (*17)

phpcs --standard=Joomla path/to/code

Alternatively if it isn't installed you can still reference it by path like:, (*18)

phpcs --standard=path/to/joomla/coding-standards path/to/code

Selectively Applying Rules

For consuming packages there are some items that will typically result in creating their own project ruleset.xml, rather than just directly using the Joomla ruleset.xml. A project ruleset.xml allows the coding standard to be selectively applied for excluding 3rd party libraries, for consideration of backwards compatibility in existing projects, or for adjustments necessary for projects that do not need PHP 5.3 compatibility (which will be removed in a future version of the Joomla Coding Standard in connection of the end of PHP 5.3 support in all active Joomla Projects)., (*19)

For information on selectively applying rules read details in the PHP CodeSniffer wiki, (*20)

Common Rule Set Adjustments

The most common adjustment is to exclude folders with 3rd party libraries, or where the code has yet to have coding standards applied., (*21)

 
 <exclude-pattern type="relative">build/*</exclude-pattern>
 <exclude-pattern type="relative">tests/*</exclude-pattern>

 
 <exclude-pattern type="relative">libraries/*</exclude-pattern>
 <exclude-pattern type="relative">vendor/*</exclude-pattern>

Another common adjustment is to exclude the camelCase format requirement for "Classes, Functions, Methods, Regular Variables and Class Properties" the essentially allows for B/C with snake_case variables which were only allowed in the context of interacting with the database., (*22)

 <rule ref="Joomla">
  <exclude name="Joomla.NamingConventions.ValidFunctionName.ScopeNotCamelCaps"/>
  <exclude name="Joomla.NamingConventions.ValidVariableName.NotCamelCaps"/>
  <exclude name="Joomla.NamingConventions.ValidVariableName.MemberNotCamelCaps"/>
  <exclude name="Joomla.NamingConventions.ValidFunctionName.FunctionNoCapital"/>
 </rule>

Old Protected method names were at one time prefixed with an underscore. These Protected method names with underscores are deprecated in Joomla projects but for B/C reasons are still in the projects. As such, excluding the MethodUnderscore sniff is a common ruleset adjustment, (*23)

 <rule ref="Joomla">
  <exclude name="Joomla.NamingConventions.ValidFunctionName.MethodUnderscore"/>
  <exclude name="Joomla.NamingConventions.ValidVariableName.ClassVarHasUnderscore"/>
 </rule>

The last most common adjustment is removing PHP 5.3 specific rules which prevent short array syntax, and allowing short array syntax for method parameters., (*24)

 <rule ref="Generic">
  <exclude name="Generic.Arrays.DisallowShortArraySyntax"/>
 </rule>
 <rule ref="Joomla.Classes.InstantiateNewClasses">
   <properties>
     <property name="shortArraySyntax" value="true"/>
   </properties>
 </rule>

Using example rulesets that Selectively Applying Rule

You have to tell you can tell PHPCS where the example ruleset folder is (i.e. install them in PHPCS), (*25)

phpcs --config-set installed_paths /path/to/joomla/coding-standards/Example-Rulesets

Note: the composer scripts will run when the standard is installed globally, but not when it's a dependency. As such, you may want to run PHPCS config-set. When you run PHPCS config-set it will always overwrite the previous values. Use --config-show to check previous values before using --config-set So instead of overwriting the existing paths you should copy the existing paths revealed with --config-show and add each one separated by a comma: phpcs --config-set installed_paths [path_1],[path_2],[/path/to/joomla-coding-standards],[/path/to/joomla/coding-standards/Example-Rulesets], (*26)

IDE autoformatters

There is further information on how to set up IDE auto formatters here:, (*27)

https://github.com/joomla/coding-standards/tree/master/Joomla/IDE, (*28)

The Versions

29/03 2018

dev-3.x-dev

dev-3.x-dev https://github.com/joomla/coding-standards

Joomla Coding Standards

  Sources   Download

GPL-2+ GPL-2.0-or-later

The Requires

 

The Development Requires

phpcs joomla coding standards php codesniffer

26/03 2018

dev-master

9999999-dev https://github.com/joomla/coding-standards

Joomla Coding Standards

  Sources   Download

GPL-2+ GPL-2.0-or-later

The Requires

 

The Development Requires

phpcs joomla coding standards php codesniffer

10/02 2018

2.0.0-rc

2.0.0.0-RC https://github.com/joomla/coding-standards

Joomla Coding Standards

  Sources   Download

GPL-2.0-or-later

The Requires

 

The Development Requires

phpcs joomla coding standards php codesniffer

26/08 2017

dev-optional-package

dev-optional-package https://github.com/joomla/coding-standards

Joomla Coding Standards

  Sources   Download

GPL-2+

The Requires

 

The Development Requires

phpcs joomla coding standards php codesniffer

28/06 2017

2.0.0-alpha2

2.0.0.0-alpha2 https://github.com/joomla/coding-standards

Joomla Coding Standards

  Sources   Download

GPL-2+

The Requires

 

The Development Requires

phpcs joomla coding standards php codesniffer

27/04 2017

dev-phpcs-2

dev-phpcs-2 https://github.com/joomla/coding-standards

Joomla Coding Standards

  Sources   Download

GPL-2+

The Requires

 

The Development Requires

phpcs joomla coding standards php codesniffer

10/04 2017

2.0.0-alpha

2.0.0.0-alpha https://github.com/joomla/coding-standards

Joomla Coding Standards

  Sources   Download

GPL-2+

The Requires

 

The Development Requires

phpcs joomla coding standards php codesniffer