Skip to content

To make our code more pleasure to read and easier to maintain, follow this set of convention

Notifications You must be signed in to change notification settings

flip-id/php-code-convention

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

Flip.id PHP Code Style Guide

We created this guide in an attempt to make our code more readable and easier to maintain.

Coding Standard

Our coding standard will mostly follow PSR1 and PSR2 standard, so take your time to read that article thoroughly. For any addition or exception from the standard, we will provide the details in this guide.

Key Overview

  • Files MUST use only <?php and <?= tags.
  • Class names MUST be declared in PascalCase.
  • Class constants MUST be declared in all upper case with underscore separators.
  • Method names MUST be declared in camelCase.
  • Code MUST use 4 spaces for indenting, not tabs.
  • Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body.
  • Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body.

Addition

  • If possible, always use double quote "" to enclose a string to enable embedding a variable.
  • Embedded variable inside a string should be enclosed by a curly bracket: "this is a {$var}".
  • Method level variable MUST be declared in $under_score.
  • Class level properties MUST be declared in $under_score
  • Constant must go on the next line after class opening braces
  • Opening braces for method must go to the next line
  • Type hinting on parameter and return type should be used
  • Method return type hinting must go on the same line with method declaration
  • Nested expression on control structure condition is not allowed
  • If a control structure have more than 2 expression, separate each expression with a new line before the operator
  • Always use curly bracket for a control structure, except ternary operation
  • Always refer to the class directly when throwing an \Exception

Documentation

  • to-do

Exception

  • We're not so crazy about space actually. So as long as your code is easily readable, you can omit some strict space rules such as in the control structures section.
  • Opening braces for methods can EITHER go on the next line OR on the same line.

Tips

  • Text editor or IDE usually can help you with indentation so that every time you hit the Tab key it will actually give you 4 spaces.

OOP Tips

  • Do not overuse static method. Use it to refer to the object as a whole and not a specific instance.
<?php

//correct
Transaction::findById($id);
Transaction::getStatusMap();

//wrong
Transaction::cancelTransaction($id);
// should be
$transaction->cancel();

Flip.id Specific Rules

  • MySQL transaction should only be declared on the controller level, not model, to prevent any unwanted nested transaction. If you want to ensure that a method is only called within a MySQL transaction, you can check if there's a transaction running and throw an exception if there isn't.
  • Currently, nothing is allowed on the beforeSave method on any model.
  • Currently, only Log writing is allowed on the afterSave method on any model.

About

To make our code more pleasure to read and easier to maintain, follow this set of convention

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published