Completed
Push — master ( 7c218c...593722 )
by Jake
02:19
created

helpers.php ➔ insert_if_exists()   C

Complexity

Conditions 10
Paths 9

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 16
c 1
b 0
f 0
nc 9
nop 3
dl 0
loc 24
rs 5.2164

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
if (! function_exists('insert_if_exists')) {
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
}