Skip to content

Strict PHPCS standard for PSR-2 code based on Squiz standards

Notifications You must be signed in to change notification settings

kkMatt/ongr-strict-standard

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ONGR Strict Coding Standard

Build Total Downloads Latest Stable Version Latest Unstable Version License

This standard provides strict code style checking for whitespace, commenting style and PHPDoc. It is PSR-2 compatible, enforces good programming and documentation practices.

Example

Ever wanted to standardize your team's code style? This is a PSR-2 compliant code:

<?php
namespace Project\MyClass ;

use Foo;

/**
 *  this class has method

 */
class MyClass
{

    public function foo($param)
    {
        $a = $param *  2;
        return $a+1;

    }
}

No more code like this!

After ONGR Strict Standard validation:

  2 | ERROR | Space found before semicolon; expected "MyClass;" but found
    |       | "MyClass ;"
  7 | ERROR | Class comment short description must start with a capital letter
  7 | ERROR | Class comment short description must end with a full stop
  8 | ERROR | Expected 1 space(s) before asterisk; 0 found
 11 | ERROR | Expected no blank lines after an opening brace, 1 found
 13 | ERROR | Missing function doc comment
 15 | ERROR | Expected 1 space after "*"; 2 found
 16 | ERROR | Missing blank line before return statement
 16 | ERROR | Expected 1 space before "+"; 0 found
 16 | ERROR | Expected 1 space after "+"; 0 found
 18 | ERROR | Expected 0 blank lines before closing function brace; 1 found

Features

  • Standardizes whitespace almost everywhere. E.g. between methods, spaces around operators, indentation of statements, etc.
  • Required class short description everywhere except for PHPUnit testing classes.
  • Required method short descriptions except for setters / getters and some magic methods.
  • Comments start with capital letter and ends with appropriate punctuation. This suggests proper documentation and reduces laziness.
  • Short array syntax is required.
  • Require type hinting in PHPDoc for all parameters. Require @return and @throws tags where necessary.
  • Code must not be aligned in assignments and array definitions.
  • Strings should be "double quoted" only with a reason ($variable interpolation inside, etc.)

Requirements

  • PHP >=5.4
  • CodeSniffer 1.x

Acknowledgement

Our work is based solely on Squiz Labs Squiz coding standard and opensky Symfony2 coding standard.

Installation

Composer:

{
    "require-dev": {
        "ongr/ongr-strict-standard": "~1.0",
        "squizlabs/php_codesniffer": "~1"
    }
}

Or optionally you can install globally to all projects at ~/.composer/composer.json.

Then: composer global update.

Warning if you are planing on developing, then sources should be located in ONGR directory.

For example, when cloning add target directory:

    git clone [email protected]:<username>/ongr-strict-standard.git ONGR

Running

vendor/bin/phpcs -p --standard=/home/<user>/.composer/vendor/ongr/ongr-strict-standard/ONGR --ignore=vendor/,Tests/app/,Resources/public/ ./

Note: do not use ~ for HOME parameter, as PHPCS will not expand it.

IDEs also support running Code Sniffer and adding error annotations directly on editor's source code (e.g. PHPStorm). Please see your IDE's documentation on how to add standard from custom directory.

PHPStorm Helper

Configure PHPCS: http://www.jetbrains.com/phpstorm/webhelp/using-php-code-sniffer-tool.html

Configure this standard:

  1. Go to Settings > Inspections > PHP > PHP Code Sniffer Validation.
  2. Mark checkbox ON for "PHP Code Sniffer Validation".
  3. Select Custom standard.
  4. Locate vendor/ongr/ongr-strict-standard/ONGR standard directory, press OK.

Code should be validated automatically on each PHP file edit.

Licensing

This repository contains copied and or modified files from other packages. Their licenses are defined and clearly stated in those files and must be followed.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

About

Strict PHPCS standard for PSR-2 code based on Squiz standards

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.9%
  • Shell 0.1%