CodingStandards » History » Version 43

moyo, 02/26/2009 06:32 PM

1 1 jmd
== Indentation ==
2 1 jmd
3 29 moyo
One tabulation OR X spaces
4 1 jmd
5 2 jmd
{{{
6 1 jmd
<?php
7 1 jmd
// base level
8 1 jmd
    // level 1
9 1 jmd
        // level 2
10 1 jmd
    // level 1
11 1 jmd
// base level
12 1 jmd
?> 
13 2 jmd
}}}
14 1 jmd
15 38 jmd
STATE : accepted : 3 spaces
16 1 jmd
17 26 jmd
TODO  : review all code
18 26 jmd
19 42 remi
Remi: tabs is better than spaces, could be use with all indent values
20 42 remi
21 43 moyo
MoYo: this is typically the problem. Tabs are not always displayed using the number of spaces. So, source code can be unreadble using several editors. Then using it dev use spaces instead of tabs and it make source code unhomogenous.
22 43 moyo
23 1 jmd
== Control structures ==
24 1 jmd
25 10 moyo
Multiple conditions in several idented lines
26 1 jmd
27 7 jmd
{{{
28 7 jmd
<?php
29 7 jmd
if ($test1){
30 7 jmd
     for ($i=0;$i<$end;$i++){
31 7 jmd
          echo "test ".( $i<10 ? "0$i" : $i )."<br>";
32 7 jmd
     }
33 7 jmd
}
34 8 jmd
35 10 moyo
if ($a==$b
36 22 moyo
    | ($c==$d
37 22 moyo
       && $e==$f)){
38 10 moyo
39 10 moyo
}
40 10 moyo
41 8 jmd
switch ($test2){
42 8 jmd
     case 1 :
43 8 jmd
          echo "Case 1";
44 8 jmd
          break;
45 8 jmd
     case 2 :
46 8 jmd
          echo "Case 2";
47 8 jmd
          break;
48 8 jmd
     default :
49 8 jmd
          echo "Default Case";
50 8 jmd
          break;
51 8 jmd
}
52 8 jmd
53 7 jmd
?> 
54 1 jmd
}}}
55 7 jmd
56 26 jmd
STATE : accepted
57 1 jmd
58 26 jmd
TODO  : review all code
59 26 jmd
60 42 remi
Remi: space before ( and afer )
61 42 remi
62 42 remi
{{{
63 42 remi
if ($toto) {
64 42 remi
   ...
65 42 remi
} else if {
66 42 remi
   ...
67 42 remi
} else {
68 42 remi
   ...
69 42 remi
}
70 42 remi
}}}
71 42 remi
72 42 remi
Remi: sould comment if closing } more than xx line avay from opening {  (p.e. 20 lines ~ 1 screen)
73 42 remi
Remi: a comment in a switch case without break (not notice it's not a mistake)
74 42 remi
75 1 jmd
== Including files ==
76 1 jmd
 
77 16 moyo
include_once in order to include the file once and to raise warning if file does not exists
78 1 jmd
79 12 moyo
{{{
80 20 jmd
include_once(GLPI_ROOT."/inc/includes.php");
81 1 jmd
}}}
82 1 jmd
83 26 jmd
STATE : accepted
84 26 jmd
85 19 moyo
TODO : require_once , include, require -> include_once
86 1 jmd
87 1 jmd
== PHP tags ==
88 1 jmd
89 2 jmd
Short tag not allowed. Use complet tags.
90 2 jmd
91 2 jmd
{{{
92 2 jmd
<?php
93 2 jmd
94 2 jmd
// code
95 2 jmd
96 2 jmd
?>
97 1 jmd
}}}
98 1 jmd
99 26 jmd
STATE : accepted
100 26 jmd
101 19 moyo
TODO : nothing
102 19 moyo
103 1 jmd
== Functions ==
104 1 jmd
105 3 jmd
Function names should be written in camelBack, for example:
106 1 jmd
107 3 jmd
{{{
108 3 jmd
<?php
109 39 moyo
function userName(){
110 3 jmd
}
111 1 jmd
?>
112 1 jmd
}}}
113 1 jmd
114 26 jmd
STATE : accepted
115 26 jmd
116 19 moyo
TODO : check all functions
117 19 moyo
118 42 remi
Remi: no space between function name and ( for declaration and call
119 42 remi
120 43 moyo
MoYo: not logical comparing to conditionnal structure.
121 43 moyo
122 1 jmd
== Class ==
123 1 jmd
124 4 jmd
Class names should be written in CamelCase, for example:
125 4 jmd
126 1 jmd
{{{
127 4 jmd
<?php
128 39 moyo
class ExampleAuthentification{
129 4 jmd
}
130 1 jmd
?>
131 1 jmd
}}}
132 1 jmd
133 26 jmd
STATE : accepted
134 26 jmd
135 19 moyo
TODO : check all classes
136 19 moyo
137 5 jmd
== Variables ==
138 5 jmd
139 9 jmd
Variable names should be as descriptive and short as possible. 
140 17 moyo
141 9 jmd
Normal variables should be written in lower case. In case of multiple words use the _ separator.
142 17 moyo
143 9 jmd
Global variables should be written in UPPER case. In case of multiple words use the _ separator.
144 5 jmd
145 5 jmd
Example:
146 5 jmd
147 5 jmd
{{{
148 5 jmd
<?php
149 5 jmd
$user = 'glpi';
150 1 jmd
$users = array('glpi', 'glpi2', 'glpi3');
151 9 jmd
$users_groups = array('glpi', 'glpi2', 'glpi3');
152 9 jmd
153 9 jmd
$CFG_GLPI=array();
154 1 jmd
155 5 jmd
?>
156 5 jmd
157 1 jmd
}}}
158 1 jmd
159 26 jmd
STATE : accepted
160 26 jmd
161 19 moyo
TODO :
162 19 moyo
163 21 moyo
globals : $cfg_glpi $db $phproot $HTMLRel $lang (glpi_trad) $cfg_glpi_plugins $dbocs $pdf_ (maybe not real global) $plugins_hooks -> DONE
164 19 moyo
165 19 moyo
+ check some global variables need to be passed as arguments
166 19 moyo
167 6 jmd
== Variable types ==
168 5 jmd
169 5 jmd
170 8 jmd
Variable types for use in DocBlocks for Doxygen:
171 1 jmd
172 5 jmd
||Type||	Description||
173 5 jmd
||mixed||	A variable with undefined (or multiple) type.||
174 5 jmd
||integer||	Integer type variable (whole number).||
175 5 jmd
||float	||Float type (point number).||
176 5 jmd
||boolean||	Logical type (true or false).||
177 5 jmd
||string||	String type (any value in "" or ' ').||
178 5 jmd
||array	||Array type.||
179 5 jmd
||object||	Object type.||
180 1 jmd
||ressource||	Resource type (returned by for example mysql_connect()).||
181 1 jmd
182 36 jmd
Inserting comment in source code for doxygen.
183 36 jmd
Result : full doc for variables, functions, classes...
184 27 jmd
185 36 jmd
STATE : accepted
186 27 jmd
187 27 jmd
TODO : check all source code
188 27 jmd
189 25 jmd
== quotes / double quotes ==
190 25 jmd
In which way can we use " or ' in glpi code :
191 25 jmd
192 25 jmd
by example :
193 25 jmd
194 25 jmd
{{{
195 25 jmd
echo "dqmkdqmsl" 
196 25 jmd
}}}
197 33 moyo
198 25 jmd
or
199 33 moyo
200 25 jmd
{{{
201 25 jmd
echo 'kjlkj'
202 1 jmd
}}}
203 1 jmd
204 31 jmd
After reading bench about strings : http://www.estvideo.net/dew/index/page/phpbench
205 31 jmd
 - Best choice seems to be simple quote.
206 1 jmd
   ex : echo 'toto' not echo "toto"
207 33 moyo
 - best is to concat vars and string.
208 30 jmd
   ex : echo 'toto'.$test.' est vivant.'
209 31 jmd
 - Best choice between echo and print is echo.
210 34 moyo
211 31 jmd
 - Best choice to construct string before make echo, result : decrease number of use echo.
212 28 jmd
213 35 jmd
STATE : accepted
214 26 jmd
215 27 jmd
TODO : check all source code
216 27 jmd
217 1 jmd
== Files ==
218 1 jmd
219 15 moyo
Name in lower case.
220 17 moyo
221 1 jmd
Maximum line length : 100 characters
222 15 moyo
223 26 jmd
STATE : accepted
224 1 jmd
225 26 jmd
TODO : check all files
226 26 jmd
227 3 jmd
== Constants ==
228 1 jmd
229 3 jmd
Capital letter :
230 3 jmd
231 3 jmd
{{{
232 3 jmd
COMPUTER_TYPE
233 1 jmd
}}}
234 1 jmd
235 26 jmd
STATE : accepted
236 26 jmd
237 27 jmd
TODO : check all constants
238 27 jmd
239 10 moyo
== MySQL ==
240 10 moyo
241 32 jmd
Queries must be written onto several lines, one MySQL item by line.
242 17 moyo
243 10 moyo
All MySQL words in UPPER case.
244 41 moyo
245 40 moyo
All item based must be slash protected (table name, field name, condition).
246 11 moyo
247 11 moyo
{{{
248 11 moyo
SELECT *
249 23 jmd
FROM `glpi_computers`
250 13 moyo
WHERE id='32'
251 37 moyo
     AND ( `glpi_computers`.`name` LIKE '%toto%'
252 37 moyo
          OR `glpi_computers`.`name` LIKE '%tata%' )
253 37 moyo
ORDER BY `glpi_computers`.`date_mod` ASC
254 1 jmd
LIMIT 1
255 26 jmd
}}}
256 26 jmd
257 1 jmd
STATE : accepted
258 27 jmd
259 42 remi
TODO : check all source code
260 1 jmd
261 43 moyo
Remi: all values from variable, even integer should be single quoted