...
Keywords must be written in uppercase.
Code Block borderStyle solid SELECT `firstname` FROM `'._DB_PREFIX_.'customer`
Back quotes ("
`
") must be used around SQL field names and table names.Code Block borderStyle solid SELECT p.`foo`, c.`bar` FROM `'._DB_PREFIX_.'product` p, `'._DB_PREFIX_.'customer` c
Table aliases have to be named by taking the first letter of each word, and must be lowercase.
Code Block borderStyle solid SELECT p.`id_product`, pl.`name` FROM `'._DB_PREFIX_.'product` p NATURAL JOIN `'._DB_PREFIX_.'product_lang` pl
When conflicts between table aliases occur, the second character has to be also used in the name.
Code Block borderStyle solid SELECT ca.`id_product`, cu.`firstname` FROM `'._DB_PREFIX_.'cart` ca, `'._DB_PREFIX_.'customer` cu
Indentation has to be done for each clause.
Code Block borderStyle solid $query = 'SELECT pl.`name` FROM `'._DB_PREFIX_.'product_lang` pl WHERE pl.`id_product` = 17';
- It is forbidden to make a
JOIN
in aWHERE
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:
- Go to Settings -> Inspection -> PHP -> PHP Code Sniffer.
- Set the path to the
phpcs
executable. - Set the coding standard as "PrestaShop" (which is only available if you did put in CodeSniffer's /Standards folder).
Eclipse integration
Quick links:
- Official installation guide: http://www.phpsrc.org/projects/pti/wiki/Installation
- Official configuration guide: http://www.phpsrc.org/projects/pti-php-codesniffer/wiki/Configuration
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)
- Click on the "External tools" button in the icon bar (a green arrow pointing at a small red folder).
- Click on the "External tools configuration" tab.
- Double-click on "Program" in order to create a configuration:
- Location: path to the
phpcs
program (orphpcs.bat
for Windows users). - Arguments: the arguments for the command line, for instance
--standard=Prestashop
.
- Location: path to the
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
|
Integrating the program to Eclipse's console (optional)
- Click on the "External tools" button in the icon bar (a green arrow pointing at a small red folder).
- Click on the "External tools configuration" tab.
- Double-click on "Program" in order to create a configuration:
- Location: path to the
phpcs
program (orphpcs.bat
for Windows users). - Arguments: the arguments for the command line, for instance
--standard=Prestashop
.
- Location: path to the