2017 © Pedro PelĂĄez
 

library php_codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

image

manoj-malviya/php_codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  • Sunday, October 9, 2016
  • by manoj-malviya
  • Repository
  • 1 Watchers
  • 0 Stars
  • 158 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 923 Forks
  • 0 Open issues
  • 36 Versions
  • 0 % Grown

The README.md

First, we need a development dependency specified to install phpcs. It looks something like this:, (*1)

{
    "require-dev": {
        "manoj-malviya/php_codesniffer": "^2.4"
    }
}

Install Scripts

Composer has a handy schema entry called scripts. It supports a script hook post-install-cmd. We will use this to install a git pre-commit hook. Adding to our example above:, (*2)

{
    "require-dev": [
        "manoj-malviya/php_codesniffer": "^2.4"
    ],
    "scripts": {
        "post-install-cmd": [
            "bash contrib/setup.sh"
        ]
    }
}

This will run a bash script called setup.sh when the command composer install is run., (*3)

Setup the Git Pre-commit Hook

In our setup.sh, we will need to copy a pre-commit script into the .git/hooks directory:, (*4)

#!/bin/sh

cp contrib/pre-commit .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

This will copy our pre-commit script from the contrib directory to the hooks section of the special git directory and make it executable., (*5)

Create the Pre-commit Hook

Whenever a contributing developer attempts to commit their code, it will run our pre-commit script. Now all we need to do is run the code sniffer rules on relavent files specific to this commit:, (*6)

#!/bin/sh

PROJECT=`php -r "echo dirname(dirname(dirname(realpath('$0'))));"`
STAGED_FILES_CMD=`git diff --cached --name-only --diff-filter=ACMR HEAD | grep \\\\.php`

# Determine if a file list is passed
if [ "$#" -eq 1 ]
then
    oIFS=$IFS
    IFS='
    '
    SFILES="$1"
    IFS=$oIFS
fi
SFILES=${SFILES:-$STAGED_FILES_CMD}

echo "Checking PHP Lint..."
for FILE in $SFILES
do
    php -l -d display_errors=0 $PROJECT/$FILE
    if [ $? != 0 ]
    then
        echo "Fix the error before commit."
        exit 1
    fi
    FILES="$FILES $PROJECT/$FILE"
done

if [ "$FILES" != "" ]
then
    echo "Running Code Sniffer..."
    ./vendor/bin/phpcs --standard=PSR1 --encoding=utf-8 -n -p $FILES
    if [ $? != 0 ]
    then
        echo "Fix the error before commit."
        exit 1
    fi
fi

exit $?

This script will get the staged files of the commit, run a php lint check (always nice), and apply the code sniffer rules to the staged files., (*7)

If there is a code standards violation, the phpcs process will return a non-zero exit status which will tell git to abort the commit., (*8)

Bringing it all together

With all of these things in place, the workflow is as follows:, (*9)

Developer runs composer install. PHP Code Sniffer is installed via a dev dependency. The post-install command automatically copies the pre-commit hook into the developer’s local git hooks. When the developer commits code, the pre-commit hook fires and checks the staged files for coding standards violations and lint checks. This is a relatively simple setup that can save pull request code reviews a significant amount of time preventing back-and-forth on simple things such as mixed tabs/spaces, bracket placement, etc., (*10)

The Versions

09/10 2016

dev-master

9999999-dev http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-simplexml *
  • ext-tokenizer *
  • ext-xmlwriter *
  • php >=5.1.2

 

The Development Requires

by Greg Sherwood

phpcs standards

27/11 2015

3.0.x-dev

3.0.9999999.9999999-dev http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • ext-xmlwriter *
  • php >=5.4.0

 

The Development Requires

by Greg Sherwood

phpcs standards

23/11 2015

2.4.0

2.4.0.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • ext-xmlwriter *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

09/09 2015

2.3.4

2.3.4.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • ext-xmlwriter *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

24/06 2015

2.3.3

2.3.3.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • ext-xmlwriter *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

29/04 2015

2.3.2

2.3.2.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • ext-xmlwriter *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

23/04 2015

2.3.1

2.3.1.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • ext-xmlwriter *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

04/03 2015

2.3.0

2.3.0.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • ext-xmlwriter *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

21/01 2015

2.2.0

2.2.0.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

18/12 2014

2.1.0

2.1.0.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

05/12 2014

2.0.0

2.0.0.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

04/12 2014

1.5.x-dev

1.5.9999999.9999999-dev http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

04/12 2014

1.5.6

1.5.6.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

06/11 2014

2.0.0RC4

2.0.0.0-RC4 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

16/10 2014

2.0.0RC3

2.0.0.0-RC3 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

26/09 2014

2.0.0RC2

2.0.0.0-RC2 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

25/09 2014

1.5.5

1.5.5.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

06/08 2014

2.0.0RC1

2.0.0.0-RC1 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

06/08 2014

1.5.4

1.5.4.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

01/05 2014

2.0.0a2

2.0.0.0-alpha2 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

01/05 2014

1.5.3

1.5.3.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

05/02 2014

2.0.0a1

2.0.0.0-alpha1 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

05/02 2014

1.5.2

1.5.2.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

12/12 2013

1.5.1

1.5.1.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

27/11 2013

1.5.0

1.5.0.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

25/11 2013

1.4.8

1.4.8.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

26/09 2013

1.5.0RC4

1.5.0.0-RC4 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

26/09 2013

1.4.7

1.4.7.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

25/07 2013

1.5.0RC3

1.5.0.0-RC3 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

25/07 2013

1.4.6

1.4.6.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • ext-tokenizer *
  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

04/04 2013

1.5.0RC2

1.5.0.0-RC2 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

04/04 2013

1.4.5

1.4.5.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

08/02 2013

1.5.0RC1

1.5.0.0-RC1 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

07/02 2013

1.4.4

1.4.4.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

04/12 2012

1.4.3

1.4.3.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.1.2

 

by Greg Sherwood

phpcs standards

09/11 2012

1.4.2

1.4.2.0 http://www.squizlabs.com/php-codesniffer

PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.1.2

 

by Greg Sherwood

phpcs standards