Skip to content

Commit a6b8bf8

Browse files
Update coding style guidelines for function arguments
Added guidelines for using arrays as function arguments, emphasizing explicit parameter listing and type definition.
1 parent 9cc88c5 commit a6b8bf8

File tree

1 file changed

+57
-0
lines changed
  • general/development/policies/codingstyle

1 file changed

+57
-0
lines changed

general/development/policies/codingstyle/index.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,63 @@ Magic methods are heavily discouraged, justification will be required when used.
11591159

11601160
(See [MDL-52634](https://moodle.atlassian.net/browse/MDL-52634) for discussion of rationale)
11611161

1162+
### Using arrays for options as arguments
1163+
1164+
All arguments to a function should be explicitly listed out and defined with a type. Using arrays like this can result in errors as the types are not enforced as well as leading to poor documentation of the function.
1165+
1166+
<InvalidExample>
1167+
1168+
```php
1169+
public function bad_function(
1170+
string $text,
1171+
?context $context = null,
1172+
array $options = [],
1173+
): string;
1174+
```
1175+
1176+
</InvalidExample>
1177+
1178+
Instead of a generic array of options, each option should be specified as a function parameter. Optional arguments should provide appropriate defaults.
1179+
1180+
<ValidExample>
1181+
1182+
```php
1183+
function goodfunction(
1184+
string $text,
1185+
?context $context = null,
1186+
bool $trusted = false,
1187+
?bool $clean = null,
1188+
bool $filter = true,
1189+
bool $para = true,
1190+
bool $newlines = true,
1191+
bool $overflowdiv = false,
1192+
bool $blanktarget = false,
1193+
bool $allowid = false,
1194+
): string;
1195+
```
1196+
1197+
</ValidExample>
1198+
1199+
Alternatively a dedicated options class may be used - for example:
1200+
1201+
<ValidExample>
1202+
1203+
```php
1204+
public function good_function(
1205+
string $text,
1206+
?context $context = null,
1207+
filter_options $options = null,
1208+
): string;
1209+
```
1210+
1211+
</ValidExample>
1212+
1213+
:::note
1214+
1215+
Whilst the use of explicitly specified options, or a dedicated options class, is strongly encouraged; this may not be possible in all cases. Where a generic options array is used, this should be discussed and the reason clearly explained.
1216+
1217+
:::
1218+
11621219
## Control statements
11631220

11641221
In general, use white space liberally between lines to add clarity.

0 commit comments

Comments
 (0)