Conditions | 10 |
Paths | 9 |
Total Lines | 24 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
4 | function insert_if_exists ($insert, $template = null, $match = null) { |
||
5 | if (!is_null($insert)) { // if item exists |
||
6 | if (is_array($insert)) { // if more than one item is given |
||
7 | $ret = ''; // total of all items to return |
||
8 | foreach ($insert as $in) { // for each item |
||
9 | $ret .= insert_if_exists($in, $template, $match); // get the value if it exists |
||
10 | } |
||
11 | return $ret; // return total |
||
12 | } |
||
13 | if (!is_null($match)) { // if a match function is set |
||
14 | if (!$match($insert)) { // if item doesn't match the given function |
||
15 | return ''; // return nothing |
||
16 | } |
||
17 | } |
||
18 | if (!is_null($template) && is_callable($template)) { // if a template is set and is a function |
||
19 | return $template($insert); // insert value into the template |
||
20 | } |
||
21 | return $insert; // else, return the value |
||
22 | } |
||
23 | if (!is_null($template) && !is_callable($template)) { // if the template variable is not a function |
||
24 | return $template; // use the template as the default |
||
25 | } |
||
26 | return ''; // if item doesn't exist, return nothing |
||
27 | } |
||
28 | } |