...
if
,elseif
,while
,for
: need a space between theif
keyword and the parentheses()
.Code Block borderStyle solid if (<condition>) while (<condition>)
When a combination of
if
andelse
is used and both can return a value, theelse
statement has to be omitted.Code Block borderStyle solid if (<condition>) return false; return true;
Note title Recommendation We recommend to use only one
return
statement per method/function.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 borderStyle solid public aFirstMethod() { return $this->aSecondMethod(); }
Tests must be grouped by entity.
Code Block borderStyle solid if ($price AND&& !empty($price)) ... if (!Validate::$myObject OR|| $myObject->id === NULL) ...
...
- Source code lines are limited to 120 150 characters wide.
- 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!
...
- It is forbidden to use a ternary into another ternary, such as
echo ((true ? 'true' : false) ? 't' : 'f');
. - We recommend the use of
&&
and||
into your conditions instead ofAND
andOR
:echo ('X' == 0 && 'X' == true)
. Please refrain from using reference parameters, such as:
Code Block function is_ref_to(&$a, &$b) { ... }
...
You can download the PrestaShop code standard using SVNGit: https://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.
- Install PEAR: http://pear.php.net/
$> apt-get install php-pear
- Install PHP CodeSniffer in PEAR: http://pear.php.net/package/PHP_CodeSniffer
$> pear install PHP_CodeSniffer
- 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
- Set the Prestashop standard as the default one
$> phpcs --config-set default_standard Prestashop
...