Combodo Coding Standards
This page is a brief description of the iTop coding conventions (mostly for PHP). The iTop coding conventions share a lot with the PSR-2 standard, but there are also quite a few significant differences, beware!
The source code files are encoded in UTF-8 (without a BOM at the beginning of the file).
PHP files must use the long PHP opening tag at the beginning of
the file <?php
but must not contain any PHP closing
tag. This prevents extra blank characters to be added by mistake at
the end of the file… where they will be appended to the output
produced by the page.
Variables naming
Variables, function parameters and object properties are named using a mixed case convention (i.e. all lowercase with an uppercase letter at the beginning of each word) and a prefix letter used as a type hint:
-
the
a
prefix is used for hash or arrays (PHP does not make any difference: each array is actually a hash) -
the
i
prefix is used of for integers -
the
o
prefix is used for PHP objects -
the
s
prefix is used for strings -
more rarely used:
f
for floating point numbers (i.e. non integer numbers),r
orh
can be used for resource handles.
Example:
<?php $iCount = 1; $sText = 'This is a text'; $oPage = new WebPage('test'); $fRank = 1.25; $hDir = opendir('/opt'); // No PHP closing tag
A variable with no prefix is supposed to be of mixed type (can be either a string, an array or an object)
Example:
public function Set($sAttCode, $value) { ... }
Allowed Exceptions
-
Local variables used for loop counters (e.g.
$i
,$j
) may have no prefix. -
In mathematical computations involving a lot of numeric variables it is acceptable to drop the prefix for such variables (e.g.
$x
,$y
,$xMax
…) -
For historical reasons, the WebPage class (and the related classes) as well as some parts inside MetaModel use a different convention: variables are all lowercase with an underscore character to separate words. Object properties (members) are prefixed using the
m_
prefix.
Functions naming
Functions and object methods are named using a mixed case convention (i.e. all lowercase with an uppercase letter at the beginning of each word). Even if PHP is not case sensitive for the function names, this useful to distinguish the functions which are part of the project from PHP's built-in functions which are generally written in lowercase. The function names are generally based on a verb expressing the action being performed.
Example:
function DisplayWelcomePopup(WebPage $oP) { // Code goes here }
White spaces and indentation
The indentation is made using tabulations (configure your editor to display a tabs as 4 characters wide)
Opening braces (for classes, functions, if/else, try/catch, switch) must go on the next line, and closing braces must go on the next line after the body. The closing brace must be vertically aligned with the corresponding opening brace.
Example:
class NiceWebPage extends WebPage { ... public function SetContentType($sContentType) { if($sContentType == '') { // Do something } else { // Do something different } } }
When writing function calls, the arguments supplied (between parentheses) immediately after the name of the function: there is no space between the function name and the arguments list. The comma in each arguments list is followed by a space.
Example:
$sValue = Utils::ReadParam('param1', 'default_value');
The binary operators (==
, +
,
&&
) are surrounded by spaces.
Exception: the concatenation operator .
is not
surrounded by spaces since this tends to keep lines more compact
and does not reduce the readability of the code when using
non-proportional fonts.
Example:
if (isset($aPerson[$this->iPersonId][0]) && ($aPerson[$this->iPersonId][0] != "")) { if ($this->sSynchronizeOrganization == 'yes') { $sName = $aPerson[$this->iPersonId]['first_name'].' '.$aPerson[$this->iPersonId]['last_name']; } }