NullableField   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 11.94 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 8
loc 67
c 0
b 0
f 0
wmc 10
lcom 1
cbo 5
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handles() 0 3 1
A inflate() 0 10 4
A getInnerParameter() 8 8 2
A getDescription() 0 3 1
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\cli\fields;
3
4
use rtens\domin\delivery\cli\CliField;
5
use rtens\domin\delivery\FieldRegistry;
6
use rtens\domin\delivery\ParameterReader;
7
use rtens\domin\Parameter;
8
use watoki\reflect\type\NullableType;
9
10
class NullableField implements CliField {
11
12
    /** @var FieldRegistry */
13
    private $fields;
14
15
    /** @var ParameterReader */
16
    private $reader;
17
18
    /**
19
     * @param FieldRegistry $fields
20
     * @param ParameterReader $reader
21
     */
22
    public function __construct(FieldRegistry $fields, ParameterReader $reader) {
23
        $this->fields = $fields;
24
        $this->reader = $reader;
25
    }
26
27
    /**
28
     * @param Parameter $parameter
29
     * @return bool
30
     */
31
    public function handles(Parameter $parameter) {
32
        return $parameter->getType() instanceof NullableType;
33
    }
34
35
    /**
36
     * @param Parameter $parameter
37
     * @param mixed $serialized
38
     * @return mixed
39
     */
40
    public function inflate(Parameter $parameter, $serialized) {
41
        $innerParameter = $this->getInnerParameter($parameter);
42
43
        if (!$this->reader->has($innerParameter) || !$serialized || strtolower($serialized) == 'n') {
44
            return null;
45
        }
46
47
        $field = $this->getField($innerParameter);
48
        return $field->inflate($innerParameter, $this->reader->read($innerParameter));
49
    }
50
51 View Code Duplication
    private function getInnerParameter(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...
52
        $type = $parameter->getType();
53
        if (!($type instanceof NullableType)) {
54
            throw new \InvalidArgumentException("[$type] is not a NullableType");
55
        }
56
57
        return new Parameter($parameter->getName() . '-value', $type->getType());
58
    }
59
60
    /**
61
     * @param Parameter $parameter
62
     * @return string
63
     */
64
    public function getDescription(Parameter $parameter) {
65
        return '? (y|N)';
66
    }
67
68
    /**
69
     * @param $innerParameter
70
     * @return CliField
71
     * @throws \Exception
72
     */
73
    private function getField($innerParameter) {
74
        return $this->fields->getField($innerParameter);
75
    }
76
}