Language and development rules: Difference between revisions

Jump to navigation Jump to search
Line 24: Line 24:
* Dolibarr is written with PHP and supports all PHP version higher than {{PHPMinVersion}}. All files must end with extension .php
* Dolibarr is written with PHP and supports all PHP version higher than {{PHPMinVersion}}. All files must end with extension .php


* Usage of superglobals PHP variables must use dedicated operators $_COOKIES, $_SERVER, $_ENV but use dolibarr function GETPOST() to get content of $_GET or $_POST..
* Usage of superglobals PHP variables must use dedicated operators $_COOKIES, $_SERVER, $_ENV but use Dolibarr function GETPOST() to get content of $_GET or $_POST..
Other operators ($HTTP_SERVER_GET, ...) are now deprecated inside PHP, so they must no more be used. Like that code will works alos if option '''register_long_arrays''' is to off.
Other operators ($HTTP_SERVER_GET, ...) are now deprecated inside PHP, so they must no more be used. Like that code will works alos if option '''register_long_arrays''' is to off.
Moreover, the code must works when PHP option '''register_globals''' is off (recommanded by PHP). It must works the same way that when option '''register_globals''' is on (by default on a lot of installations).
Moreover, the code must works when PHP option '''register_globals''' is off (recommended by PHP). It must works the same way that when option '''register_globals''' is on (by default on a lot of installations).


* Do not use '''PHP_SELF'''. Use instead $_SERVER["PHP_SELF"].
* Do not use '''PHP_SELF'''. Use instead $_SERVER["PHP_SELF"].
Line 45: Line 45:
</source>
</source>


* Comments must use the C syntax, ie a double antislash for a comment on one line and a slash-star to open a bloc for several lines
* Comments must use the C syntax, ie a double anti-slash for a comment on one line and a slash-star to open a bloc for several lines
<source lang="php">
<source lang="php">
/* Bloc of comment
/* Bloc of comment
Line 63: Line 63:
</source>
</source>


* Functions must return a value strictly higher than 0 if successfull and strictly lower than 0 if error.
* Functions must return a value strictly higher than 0 if successful and strictly lower than 0 if error.


* No dead code (code never used) into Dolibarr core code (code used by external modules only must be included into external modules).
* No dead code (code never used) into Dolibarr core code (code used by external modules only must be included into external modules).
Line 73: Line 73:
** Smart tags PHP are not used. PHP code section must start with '''<?php'''
** Smart tags PHP are not used. PHP code section must start with '''<?php'''
** But there is an exception of length of line: We accept more than 80 characters per line. 80 is really to low. In most cases, we never go over 80, but sometimes (when declaring a list of reference arrays, like into module descriptor files), it is better to have long lines instead of long page with code content that is just data declaration and does not contains any logic.
** But there is an exception of length of line: We accept more than 80 characters per line. 80 is really to low. In most cases, we never go over 80, but sometimes (when declaring a list of reference arrays, like into module descriptor files), it is better to have long lines instead of long page with code content that is just data declaration and does not contains any logic.
** The other exception is that we don't replace the tab with space. This make lot of editor crazy and break some autoformat features broken.
** The other exception is that we don't replace the tab with space. This make lot of editor crazy and break some auto-format features broken.
You can use the file dev/codesniffer/ruleset.xml as rule file to control coding style with PHPCodeSniffer.
You can use the file dev/codesniffer/ruleset.xml as rule file to control coding style with PHPCodeSniffer.


Navigation menu