NumberField   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 30.43 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 21
loc 69
c 0
b 0
f 0
wmc 11
lcom 0
cbo 2
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handles() 8 8 1
B inflate() 13 13 5
A render() 0 8 2
A getType() 0 8 2
A headElements() 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\Parameter;
5
use rtens\domin\delivery\web\Element;
6
use rtens\domin\delivery\web\WebField;
7
use watoki\reflect\type\DoubleType;
8
use watoki\reflect\type\FloatType;
9
use watoki\reflect\type\IntegerType;
10
use watoki\reflect\type\LongType;
11
12
class NumberField implements WebField {
13
14
    /**
15
     * @param Parameter $parameter
16
     * @return bool
17
     */
18 View Code Duplication
    public function handles(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...
19
        return in_array(get_class($parameter->getType()), [
20
            IntegerType::class,
21
            FloatType::class,
22
            LongType::class,
23
            DoubleType::class
24
        ]);
25
    }
26
27
    /**
28
     * @param Parameter $parameter
29
     * @param string $serialized
30
     * @return int|float|double
31
     */
32 View Code Duplication
    public function inflate(Parameter $parameter, $serialized) {
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...
33
        switch (get_class($parameter->getType())) {
34
            case LongType::class:
35
            case IntegerType::class:
36
                return (int)$serialized;
37
            case DoubleType::class:
38
                return (double)$serialized;
39
            case FloatType::class:
40
                return (float)$serialized;
41
            default:
42
                throw new \InvalidArgumentException("Not a number type [{$parameter->getType()}]");
43
        }
44
    }
45
46
    /**
47
     * @param Parameter $parameter
48
     * @param int|float|double|null $value
49
     * @return string
50
     */
51
    public function render(Parameter $parameter, $value) {
52
        return (string)new Element("input", [
53
            "class" => "form-control",
54
            "type" => $this->getType($parameter),
55
            "name" => $parameter->getName(),
56
            "value" => $value ?: '0'
57
        ]);
58
    }
59
60
    /**
61
     * @param Parameter $parameter
62
     * @return string
63
     */
64
    private function getType(Parameter $parameter) {
65
        switch (get_class($parameter->getType())) {
66
            case IntegerType::class:
67
                return 'number';
68
            default:
69
                return 'text';
70
        }
71
    }
72
73
    /**
74
     * @param Parameter $parameter
75
     * @return array|Element[]
76
     */
77
    public function headElements(Parameter $parameter) {
78
        return [];
79
    }
80
}