CodingStandards » History » Version 62

yllen, 06/03/2016 07:50 PM

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