How to keep PHP coding standards using Git (2018) | Clickittech

How to keep PHP coding standards using Git (2018)

How to keep PHP coding standards using Git

During my experience as a web developer, I have had the opportunity to work with disciplined teams that kept awesome code in their repositories, so as I have worked with groups that have no precautions when writing their code either by carelessness or just by laziness.

I guess we all have done that and I get it, we just want the code to work but, as a code writer, is very important to keep it simple, clean and foremost organized. Moreover, in these recent years where collaboration plays a crucial role in remote development teams; we need to facilitate the work for others to achieve a high-quality code.

I'll be focusing on PHP this time since it's the one I started to use without knowing about PHP coding standards. Naturally, I used to code without them and then realized the huge difference in saving time and avoiding simple mistakes.

Although PHP has never truly had a uniform standard for writing code; there is a community called FriendsOfPHP that focuses on keeping standards up to date and facilitate coding activities.

This time I’ll talk about PHP-CS-Fixer, this tool has made my day-to-day much simpler. It is well known in the PHP developer community but no one talks about how to configure it and when they do, is too long and complicated. In this blog, I want to share how to do it in a simple and easy way. Keep reading and discover step by step how to configure your PSR standards automatically with a simple “commit”.

PSR1 and PSR2

Both are coding standards used to keep a consistent style, like good readability and maintainability; so that debugging, modifying or adding new features is much more comfortable for the developer.

In this case, we’ll deal only with the standards without digging into the implementation of some of the design patterns, as well as another good coding practices that require a high knowledge of the pattern used.

Before continuing, we have to make clear that this is a supportive tool to fix small mistakes or to create the habit of following the standards, so take care that your team continues practicing and that this does not generate a dependency on them.

PHP Coding Standards Configuration

I made a script using a Git Webhook that configures the tool making it unnecessary to make configurations or perform additionals commands, that is, after the installation you just have to "commit" your changes as you have done so far. It works great for projects like Symfony, Laravel, CI, to mention a few. All of them use these standards and you can apply it to other projects, even if they are not based on any framework and must follow the standard.

It works great for projects like Symfony, Laravel, CI, to mention a few. All of them use these standards and you can apply it to other projects, even if they are not based on any framework and must follow the standard.

tickettogo-case-study

See how we helped companies like yours to develop digital products.

Apply PSR1 and PSR2 with my Git Webhook

1. Execute the following command in your terminal:
git clone https://github.com/AgusRdz/php-fixer-pre-commit

This folder contains 3 essential files:
installer.sh: Before running them if you like, you can check the configuration process in detail.
config-rules.dist: This is the rule file to carry out the standardization with PSR1 and PSR2.
pre-commit: Instructions that will be executed to validate and apply the rules before the end of the commit.

2. Access the folder with:

cd php-fixer-pre-commit

3. Execute the installer.sh file as superuser

sudo bash installer.sh

4. Access the repository you want to apply these standards, for example:

cd ~/Documents/Projects/my-awesome-project-php

git init #if it has not been initialized

5. Execute the following command

pre-commit-init

How does it works?

Before the commit we can have code like the following:

< ?php namespace App;
use XDependency;
use ADependency;
use YDependency;
use BDependency;

class MyClass{
  public function CalculatePrice () {
      $a = 9 ;
     $b= 10 ;
   return $a + $b;
  }
	}
?>

And then the code will have been applied PSR1 and PSR2, having a code like the following.

< ?php

namespace App;

use ADependency;
use BDependency;
use XDependency;
use YDependency;

class MyClass
{
    public function CalculatePrice()
    {
        $a = 9;
        $b = 10;

        return $a + $b;
    }
} 

As a developer, keeping the code organized is one of the most difficult tasks because sometimes it consumes a lot of time; but it's vital to create amazing and robust applications.

Now that you have these tools at your fingertips you can be more productive because all the small details that you omitted will be corrected automatically giving us a “< br >.” If you wish, you can fork this repository and create your version or collaborate in this one making suggestions to improve the process.

At ClickIt we strive to be at the forefront and meet the highest standards of development, you can be sure that your application will be in good hands with us.

We turn ideas into code.

Forget about endless development processes and let us do the hard work.

Tagged under: