Understanding PHP CS-FIXER with example

|
| By Webner

Steps to Understand PHP CS-FIXER with example

Introduction

The PHP coding standards fixer tool is used to solve many issues (syntax, indentation etc) in your code when you want to follow the PHP coding standards defined in PSP-1 and PSR-2 Documents and many more.

If you have to identify problems with your Code, you know it’s hard to find and fix them by hand, especially when the Project is a large one. This tool not only determines them, but it fixes them for you.

Requirements
PHP needs to be a minimum version of PHP 5.3.6.

Installation
These are the steps for installing php cs fixer on Windows

1.Select project folder under xampp/wamp or any other web-server.
2. Install composer.
Click on the https://getcomposer.org/Composer-Setup.exe
After downloading it install using the downloaded file .
3. Once Composer installed.
a. Go to project folder and make any empty file.
b. Name it as composer.json
c. Put this code there

{
  "Require-dev":{
       "squizlabs/php_codesniffer":"2.0.*"
                          }
}

d. This code will install PHP code sniffer dependency.
e. Run this command as composer updates.
Understanding PHP CS-FIXER with example

f. Open project folder (Whatever the folder where MVC file exists)
g. Open cmd
h. Type this command (phpcs –standard=PSR2 path/to/file)
i. See the list of errors and resolve
j. If you want to correct all errors in one step type below command

Phpcbf --standard=PSR2 path/to/file-w--no-patch

For example :-
Understanding PHP CS-FIXER with example
Understanding PHP CS-FIXER with example

This error occurs because index.php file is empty. Paste below code in this file.

<?php 
namespace vendor\autoloads;
class Hello
{
public function home()
{
return "hello";
}
}
?>

Save it.
Execute the previous command.

See the result
Understanding PHP CS-FIXER with example

Now fix these problem by running phpcs on command prompt
Understanding PHP CS-FIXER with example

After that you can check index.php code.

Example :-

<?php
namespace vendor\autoloads;

class Hello
{
    public function home()
    {
        return "hello";
    }
}

Leave a Reply

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