.. _coding-style-label: Coding Style ============ Scope of style guidelines ------------------------- In order to keep the code consistent, please use the following conventions. These conventions are no judgement call on your coding abilities, but more of a style and look call. Performance and Readability --------------------------- * It is more important to be correct than to be fast. * It is more important to be maintainable than to be fast. * Fast code that is difficult to maintain is likely going to be looked down upon. Indentation and line length --------------------------- - 2 spaces - Max line width: 80 .. code-block:: php foo(bar() + 4)) { } For vars declaration place values on the same column .. code-block:: php 6 && $b == 17 && (foo($b) < 1)) { } Braces ------ If statements with or without else clauses are formatted like this: .. code-block:: php Including files --------------- Use ``require_once`` in order to include the file once and to raise warning if file does not exists: .. code-block:: php with double quotes, $a will be "Hello bar" if $foo = 'bar' // => with single quotes, $a will be "Hello $foo" $a = "Hello $foo"; //use single quotes for array keys $tab = array( 'lastname' => 'john', 'firstname' => 'doe' ); //Do not use concatenation to optimize PHP7 //note that you cannot use functions call in {} $a = "Hello {$tab['firstname']}"; //single quote translations $str = _('My string to translate'); //Double quote for special characters $html = "

One paragraph

\n

Another one

"; //single quote cases switch ($a) { case 'foo' : //use single quote here ... case 'bar' : ... } Files Format ------------ * UTF-8, LF - not CR LF .. _checking-standard-label: Checking standards ------------------ In order to check some standards are respected, we provide some custom `PHP CodeSniffer `_ rules. First clone the dev-tools that contains Fusiondirectory developement tools .. code-block:: bash git clone https://gitlab.fusiondirectory.org/fusiondirectory/dev-tools.git Then run the codesniffer rules from the top directory: .. code-block:: bash find . -type f -name '*.php' -o -name '*.inc' -exec phpcs --standard=../dev-tools/php-codesniffer-rules/FDStandard/ruleset.xml "{}" \; If the above command does not provide any output, then, all is OK :) An example error output would looks like: .. code-block:: bash FILE: fusiondirectory/fusiondirectory/include/class_ldap.inc -------------------------------------------------------------------------------- FOUND 9 ERROR(S) AFFECTING 4 LINE(S) -------------------------------------------------------------------------------- 260 | ERROR | Case breaking statement indented incorrectly; expected 10 | | spaces, found 8 802 | ERROR | Assignment blocks should have all assignment tokens on the same | | column 965 | ERROR | Expected 1 space before "?"; 0 found 965 | ERROR | Expected 1 space after "?"; 0 found 965 | ERROR | Expected 1 space before ":"; 0 found 965 | ERROR | Expected 1 space after ":"; 0 found 973 | ERROR | Expected 1 space before "?"; 0 found 973 | ERROR | Expected 1 space after "?"; 0 found 973 | ERROR | Expected 1 space before ":"; 0 found --------------------------------------------------------------------------------