Code conventions

Francesco Lodolo edited this page Mar 14, 2016 · 7 revisions

Our minimum requirement for PHP is PHP 5.6, our production server runs Debian 8.

Generally speaking we try to follow the PSR standards.

Coding Style

A few key points to take into account:

  • Indentation is 4 spaces, never use tabs.
  • Remove spaces at end of lines or blank lines.
  • Variable names use underscore notation $locale_code.
  • Use meaningful variable names.
  • All classes must be namespaced.
  • Method and function names use camelCase: getLanguage().
  • Add a single space after each comma delimiter.
  • Add a single space around operators (==, &&, …).
  • Add a comma after each array item in a multi-line array, even after the last one.
  • Add a blank line before return statements, unless the return is alone inside a statement-group (like an if statement).
  • Use braces to indicate control structure body regardless of the number of statements it contains.

Ternary Operator

If the code is too long to fit on one line, use the following form:

$variable = condition
    ? action_if_true
    : action_if_false;

Commenting Style

  • Never use #.
  • Use // only for one line comments to explain logic in the code.
  • Start the comment with uppercase, don't end with a period (exception of multiline comments, where a period is needed for ending the comment).
  • Use this for general documentation and longer logic explanations (note the alignment of the closing */):
/*
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
    incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
    exercitation ullamco laboris nisi ut ali.
*/
  • If your comment explains a logic block or a loop (if, for, foreach, switch…), put it BEFORE the block and not inside.
  • Follow the rules for docblocks, they should look like that:
    /**
     * Get the list of parameters for an API call.
     *
     * @param  string $parameters The list of parameters from the URI
     * @return array  All the main parameters for the query
     */
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.