Skip to content

Commit edd83fb

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 af30a93 commit edd83fb

File tree

1 file changed

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

1 file changed

+31
-0
lines changed

general/development/policies/codingstyle/index.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,37 @@ 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 error as the types are not enforced as well as leading to poor documentation of the function
1165+
1166+
<InvalidExample>
1167+
```php
1168+
public function badfunction(
1169+
string $text,
1170+
context $context = null,
1171+
array $options = [],
1172+
): string;
1173+
</InvalidExample>
1174+
1175+
If arguments are optional, then they should be marked appropriately as optional with a default value.
1176+
<ValidExample>
1177+
function goodfunction(
1178+
string $text,
1179+
?context $context = null,
1180+
bool $trusted = false,
1181+
?bool $clean = null,
1182+
bool $filter = true,
1183+
bool $para = true,
1184+
bool $newlines = true,
1185+
bool $overflowdiv = false,
1186+
bool $blanktarget = false,
1187+
bool $allowid = false,
1188+
): string;
1189+
</ValidExample>
1190+
1191+
If there was a very good reason, then the use of an array could be considered. But it is strongly encouraged that all parameters be explict and typed.
1192+
11621193
## Control statements
11631194

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

0 commit comments

Comments
 (0)