CodingStandards » History » Version 52

moyo, 09/05/2009 04:34 PM

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