You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: general/development/policies/codingstyle/index.md
+8-191Lines changed: 8 additions & 191 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,9 @@ Unless otherwise specified, this Coding Style document **will defer to [PSR-12](
18
18
Where a **de-facto Moodle standard** is encountered but is undocumented, an appropriate **MDLSITE** issue should be raised to have the standard either documented within this Coding style guide, or rejected and the PSR recommendations adopted instead.
19
19
20
20
:::info
21
+
21
22
A "de-facto Moodle standard" is any coding style which is commonly and typically used in Moodle.
23
+
22
24
:::
23
25
24
26
### Goals
@@ -34,7 +36,9 @@ Abstract goals we strive for:
34
36
When considering the goals above, each situation requires an examination of the circumstances and balancing of various trade-offs.
35
37
36
38
:::note
39
+
37
40
Much of the existing Moodle code may not follow all of these guidelines - we continue to upgrade this code when we see it.
41
+
38
42
:::
39
43
40
44
For details about using the Moodle API to get things done, see the [coding guidelines](../../policies.md).
@@ -63,42 +67,6 @@ import Example_PhpTags_Good from '!!raw-loader!./_examples/phptags_good.php';
63
67
64
68
</ValidExample>
65
69
66
-
### Indentation
67
-
68
-
Use an indent of **4 spaces** with no tab characters. Editors should be configured to treat tabs as spaces in order to prevent injection of new tab characters into the source code.
69
-
70
-
**Do not** indent the main script level:
71
-
72
-
<ValidExample>
73
-
74
-
```php title="An example of correct indentation at the main script level"
75
-
<?php
76
-
require('config.php');
77
-
$a = required_param('a', PARAM_INT);
78
-
if ($a > 10) {
79
-
call_some_error($a);
80
-
} else {
81
-
do_something_with($a);
82
-
}
83
-
```
84
-
85
-
</ValidExample>
86
-
87
-
<InvalidExample>
88
-
89
-
```php title="An example of incorrect indentation at the main script level"
90
-
<?php
91
-
require('config.php');
92
-
$a = required_param('a', PARAM_INT);
93
-
if ($a > 10) {
94
-
call_some_error($a);
95
-
} else {
96
-
do_something_with($a);
97
-
}
98
-
```
99
-
100
-
</InvalidExample>
101
-
102
70
SQL queries use special indentation, see [SQL coding style](./sql.md).
103
71
104
72
### Maximum Line Length
@@ -111,7 +79,7 @@ The exception are string files in the `/lang` directory where lines `$string['id
111
79
112
80
#### Wrapping lines
113
81
114
-
Whenever wrapping lines, following rules should generally apply:
82
+
Whenever wrapping lines, the following rules should generally apply:
115
83
116
84
- Indent with 4 spaces by default.
117
85
- Indent the wrapped line with control statement conditions or a function/class declaration with 4 additional spaces to visually distinguish it from the following body of the control statement or the function/class.
@@ -177,158 +145,6 @@ if (($element['object']->is_course_item() || $element['object']->is_category_ite
177
145
178
146
</InvalidExample>
179
147
180
-
#### Wrapping class declarations
181
-
182
-
<ValidExample>
183
-
184
-
```php
185
-
class foo implements bar, baz, qux, quux, quuz, corge, grault,
186
-
garply, waldo, fred, plugh, xyzzy, thud {
187
-
188
-
// Class body indented with 4 spaces.
189
-
}
190
-
```
191
-
192
-
</ValidExample>
193
-
194
-
Alternatively you may want to provide each implemented interface on its own line if it helps readability:
195
-
196
-
<ValidExample>
197
-
198
-
```php
199
-
class provider implements
200
-
// These lines are indented with 8 spaces to distinguish them visually from the class body.
```php title="Wrapping function signatures with 8 spaces"
216
-
/**
217
-
* ...
218
-
*/
219
-
protected function component_class_callback_failed(\Throwable $e, string $component, string $interface,
220
-
string $methodname, array $params) {
221
-
global $CFG, $DB;
222
-
223
-
if ($this->observer) {
224
-
// ...
225
-
}
226
-
}
227
-
```
228
-
229
-
</ValidExample>
230
-
231
-
<ValidExample>
232
-
233
-
```php title="Wrapping function signatures following PSR-12"
234
-
/**
235
-
* ...
236
-
*/
237
-
protected function component_class_callback_failed(
238
-
\Throwable $e,
239
-
string $component,
240
-
string $interface,
241
-
string $methodname,
242
-
array $params
243
-
) {
244
-
global $CFG, $DB;
245
-
246
-
if ($this->observer) {
247
-
// ...
248
-
}
249
-
}
250
-
```
251
-
252
-
</ValidExample>
253
-
254
-
#### Wrapping parameters of a function call
255
-
256
-
Normally, parameters will just fit on one line. If they eventually become too long to fit a single line or of it helps readability, indent with 4 spaces.
257
-
258
-
<ValidExample>
259
-
260
-
```php title="Wrapping function calls the Moodle way"
261
-
do_something($param1, $param2, null, null,
262
-
$param5, null, true);
263
-
```
264
-
265
-
</ValidExample>
266
-
267
-
<ValidExample>
268
-
269
-
```php title="Wrapping function calls following PSR-12"
270
-
do_something(
271
-
$param1,
272
-
$param2,
273
-
null,
274
-
null,
275
-
$param5,
276
-
null,
277
-
true
278
-
);
279
-
```
280
-
281
-
</ValidExample>
282
-
283
-
#### Wrapping arrays
284
-
285
-
There is nothing special and the general rules apply again. Indent the wrapped line with 4 spaces.
In many cases, the following style with each item on its own line will make the code more readable.
297
-
298
-
<ValidExample>
299
-
300
-
```php
301
-
$plugininfo['preferences'][$plugin] = [
302
-
'id' => $plugin,
303
-
'link' => $pref_url,
304
-
'string' => $modulenamestr,
305
-
];
306
-
```
307
-
308
-
</ValidExample>
309
-
310
-
:::note
311
-
312
-
The last item has a trailing comma left there which allows to extend the list of items later with a cleaner diff.
313
-
For the same reason, it is better not to align the assignment operators.
314
-
315
-
:::
316
-
317
-
#### Wrapping arrays passed as function parameter
318
-
319
-
This is just an example of combining some of the examples above.
320
-
321
-
<ValidExample>
322
-
323
-
```php
324
-
$url = new moodle_url('/course/loginas.php', [
325
-
'id' => $course->id,
326
-
'sesskey' => sesskey(),
327
-
]);
328
-
```
329
-
330
-
</ValidExample>
331
-
332
148
### Line Termination
333
149
334
150
- Every line must be terminated by a Unix line feed character (LF, decimal 10, hexadecimal 0x0A).
@@ -446,7 +262,9 @@ This has now been deprecated. Please use `stdClass` or the array instantiation i
446
262
447
263
### Functions and Methods
448
264
449
-
Function names should be simple English lowercase words, and start with the [Frankenstyle](./frankenstyle.md) prefix and plugin name to avoid conflicts between plugins. Words should be separated by underscores.
265
+
Method and function names should be simple English lowercase words. Words should be separated by underscores.
266
+
267
+
In the case of legacy functions (those not placed in classes), names should start with the [Frankenstyle](./frankenstyle.md) prefix and plugin name to avoid conflicts between plugins.
450
268
451
269
Verbosity is encouraged: function names should be as illustrative as is practical to enhance understanding.
452
270
@@ -1248,7 +1066,6 @@ $myarray = [
1248
1066
- Classes must be named according to Moodle's naming conventions.
1249
1067
- Classes must go under their respective "component/classes" dir to get the benefits of autoloading and [namespacing](#namespaces). There aren't such luxuries out from there.
1250
1068
- Each php file will contain only one class (or interface, or trait, etc.). Unless it's part of old APIs where multi-artifact files were allowed.
1251
-
- The brace should always be written on the line beside the class name.
1252
1069
- Every class must have a documentation block that conforms to the PHPDocumentor standard.
1253
1070
- All code in a class must be indented with 4 spaces.
1254
1071
- Placing additional code (side-effects) in class files is only permitted to require artifacts not provided via autoloading (old classes or libs out from the "classes" directories and not loaded by Moodle's bootstrap). In those cases, the use of the [`MOODLE_INTERNAL` check](#require--include) will be required.
0 commit comments