ColorField   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 4
dl 42
loc 42
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handles() 3 3 1
A inflate() 3 3 2
A render() 9 9 3
A headElements() 3 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\web\Element;
5
use rtens\domin\delivery\web\WebField;
6
use rtens\domin\Parameter;
7
use rtens\domin\parameters\Color;
8
use watoki\reflect\type\ClassType;
9
10 View Code Duplication
class ColorField implements WebField {
0 ignored issues
show
Duplication introduced by
This class 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...
11
12
    /**
13
     * @param Parameter $parameter
14
     * @return bool
15
     */
16
    public function handles(Parameter $parameter) {
17
        return $parameter->getType() == new ClassType(Color::class);
18
    }
19
20
    /**
21
     * @param Parameter $parameter
22
     * @param string $serialized
23
     * @return mixed
24
     */
25
    public function inflate(Parameter $parameter, $serialized) {
26
        return $serialized ? Color::fromHex($serialized) : null;
27
    }
28
29
    /**
30
     * @param Parameter $parameter
31
     * @param Color $value
32
     * @return string
33
     */
34
    public function render(Parameter $parameter, $value) {
35
        return (string)new Element('input', array_merge([
36
            'type' => 'color',
37
            'name' => $parameter->getName(),
38
            'value' => $value ? $value->asHex() : ''
39
        ], $parameter->isRequired() ? [
40
            'required' => 'required'
41
        ] : []));
42
    }
43
44
    /**
45
     * @param Parameter $parameter
46
     * @return array|Element[]
47
     */
48
    public function headElements(Parameter $parameter) {
49
        return [];
50
    }
51
}