CodingStandards » History » Version 57

yllen, 01/10/2011 10:20 AM

1 49 moyo
validate on 09/07/09
2 49 moyo
3 1 jmd
4 51 tsmr
h2. Indentation
5 51 tsmr
6 51 tsmr
7 48 moyo
3 spaces
8 44 moyo
9 1 jmd
Line width : 100 
10 44 moyo
11 51 tsmr
<pre>
12 1 jmd
<?php
13 1 jmd
// base level
14 1 jmd
    // level 1
15 1 jmd
        // level 2
16 1 jmd
    // level 1
17 1 jmd
// base level
18 1 jmd
?> 
19 51 tsmr
</pre>
20 44 moyo
21 42 remi
STATE : accepted : 3 spaces / 100 chars max
22 1 jmd
23 1 jmd
24 43 moyo
25 51 tsmr
h2. Control structures
26 51 tsmr
27 51 tsmr
28 10 moyo
Multiple conditions in several idented lines
29 1 jmd
30 51 tsmr
<pre>
31 7 jmd
<?php
32 45 moyo
if ($test1) {
33 45 moyo
     for ($i=0 ; $i<$end ; $i++) {
34 7 jmd
          echo "test ".( $i<10 ? "0$i" : $i )."<br>";
35 7 jmd
     }
36 7 jmd
}
37 8 jmd
38 1 jmd
if ($a==$b
39 54 yllen
    || ($c==$d && $e==$f)) {
40 54 yllen
   ...
41 54 yllen
} else if {
42 54 yllen
   ... 
43 10 moyo
}
44 54 yllen
45 45 moyo
 
46 45 moyo
switch ($test2) {
47 1 jmd
     case 1 :
48 1 jmd
          echo "Case 1";
49 8 jmd
          break;
50 45 moyo
51 8 jmd
     case 2 :
52 45 moyo
          echo "Case 2";
53 8 jmd
          // No break here : because...
54 1 jmd
55 8 jmd
     default :
56 8 jmd
          echo "Default Case";
57 42 remi
}
58 51 tsmr
59 1 jmd
?> 
60 1 jmd
</pre>
61 42 remi
62 42 remi
STATE : accepted
63 1 jmd
64 1 jmd
65 51 tsmr
66 51 tsmr
h2. Including files
67 51 tsmr
68 1 jmd
 
69 12 moyo
include_once in order to include the file once and to raise warning if file does not exists
70 1 jmd
71 51 tsmr
<pre>
72 1 jmd
include_once(GLPI_ROOT."/inc/includes.php");
73 51 tsmr
</pre>
74 1 jmd
75 1 jmd
STATE : accepted
76 26 jmd
77 19 moyo
TODO : require_once , include, require -> include_once
78 1 jmd
79 1 jmd
80 51 tsmr
h2. PHP tags
81 51 tsmr
82 51 tsmr
83 2 jmd
Short tag not allowed. Use complet tags.
84 2 jmd
85 51 tsmr
<pre>
86 1 jmd
<?php
87 1 jmd
88 2 jmd
// code
89 1 jmd
90 2 jmd
?>
91 51 tsmr
</pre>
92 26 jmd
93 19 moyo
STATE : accepted
94 1 jmd
95 1 jmd
TODO : nothing
96 3 jmd
97 1 jmd
98 51 tsmr
h2. Functions
99 51 tsmr
100 51 tsmr
101 1 jmd
Function names should be written in camelBack, for example:
102 1 jmd
103 51 tsmr
<pre>
104 1 jmd
<?php
105 46 moyo
function userName($a, $bldkjqmjk , $cldqkjmlqdsjkm, $ldqjlqdskj, $peaoizuoiauz, 
106 1 jmd
                  $lqdkjlqsdmj) {
107 46 moyo
}
108 3 jmd
?>
109 51 tsmr
</pre>
110 1 jmd
111 26 jmd
STATE : accepted
112 1 jmd
113 1 jmd
TODO : check all functions
114 1 jmd
115 42 remi
116 51 tsmr
h2. Class
117 43 moyo
118 51 tsmr
119 51 tsmr
Class names should be written in [[CamelCase]], for example:
120 51 tsmr
121 51 tsmr
<pre>
122 1 jmd
<?php
123 51 tsmr
class [[ExampleAuthentification]] {
124 1 jmd
}
125 1 jmd
?>
126 51 tsmr
</pre>
127 26 jmd
128 26 jmd
STATE : accepted
129 19 moyo
130 19 moyo
TODO : check all classes
131 1 jmd
132 5 jmd
133 51 tsmr
h2. Variables
134 51 tsmr
135 51 tsmr
136 17 moyo
Variable names should be as descriptive and short as possible. 
137 1 jmd
138 1 jmd
Normal variables should be written in lower case. In case of multiple words use the _ separator.
139 1 jmd
140 9 jmd
Global variables should be written in UPPER case. In case of multiple words use the _ separator.
141 9 jmd
142 5 jmd
Example:
143 5 jmd
144 51 tsmr
<pre>
145 5 jmd
<?php
146 56 yllen
$user         = 'glpi';
147 56 yllen
$users        = array('glpi', 'glpi2', 'glpi3'); // put elements in alphabetic order
148 9 jmd
$users_groups = array('glpi', 'glpi2', 'glpi3');
149 9 jmd
150 56 yllen
$CFG_GLPI = array();
151 5 jmd
152 5 jmd
?>
153 1 jmd
154 51 tsmr
</pre>
155 19 moyo
156 1 jmd
STATE : accepted
157 1 jmd
158 1 jmd
159 51 tsmr
h2. Variable types
160 6 jmd
161 1 jmd
162 51 tsmr
163 51 tsmr
Variable types for use in [[DocBlocks]] for Doxygen:
164 51 tsmr
165 1 jmd
||Type||	Description||
166 5 jmd
||mixed||	A variable with undefined (or multiple) type.||
167 5 jmd
||integer||	Integer type variable (whole number).||
168 5 jmd
||float	||Float type (point number).||
169 5 jmd
||boolean||	Logical type (true or false).||
170 5 jmd
||string||	String type (any value in "" or ' ').||
171 5 jmd
||array	||Array type.||
172 1 jmd
||object||	Object type.||
173 36 jmd
||ressource||	Resource type (returned by for example mysql_connect()).||
174 36 jmd
175 27 jmd
Inserting comment in source code for doxygen.
176 36 jmd
Result : full doc for variables, functions, classes...
177 1 jmd
178 1 jmd
STATE : accepted
179 1 jmd
180 25 jmd
TODO : check all source code
181 25 jmd
182 25 jmd
183 51 tsmr
h2. quotes / double quotes
184 51 tsmr
185 51 tsmr
186 51 tsmr
<pre>
187 28 jmd
echo 'dqmkdqmsl';
188 1 jmd
echo 'toto'.$test.' est vivant';
189 51 tsmr
</pre>
190 1 jmd
191 35 jmd
192 1 jmd
After reading bench about strings : http://www.estvideo.net/dew/index/page/phpbench
193 1 jmd
 - Best choice seems to be simple quote.
