PHP_CodeSniffer Introduction

Author - Preeti Bisht

What Is PHP_CodeSniffer?

PHP_CodeSniffer is an essential development tool that helps developers to work in a standardise format so as to make code clean and consistent. It ensures that same coding style is being followed. It allows a set of rules or a standard to be applied to source code.These rules can be used to detect common programming errors. This tool can also be used to define a set of Coding Standards for your project.

You can check if it is already installed on your machine by:

* phpcs -v

It can be installed by the following command:

* sudo apt-get update
* sudo apt-get install php-codesniffer

There will be a file phpcs in the vendor/din directory of your project which will look like this:

You can download its dependencies via composer as well:

For that first install composer:

* curl -sS https://getcomposer.org/installer | sudo php — –install-dir=/usr/local/bin –filename=composer

Then install PHP_CodeSniffer by:

* $ composer global require “squizlabs/php_codesniffer=*”

Which will look like this in the composer.json file of your project :

Php_codesniffer can be used as follows:

Navigate to your project directory and type the following command:

* $ vendor/bin/phpcs

Example:
* $ vendor/bin/phpcs app/controller/userController

You can also specify the php coding standard to run php_codesniffer with:

PSR (PHP Standard Recommendation) is a PHP specification published by the PHP Framework Interop Group

There are 4 PSR standards that you can use:

* $ vendor/bin/phpcs –standard=PSR2 src/controller/userController

It will return errors for all the mismatches which are not in accordance with PSR2:

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

Send your message