DEV Community

Cover image for Writing Code with Standards and generate report on PreCommit : PHP / Laravel Project
SyedAsadRazaDevops
SyedAsadRazaDevops

Posted on

Writing Code with Standards and generate report on PreCommit : PHP / Laravel Project

When we start the project, we always say “we will use proper coding standard and let’s do the code review”. Every time, when someone makes a pull request, we have to comment about coding standard errors all the time.

What is Code Efficiency?

Code efficiency is a broad term used to depict the reliability, speed and programming methodology used in developing codes for an application. Code efficiency is directly linked with algorithmic efficiency and the speed of runtime execution for software. It is the key element in ensuring high performance.

Why we need Code Efficiency?

The goal of code efficiency is to reduce resource consumption and completion time as much as possible with minimal risk to the business or operating environment. The software product quality can be accessed and evaluated with the help of the efficiency of the code used.

How To Do?

  1. Config caching
  2. Routes caching
  3. Remove Unused Service
  4. Class map optimization
  5. Optimizing the composer autoload
  6. Limit the Use of Plugins
  7. JIT Compiler
  8. Choose a faster cache and session driver
  9. Cache the queries results
  10. Use “Eager Loading” of Your Data
  11. Precompile Assets
  12. Use CDN for delivering your static assets

Stop wasting time, let’s PHP_CodeSniffer do for you.

What’s the benefit of using PHP CodeSniffer?

PHP CodeSniffer shows you coding standard errors and phpcbf help fix you basic coding standard problems. By using PHP CodeSniffer, you don’t have to worry that much for the coding standard. When someone new comes to join the team, it’s much easier for him/her and for the rest of the team as well.

Setup PHP CodeSniffer

First, go to your terminal and install PHP CodeSniffer as your development package with the composer in your Laravel project. (You can use PHP CodeSniffer outside of PHP project as well. It’s for PHP Projects. But, we plan to use with Laravel.)

composer require --dev squizlabs/php_codesniffer

After you finish setting up, create a new simple controller

<?php namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HelloController extends Controller{
}
Enter fullscreen mode Exit fullscreen mode

And run phpcs on your project like
./vendor/bin/phpcs app/Http/Controllers/HelloController.php

You got PHPCS errors. There is a message which said PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY Which means, phpcbf can fix that bug you. Let’s run phpcbf on your project
example

Setup PHPCS Config

After you understand what’s phpcs. Now, let’s try to setup phpcs for our Laravel project. Create phpcs.xml on your Laravel document root and paste the following config

<?xml version="1.0"?>
<ruleset name="PSR2">    
<description>The PSR2 coding standard.</description>    
<rule ref="PSR2"/>     
<file>app/</file>     
<exclude-pattern>vendor</exclude-pattern>    
<exclude-pattern>resources</exclude-pattern>    
<exclude-pattern>database/</exclude-pattern>    
<exclude-pattern>storage/</exclude-pattern>    
<exclude-pattern>node_modules/</exclude-pattern>
</ruleset>
Enter fullscreen mode Exit fullscreen mode

Since Laravel uses PSR2 coding stands we use PSR2 coding stand and excluded vendor, resoruces, database, storage, node_modules directory.

Now, when you run phpcs on your project directory it will check with PSR2 coding standard and will show the errors to fix some of those errors, you just run phpcbf and it will help you fix. The rest which can’t fix from phpcbf need to be fixed by you or your teammate. Like that, everyone is happy to code on the PSR2 coding standard project.

Before implementation

  1. Line of code is greater than the standard line of code
  2. Code looks messy
  3. No comments which describe what this piece of code does
  4. Code is not reusable
  5. Extra code is written
  6. A lot of code is commented on which is not used in the system
  7. Parameters are not defined (what does it do)
  8. Code is not optimized
  9. Use of code spirited everywhere
  10. Code readability is not so good

After implementation

1. Line of code 

  • Before: It takes 160 lines to do a proper implementation. 
  • After: It takes 98 lines to implement

2. Messy Code 

  • Before: The code is not easily readable 
  • After: The code is easily readable 

3. Comments 

  • Before: Comments are missing  
  • After: Comments with API and functionality of code added 

4. Reusable 

  • Before: Code can’t be used anywhere when you need 
  • After: Code can be used whenever or wherever you want
     

    5. Extra Code 

  • Before: Non-used code written 

  • After: Extra code removed 

6. Commented Code 

  • Before: Commented code written
  • After: Commented Code removed which is not usable 

7. Parameters 

  • Before: Function parameters are defined but do not describe 
  • After: Function parameters are described briefly 

8. Optimization 

  • Before: Code is not optimized
  • After: Code is optimized 

9. Spirited Code 

  • Before: Code is spirited in diff.. files 
  • After: Arrange code under a single file and code finding is easy 

10. Readability 

  • Before: Code looks messy 
  • After: The code is neat & clean  

Top comments (0)