When we write code, there’s one thing we need to focus beyond the results, and that’s the code consistency. This means you need to adopt conventions that would make all your project’s code like it was written by the same person. It doesn’t matter how many people were involved, you can’t differentiate personal styles. To adopt a code style and establish a style convention you must not cling to personal tastes but such really important things as maintainability, readability and re-usability.

Consistency is important even though one is writing open source code. The major task for the code is to locate bugs correctly and get to know how to resolved it. This is why, when writing anything for PrestaShop, be it a theme, a module or a core patch, you should follow the following guidelines.

PHP
Follow the following are the standers while writing php script in Prestashop

Variable names
Variable names should be written in english like in class, method and function names.
It should be readable to any one. Use lowercase letters, and separate words using underscores.
In PrestaShop using CamalCase naming convention is not standard practice.
Eg. $my_var. (standard)
   $MyVar. (non standard)
   visibility of variable does not affect its name: private $my_var.

Assignments
There should be a space between variable and operators.
$my_var = 25;
$x = $y;

Operators

  • For the following operator or their combination there should be a space between their left and right members.
    “+”, “-“, “*”, “/”, “=” and any combination of them (e.g. “+=”)
    $x += 56;
    $y + 10 ;
  • There should not have a space between its left and right members for “.”
    echo $x.$y;
  • There should be a space between its left and right members for “.=”
    $x .= ‘Test’ ;
  • While testing a boolean variable, directly use its value or prefixed value with an exclamation mark. avoid comparison operator (‘ = ‘).
    do not use like this
    if ($var == true) or if ($var == false)
    use like
    if ($var) or if ($var)

Statements

  • if , elseif, while, for statement
    There should be a space between the statement keyword and the parentheses().
    Ex: if () elseif ()
         while ()
          for ()
  • If if and else both can return a value, then else statement has to be omitted.
    If ()
    return false;
    return true;

Visibility

  • It is mandatory to define visibility each and every time even though it is a public method.
  • Properties of method should be as: visibility static function functionName()
    private static function foo()

Method / Function names

  • It is mandatory to use CamelCase in method and function names .

    Keep in mind that always begin with a lowercase character and each following words must begin with an uppercase character.

    public function myExampleMethodWithALotOfWordsInItsName()

  • Method and function names must be defined as explicit, so function names such as b() or ef()are completely prohibited.

Enumeration

  • Commas have to be followed only and not to be preceded by a space.
    protected function myProtectedMethod($arg1, $arg2, $arg3 = null)

Objects / Classes

  • Object name must be singular
    Ex : class Order
  • For class name one must follow CamelCase convention practice, except that the first letter is uppercase
    Eg : class MyBeautifulClass

Constants

  • Constant names must be written in uppercase, except for “true”, “false” and “null” which must be lowercase.
  • Constant should have been prefixed with “PS_” .
    define(‘PS_DEBUG’, 1);
    define(‘PS_MODULE_NAME_DEBUG’, 1);
  • Use only alphabetical characters and “_” in while defining constant name.

Keywords
All keywords should be in lower case : null, if , case, echo , as etc.

Configuration variables
All configuration variables should be in lower case : null, if , case, echo , as etc.

Strings
Write Strings always in simple quotes, double quoting is non standard.
echo ‘Debug’; // standard
echo “Debug”; // non standard

Comments

  • Only the “//” comment tag is allowed Inside functions and methods.
  • It is mandatory to give a space after the “//” comment marker.
    // My comment
  • The “//” comment marker is permitted at the end of a code line.
    $x = 27 + 33; // A comment inside my example function
  • “/*” and “*/” comment markers are allowed only outside of functions and methods. /* This method is required for compatibility issues */
    public function foo()
    {
    // statement

    }
  • Before the declaration of the method a phpDoc comment block is mandatory.
    /**
    * Return field value if possible (both classical and multilingual fields)
    *
    * Case 1: Return value if present in $_POST / $_GET
    * Case 2: Return object value
    *
    * @param object $obj Object
    * @param string $key Field name
    * @param integer $id_lang Language id (optional)
    * @return string
    */
    protected function getFieldValue($obj, $key, $id_lang = NULL)
    		
    

Return values

  • Except when it deals with a composed expression, the return statement does not need brackets. return false; return $result; return ($x + $y); return (x() - y());
  • The return statement can be used to break out of a function. Return;

Call
erforming a function call preceded by a "@" is prohibited but always should be taken care of function/method call with login/password or path arguments.

/ In the following example, we put a @ for security reasons @mysql_connect(...);

Tags

  • There must be an empty line after the PHP opening tag.
  • At the end of the file, PHP closing tag is prohibited.

Indentation

  • The tabulation character ("\t") is the only allowed for indentation.
  • Each indentation level must be represented by a single tabulation character.

Array

  • Array should be defined like Array(27, 33, 52);,it must not be followed by a space.
  • When complex data is inside an array, the indentation has to be as follows.
    $x = array(
         46 => $y,
         $z => 'foo',
         $p=> array(27, 33, 52),
         $q => array(
               0 => 'zero',
               1 => $one
        )
    );
    

Block

  • Braces are forbidden when they only define one instruction or a combination of statements.
    if (!$result)
    return false;

Security

  • All the data entered by users has to be cast.
    $data = Tools::getValue('name');
    $myObject->street_number = (int)Tools::getValue('street_number');
    Note: Avoid getValue() , it does not protect your code from hacking attept.
  • When Array or Objects are recived, all method and function's parameter must be typed
    public myMethod(Array $var1, $var2, Object $var3)
  • For all other parameters, they have to be cast each time they are used, except when they are sent to other methods/functions.
    protected myProtectedMethod($id, $text, $price)
    {
         $this->id = (int)$id;
         $this->price = (float)$price;
        $this->callMethod($id, $price);
     }
    

Limitations

  • There is a limitation set to source code lines to 150 characters wide.
  • Only if is essential functions to have an overly long name
  • Functions and methods lines are limited to 80 characters.

Other

  • use a ternary into another ternary, such as echo (( ? 'true' : false) ? 't' : 'f'), it is strictly prohibited.
  • use && and || into your conditions instead of AND AND and OR.
    Eg: ('X' == 0 && 'X' == true).

SQL
Follow the following are the standers while writing sql in Prestashop

Table names

  • Table names must begin with the "_DB_PREFIX_" prefix.
  • Table names must have the same name as the object they reflect: "ps_order".
  • Table names should be singular : "ps_order".
  • Language data have to be stored in a table with the "_lang" suffix (ps_product_lang) same like the object's table.

SQL query

  • SQL command keyword must be written in uppercase.
    Eg : SELECT `firstname` FROM `'._DB_PREFIX_.'customer`.
  • SQL field names and table names should be write in Back quotes ("`").
    Eg: SELECT p.`id_product`, c.`firstname` FROM `'._DB_PREFIX_.'product` p, `'._DB_PREFIX_.'customer` c
  • Table aliases must be lowercase and named by first letter of each word.
  • If table aliases conflicts then the second character has to be also used in the name.
    Ex: SELECT ca.`id_product`, cu.`firstname` FROM `'._DB_PREFIX_.'cart` ca,`'._DB_PREFIX_.'customer` cu
  • Each clause should be in a new line.
    Ex: $query = 'SELECT pl.`name`
    FROM `'._DB_PREFIX_.'product_lang` pl
    WHERE pl.`id_product` = 1';
    You can not to make a JOIN in a WHERE clause.