Skip to content

Commit 95050f8

Browse files
authored
Merge pull request #1391 from andrewnicols/mdlsite7597
[docs] Updating coding style per MDLSITE-7597
2 parents 225d850 + 225eede commit 95050f8

File tree

1 file changed

+8
-191
lines changed
  • general/development/policies/codingstyle

1 file changed

+8
-191
lines changed

general/development/policies/codingstyle/index.md

Lines changed: 8 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ Unless otherwise specified, this Coding Style document **will defer to [PSR-12](
1818
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.
1919

2020
:::info
21+
2122
A "de-facto Moodle standard" is any coding style which is commonly and typically used in Moodle.
23+
2224
:::
2325

2426
### Goals
@@ -34,7 +36,9 @@ Abstract goals we strive for:
3436
When considering the goals above, each situation requires an examination of the circumstances and balancing of various trade-offs.
3537

3638
:::note
39+
3740
Much of the existing Moodle code may not follow all of these guidelines - we continue to upgrade this code when we see it.
41+
3842
:::
3943

4044
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';
6367

6468
</ValidExample>
6569

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-
10270
SQL queries use special indentation, see [SQL coding style](./sql.md).
10371

10472
### Maximum Line Length
@@ -111,7 +79,7 @@ The exception are string files in the `/lang` directory where lines `$string['id
11179

11280
#### Wrapping lines
11381

114-
Whenever wrapping lines, following rules should generally apply:
82+
Whenever wrapping lines, the following rules should generally apply:
11583

11684
- Indent with 4 spaces by default.
11785
- 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
177145

178146
</InvalidExample>
179147

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.
201-
\core_privacy\local\metadata\provider,
202-
\core_privacy\local\request\subsystem\provider,
203-
\core_privacy\local\request\core_userlist_provider {
204-
205-
// Class body indented with 4 spaces.
206-
}
207-
```
208-
209-
</ValidExample>
210-
211-
#### Wrapping of the function signatures
212-
213-
<ValidExample>
214-
215-
```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.
286-
287-
<ValidExample>
288-
289-
```php
290-
$plugininfo['preferences'][$plugin] = ['id' => $plugin, 'link' => $pref_url,
291-
'string' => $modulenamestr];
292-
```
293-
294-
</ValidExample>
295-
296-
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-
332148
### Line Termination
333149

334150
- 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
446262

447263
### Functions and Methods
448264

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.
450268

451269
Verbosity is encouraged: function names should be as illustrative as is practical to enhance understanding.
452270

@@ -1248,7 +1066,6 @@ $myarray = [
12481066
- Classes must be named according to Moodle's naming conventions.
12491067
- 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.
12501068
- 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.
12521069
- Every class must have a documentation block that conforms to the PHPDocumentor standard.
12531070
- All code in a class must be indented with 4 spaces.
12541071
- 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

Comments
 (0)