CodingStandards » History » Version 59

yllen, 05/24/2011 10:50 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 59 yllen
h3. Call static function
112 59 yllen
113 59 yllen
If the static function is
114 59 yllen
- in the class     => self::
115 59 yllen
- in parent class  => parent::
116 59 yllen
- in another class => ClassName::
117 59 yllen
118 26 jmd
STATE : accepted
119 1 jmd
120 1 jmd
TODO : check all functions
121 1 jmd
122 42 remi
123 51 tsmr
h2. Class
124 43 moyo
125 51 tsmr
126 51 tsmr
Class names should be written in [[CamelCase]], for example:
127 51 tsmr
128 51 tsmr
<pre>
129 1 jmd
<?php
130 51 tsmr
class [[ExampleAuthentification]] {
131 1 jmd
}
132 1 jmd
?>
133 51 tsmr
</pre>
134 26 jmd
135 26 jmd
STATE : accepted
136 19 moyo
137 19 moyo
TODO : check all classes
138 1 jmd
139 5 jmd
140 51 tsmr
h2. Variables
141 51 tsmr
142 51 tsmr
143 17 moyo
Variable names should be as descriptive and short as possible. 
144 1 jmd
145 1 jmd
Normal variables should be written in lower case. In case of multiple words use the _ separator.
146 1 jmd
147 9 jmd
Global variables should be written in UPPER case. In case of multiple words use the _ separator.
148 9 jmd
149 5 jmd
Example:
150 5 jmd
151 51 tsmr
<pre>
152 5 jmd
<?php
153 56 yllen
$user         = 'glpi';
154 56 yllen
$users        = array('glpi', 'glpi2', 'glpi3'); // put elements in alphabetic order
155 58 yllen
$users        = array('glpi1'  => 'valeur1',
156 58 yllen
                      'nexglpi => array('down' => '1',                                        
157 58 yllen
                                        'up'   => array('firstfield'
158 58 yllen
                                                         => 'newvalue)),  // if too long for width of colomns 
159 58 yllen
                      'glpi2   => 'valeur2');
160 9 jmd
$users_groups = array('glpi', 'glpi2', 'glpi3');
161 9 jmd
162 56 yllen
$CFG_GLPI = array();
163 5 jmd
164 5 jmd
?>
165 1 jmd
166 51 tsmr
</pre>
167 19 moyo
168 1 jmd
STATE : accepted
169 1 jmd
170 1 jmd
171 51 tsmr
h2. Variable types
172 6 jmd
173 1 jmd
174 51 tsmr
175 51 tsmr
Variable types for use in [[DocBlocks]] for Doxygen:
176 51 tsmr
177 1 jmd
||Type||	Description||
178 5 jmd
||mixed||	A variable with undefined (or multiple) type.||
179 5 jmd
||integer||	Integer type variable (whole number).||
180 5 jmd
||float	||Float type (point number).||
181 5 jmd
||boolean||	Logical type (true or false).||
182 5 jmd
||string||	String type (any value in "" or ' ').||
183 5 jmd
||array	||Array type.||
184 1 jmd
||object||	Object type.||
185 36 jmd
||ressource||	Resource type (returned by for example mysql_connect()).||
186 36 jmd
187 27 jmd
Inserting comment in source code for doxygen.
188 36 jmd
Result : full doc for variables, functions, classes...
189 1 jmd
190 1 jmd
STATE : accepted
191 1 jmd
192 25 jmd
TODO : check all source code
193 25 jmd
194 25 jmd
195 51 tsmr
h2. quotes / double quotes
196 51 tsmr
197 51 tsmr
198 51 tsmr
<pre>
199 28 jmd
echo 'dqmkdqmsl';
200 1 jmd
echo 'toto'.$test.' est vivant';
201 51 tsmr
</pre>
202 1 jmd
203 35 jmd
204 1 jmd
After reading bench about strings : http://www.estvideo.net/dew/index/page/phpbench
205 1 jmd
 - Best choice seems to be simple quote.
206 26 jmd
   ex : echo 'toto' not echo "toto"
207 27 jmd
 - best is to concat vars and string.
208 27 jmd
   ex : echo 'toto'.$test.' est vivant.'
209 1 jmd
 - Best choice between echo and print is echo.
210 1 jmd
211 1 jmd
 - Best choice to construct string before make echo, result : decrease number of use echo.
212 1 jmd
213 52 moyo
214 52 moyo
Performance says to use simple quotes but it make using \n slower (using a constant)
215 52 moyo
216 55 ddurieux
*Conclusion : Use double quotes (more lisible)*
217 52 moyo
218 52 moyo
219 1 jmd
STATE : accepted
220 1 jmd
221 1 jmd
TODO : check all source code
222 1 jmd
223 1 jmd
224 51 tsmr
h2. Files
225 51 tsmr
226 51 tsmr
227 1 jmd
Name in lower case.
228 15 moyo
229 17 moyo
Maximum line length : 100 characters
230 1 jmd
231 15 moyo
STATE : accepted
232 26 jmd
233 1 jmd
TODO : check all files
234 26 jmd
235 26 jmd
236 51 tsmr
h2. Constants
237 51 tsmr
238 51 tsmr
239 3 jmd
Capital letter :
240 3 jmd
241 51 tsmr
<pre>
242 1 jmd
COMPUTER_TYPE
243 51 tsmr
</pre>
244 26 jmd
245 26 jmd
STATE : accepted
246 1 jmd
247 1 jmd
TODO : check all constants
248 27 jmd
249 1 jmd
250 51 tsmr
h2. [[MySQL]]
251 1 jmd
252 1 jmd
253 51 tsmr
Queries must be written onto several lines, one [[MySQL]] item by line.
254 51 tsmr
255 51 tsmr
All [[MySQL]] words in UPPER case.
256 51 tsmr
257 48 moyo
All item based must be slash protected (table name, field name, condition).
258 40 moyo
259 48 moyo
All values from variable, even integer should be single quoted
260 48 moyo
261 51 tsmr
<pre>
262 48 moyo
$query = "SELECT *
263 53 ddurieux
          FROM `glpi_computers`
264 53 ddurieux
          LEFT JOIN `xyzt` ON (`glpi_computers`.`fk_xyzt` = `xyzt`.`id`
265 53 ddurieux
                               AND `xyzt`.`toto` = 'jk')
266 51 tsmr
          WHERE @id@ = '32'
267 53 ddurieux
                AND ( `glpi_computers`.`name` LIKE '%toto%'
268 53 ddurieux
                      OR `glpi_computers`.`name` LIKE '%tata%' )
269 53 ddurieux
          ORDER BY `glpi_computers`.`date_mod` ASC
270 42 remi
          LIMIT 1";
271 51 tsmr
</pre>
272 43 moyo
273 57 yllen
<pre>
274 57 yllen
$query = "INSERT INTO `glpi_alerts`
275 57 yllen
                (`itemtype`, `items_id`, `type`, `date`) // put field's names to avoid mistakes when names of fields change
276 57 yllen
          VALUE ('contract', '5', '2', NOW())";
277 57 yllen
</pre>
278 57 yllen
279 1 jmd
STATE : accepted
280 1 jmd
281 1 jmd
TODO : check all source code