Completed
Push — develop ( 9e3d33...36b339 )
by
unknown
08:18
created

SettingsFieldset::build()   D

Complexity

Conditions 14
Paths 121

Size

Total Lines 78
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 78
rs 4.7994
cc 14
eloc 51
nc 121
nop 0

How to fix   Long Method    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 Settings\Form;
4
5
use Zend\Form\Fieldset;
6
use Settings\Entity\SettingsContainerInterface;
7
use Settings\Entity\ModuleSettingsContainerInterface;
8
use Settings\Entity\Hydrator\SettingsEntityHydrator;
9
use Zend\ServiceManager\ServiceLocatorAwareInterface;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
12
//use Zend\InputFilter\InputFilterProviderInterface;
13
14
class SettingsFieldset extends Fieldset implements ServiceLocatorAwareInterface
15
{
16
    protected $forms;
17
    protected $isBuild = false;
18
    protected $labelMap = [];
19
    
20
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
21
    {
22
        $this->forms = $serviceLocator;
23
        return $this;
24
    }
25
    
26
    public function getServiceLocator()
27
    {
28
        return $this->forms;
29
    }
30
    
31
    public function getHydrator()
32
    {
33
        if (!$this->hydrator) {
34
            $hydrator = new SettingsEntityHydrator();
35
            $this->setHydrator($hydrator);
36
        }
37
        return $this->hydrator;
38
    }
39
    
40
    public function setObject($object)
41
    {
42
        parent::setObject($object);
43
        $this->build();
44
        return $this;
45
    }
46
    
47
    public function build()
48
    {
49
        
50
        if ($this->isBuild) {
51
            return;
52
        }
53
        
54
        $settings = $this->getObject();
55
        $reflection = new \ReflectionClass($settings);
56
        $properties = $reflection->getProperties();
57
        
58
        $skipProperties = array('_settings', 'isWritable');
59
        if ($settings instanceof ModuleSettingsContainerInterface) {
60
            $skipProperties[] = '_module';
61
        }
62
        $children = array();
63
        foreach ($properties as $property) {
64
            if (in_array($property->getName(), $skipProperties) || $this->has($property->getName())) {
65
                continue;
66
            }
67
            $property->setAccessible(true);
68
            $value = $property->getValue($settings);
69
            if ($value instanceof SettingsContainerInterface) {
70
                $children[$property->getName()] = $value;
71
                continue;
72
            }
73
74
            $inputName = $property->getName();
75
76
            $inputLabel = isset($this->labelMap[$inputName]) ? $this->labelMap[$inputName] : $inputName;
77
78
            if (is_array($inputLabel)){
79
                $priority = isset($inputLabel[1])?$inputLabel[1]:0;
80
                $inputLabel = $inputLabel[0];
81
            }else{
82
                $priority = 0;
83
            }
84
85
            $input = array(
86
                    'name' => $inputName,
87
                    'options' => array(
88
89
                        'label' => $inputLabel
90
                    ),
91
            );
92
            if (is_bool($value)) {
93
                $input['type']= 'Checkbox';
94
                $input['attributes']['checked'] = $value;
95
            } else {
96
                $input['attributes']['value'] = $value;
97
            }
98
            $this->add($input,['priority'=>$priority]);
99
            
100
        }
101
        
102
        foreach ($children as $name => $child) {
103
            $objectClass  = ltrim(get_class($settings), '\\');
104
            $moduleName   = substr($objectClass, 0, strpos($objectClass, '\\'));
105
            $fieldsetName = $moduleName . '/' . ucfirst($name) . 'SettingsFieldset';
106
            
107
            if ($this->forms->has($fieldsetName)) {
108
                $fieldset = $this->forms->get($fieldsetName);
109
                if (!$fieldset->getHydrator() instanceof SettingsEntityHydrator) {
110
                    $fieldset->setHydrator($this->getHydrator());
111
                }
112
            } else {
113
                $fieldset = new self();
114
                $label = preg_replace('~([A-Z])~', ' $1', $name);
115
                $fieldset->setLabel(ucfirst($label));
116
            }
117
            $fieldset->setName($name)
118
                     ->setObject($child);
119
            
120
            
121
            $this->add($fieldset);
0 ignored issues
show
Bug introduced by
It seems like $fieldset defined by $this->forms->get($fieldsetName) on line 108 can also be of type object; however, Zend\Form\Fieldset::add() does only seem to accept array|object<Traversable...\Form\ElementInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
122
        }
123
        $this->isBuild = true;
124
    }
125
}
126