PHP_CodeSniffer – A Library For Improving Your PHP Coding Standards

Do you want to improve your PHP coding standards? Obviously, yes. No matter you are a fresher or experienced PHP developer, everyone wishes to improve their coding standards.

But, How?

Well, there are several resources available on the Internet that you can read and start to implement.

There is one more solution that does not require reading about standard coding. Instead, it gives you suggestions to improve your standards runtime by looking into your PHP file. This way is better as you will get a clear suggestion along with the line number.

Introduction To PHP_CodeSniffer

As the name suggests, PHP_CodeSniffer smells the code in your files or folders. Wherever it smells the bad code it gives you a report where the bad code is and what steps you should take to correct it. PHP_CodeSniffer acts like your PHP code checker.

It is a highly recommended library for a PHP programmer. While working on a PHP project, you should not deliver your code to the end client until you finished with PHP_CodeSniffer suggested PHP coding standards.

After all, a PHP programmer should deliver a better code and PHP_CodeSniffer helps you for better coding.

Install PHP_CodeSniffer Using Composer

Composer is a dependency manager for PHP. It allows you to install/update the library you require for your project.

You can install PHP_CodeSniffer in the root directory of your project. But, I recommend you create a folder ‘php_codesniffer’ in the root directory and install the library inside this folder.

To install the PHP_CodeSniffer using composer, open the command prompt in the ‘php_codesniffer’ directory and run the below command.

composer require "squizlabs/php_codesniffer=*"

Composer Require Command

Let’s Check Our PHP Coding Standards

Next, head over to the php_codesniffer/vendor/bin directory from the command prompt. From this bin folder, we will check our PHP coding standards against the PHP_CodeSniffer.

For instance, let’s assume we have a file called index.php in your project. My index.php file contains the below code.

<?php
require_once ("vendor/autoload.php");
require_once ("config.php");
 
\Tinify\setKey(TINIFY_KEY);
 
$source = \Tinify\fromFile("large.jpg");
$resized = $source->resize(
    array(
        "method" => "cover",
        "width" => 150,
        "height" => 100
    ));
$resized->toFile("thumbnail1.jpg");
 
echo "Image resized.";
 
$bool = true;
?>

This code is used for resizing images in PHP using TinyPNG library. Our code is working fine and giving the expected result. But, we missed some coding standards in our file.

Let’s find out what standard we are missing in the above file.

We will check this file against PHP_CodeSniffer by running the below command.

phpcs --standard=PEAR /path/to/your/file

We are working on Windows so in our case, a command would be as follows.

phpcs --standard=PEAR D:\wamp\www\api\tinypng\index.php

Here ‘–standard=PEAR’ means our file will check against the standard set by PEAR.

Run PHPCS script command

After running the above command, we got some errors in the coding standards.

Correct The Code By Using Guidelines

Let’s take one example of an error.

The second error saying ‘require_once’ is a statement not a function: no parentheses are required

By this error, we come to know about the standard for including external files.

Our below code

require_once ("vendor/autoload.php");
require_once ("config.php");

should replace with

require_once "vendor/autoload.php";
require_once "config.php";

This is one example of correcting code. In your case, you may find some other errors. You should correct it by following the instructions you got.

PHP_CodeSniffer library provides one command which automatically corrects coding standard violations. Remember, this command does not correct all errors. It will take care of some minor errors like indentation, closing parentheses, etc.

If you look at the above screenshot you will notice that a total of 7 errors were found in our file. After the error list, see the line

‘PHPCBF’ CAN FIX THE 6 MARKED SNIFF VIOLATIONS AUTOMATICALLY

PHPCBF Notice

The first error which is related to missing file doc comment, we have to the care of this doc comment. The ‘PHPCBF’ command will not correct it.

Having said that, let’s write the command for it.

phpcbf --standard=PEAR D:\wamp\www\api\tinypng\index.php

PHPCBF Command

In the screenshot, you can see it fixed 6 errors and 1 is remaining. The remaining error is related to the file doc comment.

So, we will add the file commenting and our final code will as follow.

<?php
/**
 * This file will generate the resize version of an image
 *
 * PHP version 5.6.25
 *
 * @category Resize_Image
 * @package  TinyPNG
 * @author   Sajid <sajid@artisansweb.net>
 * @license  http://www.php.net/license/3_01.txt  PHP License 3.01
 * @link     https://artisansweb.net/resize-image-php-using-tinypng
 */
require_once "vendor/autoload.php";
require_once "config.php";
 
\Tinify\setKey(TINIFY_KEY);
 
$source = \Tinify\fromFile("large.jpg");
$resized = $source->resize(
    array(
        "method" => "cover",
        "width" => 150,
        "height" => 100
    )
);
$resized->toFile("thumbnail1.jpg");
 
echo "Image resized.";
 
$bool = true;
?>

Now, if we run the ‘phpcs’ script we will not get any PHP coding standards errors.

No PHP Coding Standards Errors

Note: PHP_CodeSniffer validates your JS and CSS files also.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

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