Completed
Push — master ( 951207...f9572e )
by Leandro
03:36
created

OptionTrait::toOptions()   C

Complexity

Conditions 10
Paths 13

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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

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
namespace leandrogehlen\querybuilder;
4
5
6
use yii\helpers\Inflector;
7
8
/**
9
 * OptionTrait implements the method [[toOption()]] for Optionable classes.
10
 *
11
 * @author Leandro Gehlen <[email protected]>
12
 */
13
trait OptionTrait
14
{
15
16
    /**
17
     * Converts the model into an options array.
18
     * @return array the array representation of the object
19
     */
20
    public function toOptions()
21
    {
22
        $options = [];
23
24
        foreach ($this as $prop => $value)
0 ignored issues
show
Bug introduced by
The expression $this of type this<leandrogehlen\querybuilder\OptionTrait> is not traversable.
Loading history...
25
        {
26
            $key = (strpos($prop, "on") !== 0) ? Inflector::underscore($prop) : $prop;
27
28
            if ($value instanceof Optionable){
29
                $value = $value->toOptions();
30
            } elseif (is_array($value)) {
31
                foreach ($value as $k => $v) {
32
                    if ($v instanceof Optionable) {
33
                        $value[$k] = $v->toOptions();
34
                    }
35
                }
36
            }
37
38
            if (!is_array($value) && $value !== null || !empty($value)) {
39
                $options[$key] = $value;
40
            }
41
        }
42
        return $options;
43
    }
44
45
}