Child pages
  • Coding Standards

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. if, elseif, while, for: need a space between the if keyword and the parentheses ().

    Code Block
    borderStylesolid
    if (<condition>)
    
    while (<condition>)
    
  2. When a combination of if and else is used and both can return a value, the else statement has to be omitted.

    Code Block
    borderStylesolid
    if (<condition>)
    	return false;
    return true;
    
    Note
    titleRecommendation

    We recommend to use only one return statement per method/function.

  3. When a method/function returns a boolean and the current method/function's returned value depends on it, the if statement has to be avoided.

    Code Block
    borderStylesolid
    public aFirstMethod()
    {
    	return $this->aSecondMethod();
    }
    
  4. Tests must be grouped by entity.

    Code Block
    borderStylesolid
    if ($price AND&& !empty($price))
    	...
    if (!Validate::$myObject OR|| $myObject->id === NULL)
    	...
    

...

  1. Source code lines are limited to 120 150 characters wide.
  2. Functions and methods lines are limited to 80 characters. Functions must have a good reason to have an overly long name: keep it to the essential!

...

  1. It is forbidden to use a ternary into another ternary, such as echo ((true ? 'true' : false) ? 't' : 'f');.
  2. We recommend the use of && and || into your conditions instead of AND and OR: echo ('X' == 0 && 'X' == true).
  3. Please refrain from using reference parameters, such as:

    Code Block
    function is_ref_to(&$a, &$b) { ... }

...

You can download the PrestaShop code standard using SVNGithttps://github.com/PrestaShop/PrestaShop-norm-validator (you must perform this step before going any further with this tutorial).

...

Command line (Linux)

You do not have to use Eclipse PhpStorm to use PHP CodeSniffer, you can also install it so that it can be called from the command line.

  1. Install PEAR: http://pear.php.net/
    $> apt-get install php-pear
  2. Install PHP CodeSniffer in PEAR: http://pear.php.net/package/PHP_CodeSniffer
    $> pear install PHP_CodeSniffer
  3. Add the PrestaShop standard that you downloaded from SVN earlier, and place it in PHP CodeSniffer's "Standards" folder.
    $> svn cohttpgit clone https://svngithub.prestashop.com/branchesPrestaShop/PrestaShop-norm-validator //usr/share/php/PHP/CodeSniffer/Standards/Prestashop
  4. Set the Prestashop standard as the default one
    $> phpcs --config-set default_standard Prestashop

...