CodingStandards » History » Version 47

moyo, 07/09/2009 02:33 AM

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