Development

How to keep PHP coding standards using Git

In the work environment of web developers, many disciplined teams keep awesome code in their repositories. In the same way, some start coding PHP without knowing the right standards and precautions. Recently, it’s become more important since dev communities are growing, and collaboration plays a crucial role in remote development teams. So, we need to facilitate PHP coding standards for others to achieve high-quality code.

It’s true; we might have all been in a position where we all want the code to work. But, as a code writer, it is very important to keep it simple, clean, and, foremost, organized. So, let’s review some of the best practices for coding PHP.

Table of contents

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 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, it is too long and complicated. In this blog, I want to share how to do it easily. Keep reading and discover step by step how to configure your PSR standards automatically with a simple “commit.”

PHP vs Node.js: Read the full comparison.

PSR1 and PSR2

Both are coding standards used to maintain a consistent style, such as 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 and other 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 for fixing small mistakes or creating 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 additional commands; after the installation, you have to “commit” your changes as you have done so far.

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

Comparing Symfony vs Laravel: clear the debate.

Are you considering hiring PHP developers? Check out this comprehensive guide.

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: If you like, you can check the configuration process in detail before running them.
– 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 work?

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 be applied to 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 omitted details will be corrected automatically, giving us a “< br >.” If you wish, you can fork this repository and create your version or collaborate on this one, making suggestions to improve the process.

With our PHP Development Services, our team at ClickIT strives to be at the forefront and meet the highest development standards; you can be sure that your application will be in good hands with us.

PHP Coding Standards FAQs

Why are coding standards important in PHP development?

Coding standards are crucial in PHP development for several reasons. They enhance code readability, making it easier for developers to understand and maintain the code. Consistent coding practices across a project or team ensure uniformity, reduce errors, and facilitate collaboration. Adhering to standards also improves code quality, making it more robust and scalable.

Which PHP coding standards should I follow, and are there any widely recognized standards in the PHP community?

Some widely recognized PHP coding standards include PSR-1 and PSR-2, established by the PHP-FIG (Framework Interoperability Group). These standards focus on basic coding standards and coding style and following them helps create a consistent and interoperable codebase, promoting best practices and collaboration within the PHP community.

How can I enforce PHP coding standards in my projects?

Enforcing PHP coding standards is made convenient with tools like PHP_CodeSniffer and PHP-CS-Fixer. PHP_CodeSniffer checks code against predefined standards, including PSR standards, identifying violations. On the other hand, PHP-CS-Fixer not only detects issues but automatically corrects many of them. Integrating these tools into your development workflow, such as in pre-commit hooks or continuous integration processes, helps catch and fix coding standard violations early in the development process. Many modern IDEs also support automatic code formatting using PHP-CS-Fixer, ensuring consistent adherence to coding standards during development.

Disqus Comments Loading...
Published by
DevOps Guy
Tags: codegitphp

Recent Posts

Web Application Architecture: The Latest Guide 2024

When a user logs on to this desktop/laptop or mobile, opens a browser and types…

1 week ago

Low-Code Development for Business Success

Low-code development is great news for businesses, reducing time-to-market for apps, and allocating costs more…

1 week ago

PHP Latest Versions Guide | Video

So, have you caught wind of the latest PHP update? If you not, this PHP…

3 weeks ago

ECS vs EC2: Choosing the Right AWS Service for Your Workloads

When it comes to AWS ECS vs EC2, the choice boils down to your specific…

4 weeks ago

Netflix Architecture | A Look Into Its System Architecture

Ever wondered how Netflix keeps you glued to your screen with uninterrupted streaming bliss? Netflix…

1 month ago

Snowflake vs Redshift: Key Differences

In today's busy world, where information is important, handling data well is crucial for success.…

1 month ago