MultiField   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 125
Duplicated Lines 16.8 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 21
loc 125
c 0
b 0
f 0
wmc 22
lcom 1
cbo 8
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handles() 0 3 1
A inflate() 0 9 3
A render() 0 10 1
B renderOptions() 0 11 6
A headElements() 0 18 2
A getOptions() 13 13 3
A toString() 0 7 2
A getTypes() 8 8 2
A getField() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace rtens\domin\delivery\web\fields;
3
4
use rtens\domin\delivery\FieldRegistry;
5
use rtens\domin\delivery\web\Element;
6
use rtens\domin\delivery\web\HeadElements;
7
use rtens\domin\delivery\web\WebField;
8
use rtens\domin\Parameter;
9
use watoki\reflect\Type;
10
use watoki\reflect\type\ClassType;
11
use watoki\reflect\type\MultiType;
12
13
class MultiField implements WebField {
14
15
    /** @var FieldRegistry */
16
    private $fields;
17
18
    /**
19
     * @param FieldRegistry $fields
20
     */
21
    public function __construct(FieldRegistry $fields) {
22
        $this->fields = $fields;
23
    }
24
25
    /**
26
     * @param Parameter $parameter
27
     * @return bool
28
     */
29
    public function handles(Parameter $parameter) {
30
        return $parameter->getType() instanceof MultiType;
31
    }
32
33
    /**
34
     * @param Parameter $parameter
35
     * @param array $serialized
36
     * @return mixed
37
     */
38
    public function inflate(Parameter $parameter, $serialized) {
39
        foreach ($this->getTypes($parameter) as $i => $type) {
40
            if (array_key_exists("multi-$i", $serialized)) {
41
                $optionParameter = new Parameter($parameter->getName(), $type);
42
                return $this->getField($optionParameter)->inflate($optionParameter, $serialized["multi-$i"]);
43
            }
44
        }
45
        return null;
46
    }
47
48
    /**
49
     * @param Parameter $parameter
50
     * @param mixed|null $value
51
     * @return string
52
     */
53
    public function render(Parameter $parameter, $value) {
54
        $id = str_replace(['[', ']'], '-', $parameter->getName());
55
56
        return implode("\n", array_merge([
57
            new Element('select', [
58
                'class' => 'form-control form-group',
59
                'onchange' => "$(this).next().hide().appendTo('body'); $('#' + $(this).val()).show().insertAfter($(this));"
60
            ], $this->getOptions($parameter, $value, $id))
61
        ], $this->renderOptions($parameter, $value, $id)));
62
    }
63
64
    private function renderOptions(Parameter $parameter, $value, $id) {
65
        $fields = [];
66
        foreach ($this->getTypes($parameter) as $i => $type) {
67
            $optionParameter = new Parameter($parameter->getName() . "[multi-$i]", $type);
68
            $selected = is_null($value) && $i == 0 || $type->is($value);
69
            $fields[] = new Element('div', ['class' => 'multi-control' . ($selected ? '' : ' not-selected'), 'id' => "$id-multi-option-$i"], [
70
                $this->getField($optionParameter)->render($optionParameter, $type->is($value) ? $value : null)
71
            ]);
72
        }
73
        return $fields;
74
    }
75
76
    /**
77
     * @param Parameter $parameter
78
     * @return array|Element[]
79
     */
80
    public function headElements(Parameter $parameter) {
81
        $ownElements = [
82
            HeadElements::jquery(),
83
            new Element('script', [], [
84
                "$(function () {
85
                    $('.multi-control.not-selected').hide().appendTo('body');
86
                });"
87
            ])
88
        ];
89
90
        foreach ($this->getTypes($parameter) as $i => $type) {
91
            $optionParameter = new Parameter($parameter->getName(), $type);
92
            $optionElements = $this->getField($optionParameter)->headElements($optionParameter);
93
            $ownElements = array_merge($ownElements, $optionElements);
94
        }
95
96
        return $ownElements;
97
    }
98
99 View Code Duplication
    private function getOptions(Parameter $parameter, $value, $id) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
        $options = [];
101
        foreach ($this->getTypes($parameter) as $i => $type) {
102
            $options[] = new Element('option', array_merge([
103
                'value' => "$id-multi-option-$i"
104
            ], $type->is($value) ? [
105
                'selected' => 'selected'
106
            ] : []), [
107
                $this->toString($type)
108
            ]);
109
        }
110
        return $options;
111
    }
112
113
    private function toString(Type $type) {
114
        if ($type instanceof ClassType) {
115
            return (new \ReflectionClass($type->getClass()))->getShortName();
116
        } else {
117
            return (string)$type;
118
        }
119
    }
120
121 View Code Duplication
    private function getTypes(Parameter $parameter) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
        $type = $parameter->getType();
123
        if (!($type instanceof MultiType)) {
124
            throw new \InvalidArgumentException("[$type] must be a MultiType");
125
        }
126
127
        return $type->getTypes();
128
    }
129
130
    /**
131
     * @param Parameter $optionParameter
132
     * @return WebField
133
     */
134
    private function getField($optionParameter) {
135
        return $this->fields->getField($optionParameter);
136
    }
137
}