EnumerationField   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 14.61 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 13
loc 89
c 0
b 0
f 0
wmc 11
lcom 1
cbo 5
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handles() 0 3 1
A inflate() 0 4 1
A render() 0 6 1
A renderOptions() 13 13 3
A headElements() 0 3 1
A getOptions() 0 3 1
A getType() 0 3 1
A getOptionType() 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\Parameter;
6
use rtens\domin\reflection\types\EnumerationType;
7
use rtens\domin\delivery\web\Element;
8
use rtens\domin\delivery\web\WebField;
9
10
class EnumerationField implements WebField {
11
12
    /** @var FieldRegistry */
13
    private $fields;
14
15
    /**
16
     * EnumerationField constructor.
17
     * @param FieldRegistry $fields
18
     */
19
    public function __construct(FieldRegistry $fields) {
20
        $this->fields = $fields;
21
    }
22
23
    /**
24
     * @param Parameter $parameter
25
     * @return bool
26
     */
27
    public function handles(Parameter $parameter) {
28
        return $parameter->getType() instanceof EnumerationType;
29
    }
30
31
    /**
32
     * @param Parameter $parameter
33
     * @param string $serialized
34
     * @return mixed
35
     */
36
    public function inflate(Parameter $parameter, $serialized) {
37
        $param = new Parameter($parameter->getName() . '-value', $this->getOptionType($parameter));
38
        return $this->fields->getField($param)->inflate($param, $serialized);
39
    }
40
41
    /**
42
     * @param Parameter $parameter
43
     * @param mixed $value
44
     * @return string
45
     */
46
    public function render(Parameter $parameter, $value) {
47
        return (string)new Element('select', [
48
            'name' => $parameter->getName(),
49
            'class' => 'form-control'
50
        ], $this->renderOptions($parameter, $value));
51
    }
52
53 View Code Duplication
    private function renderOptions(Parameter $parameter, $value) {
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...
54
        $options = [];
55
        foreach ($this->getOptions($parameter) as $key => $caption) {
56
            $options[] = new Element('option', array_merge([
57
                'value' => $key
58
            ], $key == $value ? [
59
                'selected' => 'selected'
60
            ] : []), [
61
                $caption
62
            ]);
63
        }
64
        return $options;
65
    }
66
67
    /**
68
     * @param Parameter $parameter
69
     * @return array|\rtens\domin\delivery\web\Element[]
70
     */
71
    public function headElements(Parameter $parameter) {
72
        return [];
73
    }
74
75
    /**
76
     * @param Parameter $parameter
77
     * @return array With captions indexed by values
78
     */
79
    protected function getOptions(Parameter $parameter) {
80
        return $this->getType($parameter)->getOptions();
81
    }
82
83
    /**
84
     * @param Parameter $parameter
85
     * @return EnumerationType
86
     */
87
    private function getType(Parameter $parameter) {
88
        return $parameter->getType();
89
    }
90
91
    /**
92
     * @param Parameter $parameter
93
     * @return \watoki\reflect\Type
94
     */
95
    protected function getOptionType(Parameter $parameter) {
96
        return $this->getType($parameter)->getOptionType();
97
    }
98
}