194 26 jmd
   ex : echo 'toto' not echo "toto"
195 27 jmd
 - best is to concat vars and string.
196 27 jmd
   ex : echo 'toto'.$test.' est vivant.'
197 1 jmd
 - Best choice between echo and print is echo.
198 1 jmd
199 1 jmd
 - Best choice to construct string before make echo, result : decrease number of use echo.
200 1 jmd
201 52 moyo
202 52 moyo
Performance says to use simple quotes but it make using \n slower (using a constant)
203 52 moyo
204 55 ddurieux
*Conclusion : Use double quotes (more lisible)*
205 52 moyo
206 52 moyo
207 1 jmd
STATE : accepted
208 1 jmd
209 1 jmd
TODO : check all source code
210 1 jmd
211 1 jmd
212 51 tsmr
h2. Files
213 51 tsmr
214 51 tsmr
215 1 jmd
Name in lower case.
216 15 moyo
217 17 moyo
Maximum line length : 100 characters
218 1 jmd
219 15 moyo
STATE : accepted
220 26 jmd
221 1 jmd
TODO : check all files
222 26 jmd
223 26 jmd
224 51 tsmr
h2. Constants
225 51 tsmr
226 51 tsmr
227 3 jmd
Capital letter :
228 3 jmd
229 51 tsmr
<pre>
230 1 jmd
COMPUTER_TYPE
231 51 tsmr
</pre>
232 26 jmd
233 26 jmd
STATE : accepted
234 1 jmd
235 1 jmd
TODO : check all constants
236 27 jmd
237 1 jmd
238 51 tsmr
h2. [[MySQL]]
239 1 jmd
240 1 jmd
241 51 tsmr
Queries must be written onto several lines, one [[MySQL]] item by line.
242 51 tsmr
243 51 tsmr
All [[MySQL]] words in UPPER case.
244 51 tsmr
245 48 moyo
All item based must be slash protected (table name, field name, condition).
246 40 moyo
247 48 moyo
All values from variable, even integer should be single quoted
248 48 moyo
249 51 tsmr
<pre>
250 48 moyo
$query = "SELECT *
251 53 ddurieux
          FROM `glpi_computers`
252 53 ddurieux
          LEFT JOIN `xyzt` ON (`glpi_computers`.`fk_xyzt` = `xyzt`.`id`
253 53 ddurieux
                               AND `xyzt`.`toto` = 'jk')
254 51 tsmr
          WHERE @id@ = '32'
255 53 ddurieux
                AND ( `glpi_computers`.`name` LIKE '%toto%'
256 53 ddurieux
                      OR `glpi_computers`.`name` LIKE '%tata%' )
257 53 ddurieux
          ORDER BY `glpi_computers`.`date_mod` ASC
258 42 remi
          LIMIT 1";
259 51 tsmr
</pre>
260 43 moyo
261 57 yllen
<pre>
262 57 yllen
$query = "INSERT INTO `glpi_alerts`
263 57 yllen
                (`itemtype`, `items_id`, `type`, `date`) // put field's names to avoid mistakes when names of fields change
264 57 yllen
          VALUE ('contract', '5', '2', NOW())";
265 57 yllen
</pre>
266 57 yllen
267 1 jmd
STATE : accepted
268 1 jmd
269 1 jmd
TODO : check all source code