PHP_CodeSniffer requires PHP version 5.1.2 or greater, although individual sniffs may have additional requirements such as external applications and scripts. See the Configuration Options manual page for a list of these requirements.
The SVN pre-commit hook requires PHP version 5.2.4 or greater due to its use of the vertical whitespace character.
-
You can install PHP_CodeSniffer using the PEAR installer. This will make the
phpcsandphpcbfcommands immediately available for use. To install PHP_CodeSniffer using the PEAR installer, first ensure you have installed PEAR and then run the following command:$ pear install PHP_CodeSniffer -
If you installed PHP_CodeSniffer you must add NVD folder to your PHP_CodeSniffer standard folder so run the following command:
$ cd ~ $ mkdir .phpcs $ cd .phpcs $ git clone git@github.com:NVD-R/NVDPhpStandard.git $ sudo ln -s ~/.phpcs/NVDPhpStandard/NVD /usr/share/pear/PHP/CodeSniffer/Standards/NVD
-
PHP_CodeSniffer can have multiple coding standards installed to allow a single installation to be used with multiple projects. When checking PHP code, PHP_CodeSniffer can be told which coding standard to use. This is done using the --standard command line argument so you should use [NVD] standard.
The example below checks the myfile.php file for violations of the [NVD] coding standard (installed above).
$ phpcs --standard=NVD /path/to/code/myfile.php -
Also you can use this standard in git hooks file. The ecample below run git hooks pre-commit for checks your code when you use [git commit] command.
$ cd yourProject/.git/hooks $ vim pre-commitAdd below code to the pre-commit file.
#!/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
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..." phpcbf --standard=NVD --encoding=utf-8 -n -p $FILES phpcs --standard=NVD --encoding=utf-8 -n -p $FILES
if [ $? != 0 ]
then
echo "Fix the error before commit."
exit 1
fi
fi
exit $?
Standard Code For Instance
--------------------------
- The code below is an instance for standard coding.