InteractiveCliParameterReader   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 8
loc 60
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A has() 0 3 1
B read() 0 18 5
A getField() 8 8 2

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;
3
4
use rtens\domin\delivery\FieldRegistry;
5
use rtens\domin\delivery\ParameterReader;
6
use rtens\domin\Parameter;
7
8
class InteractiveCliParameterReader implements ParameterReader {
9
10
    private $fields;
11
    private $console;
12
13
    /**
14
     * @param FieldRegistry $fields <-
15
     * @param Console $console
16
     */
17
    public function __construct(FieldRegistry $fields, Console $console) {
18
        $this->fields = $fields;
19
        $this->console = $console;
20
    }
21
22
    /**
23
     * @param Parameter $parameter
24
     * @return boolean
25
     */
26
    public function has(Parameter $parameter) {
27
        return true;
28
    }
29
30
    /**
31
     * @param Parameter $parameter
32
     * @return string
33
     * @throws \Exception
34
     */
35
    public function read(Parameter $parameter) {
36
        $prompt = $parameter->getName();
37
        if ($parameter->isRequired()) {
38
            $prompt = $prompt . '*';
39
        }
40
41
        $field = $this->getField($parameter);
42
        $description = $field->getDescription($parameter);
43
        if ($description !== null) {
44
            $prompt .= ' ' . $description;
45
        }
46
47
        $value = $this->console->read($prompt . ': ');
48
        while ($parameter->isRequired() && !$value) {
49
            $value = $this->console->read($prompt . ': ');
50
        }
51
        return $value;
52
    }
53
54
    /**
55
     * @param Parameter $parameter
56
     * @return \rtens\domin\delivery\cli\CliField
57
     * @throws \Exception
58
     */
59 View Code Duplication
    private function getField(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...
60
        $field = $this->fields->getField($parameter);
61
        if (!($field instanceof CliField)) {
62
            $fieldClass = get_class($field);
63
            throw new \Exception("Not a CliField [$fieldClass]");
64
        }
65
        return $field;
66
    }
67
}