Child pages
  • Coding Standards

Versions Compared

Key

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

...

  1. Keywords must be written in uppercase.

    Code Block
    borderStylesolid
    SELECT `firstname`
    FROM `'._DB_PREFIX_.'customer`
    
  2. Back quotes ("`") must be used around SQL field names and table names.

    Code Block
    borderStylesolid
    SELECT p.`foo`, c.`bar`
    FROM `'._DB_PREFIX_.'product` p, `'._DB_PREFIX_.'customer` c
    
  3. Table aliases have to be named by taking the first letter of each word, and must be lowercase.

    Code Block
    borderStylesolid
    SELECT p.`id_product`, pl.`name`
    FROM `'._DB_PREFIX_.'product` p
    NATURAL JOIN `'._DB_PREFIX_.'product_lang` pl
    
  4. When conflicts between table aliases occur, the second character has to be also used in the name.

    Code Block
    borderStylesolid
    SELECT ca.`id_product`, cu.`firstname`
    FROM `'._DB_PREFIX_.'cart` ca, `'._DB_PREFIX_.'customer` cu
    
  5. Indentation has to be done for each clause.

    Code Block
    borderStylesolid
    $query = 'SELECT pl.`name`
    FROM `'._DB_PREFIX_.'product_lang` pl
    WHERE pl.`id_product` = 17';
    
  6. It is forbidden to make a JOIN in a WHERE clause.

Installing the code validator (PHP CodeSniffer)

This is a brief tutorial on how to install a code validator on your PC and use it to validate your files. The code validator uses PHP CodeSniffer, which is a PEAR package (http://pear.php.net/package/PHP_CodeSniffer/). The PrestaShop code standard was created specifically for CodeSniffer, using many rules taken from existing standards, with added customized rules in order to better fit our project.

You can download the PrestaShop code standard using SVN: http://svn.prestashop.com/branches/norm/ (you must perform this step before going any further with this tutorial).

Info
To properly install the coding standard, you must rename it to "Prestashop" (and not "norm").
So that it is recognized as a basic standard, it must be placed in the CodeSniffer's  /Standards folder

PhpStorm integration

If you use PhpStorm (http://www.jetbrains.com/phpstorm/), follow these steps:

  1. Go to Settings -> Inspection -> PHP -> PHP Code Sniffer.
  2. Set the path to the phpcs executable.
  3. Set the coding standard as "PrestaShop" (which is only available if you did put in CodeSniffer's /Standards folder).

Eclipse integration

Quick links:

If you use Eclipse (http://www.eclipse.org/), you can integrate code validation within the text editor using a plugin, which is very easy to install: http://www.phpsrc.org/projects/pti/wiki/Installation.

...

Tip

If the file does not automatically validate, as it should, you can configure this in the "Preferences" menu, "Validation" option. Otherwise, just right-click on the folder/file in the file-tree, and choose "PHP Tools" in the contextual menu (which you can also be set as a shortcut).

Integrating the program to Eclipse's console (optional)

  1. Click on the "External tools" button in the icon bar (a green arrow pointing at a small red folder).
  2. Click on the "External tools configuration" tab.
  3. Double-click on "Program" in order to create a configuration:
    1. Location: path to the phpcs program (or phpcs.bat for Windows users).
    2. Arguments: the arguments for the command line, for instance --standard=Prestashop.

Integration to vim

Several plugins are available online. For instance, you can use this one: https://github.com/bpearson/vim-phpcs/blob/master/plugin/phpcs.vim
Put in your ~/.vim/plugin folder.

You can add two shortcuts (for instance, F9 to display everything and Ctrl+F9 to hide warnings) in your .vimrc file in normal and insert mode :

Code Block
nmap <C-F9>:CodeSniffErrorOnly<CR>
imap <C-F9> <Esc>:CodeSniffErrorOnly<CR>
nmap <F9>:CodeSniff<CR>
imap <F9> <Esc>:CodeSniff<CR>a		

Command line (Linux)

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

...

If you have already manually installed PHP CodeSniffer, the program should be in PEAR's "/scripts" folder.

Note

Windows users: although the phpcs.bat file should be in that /scripts folder, you might have to edit it in order for it to work properly (replace the paths with yours):

Code Block
path/to/php.exe -d auto_apprend_file="" -d auto_prepend_file -d include_path="path/to/PEAR/" path/to/pear/scripts/phpcs

Integrating the program to Eclipse's console (optional)

  1. Click on the "External tools" button in the icon bar (a green arrow pointing at a small red folder).
  2. Click on the "External tools configuration" tab.
  3. Double-click on "Program" in order to create a configuration:
    1. Location: path to the phpcs program (or phpcs.bat for Windows users).
    2. Arguments: the arguments for the command line, for instance --standard=Prestashop.