ObjectField   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 16
loc 88
c 0
b 0
f 0
wmc 14
lcom 1
cbo 8
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handles() 0 3 1
B inflate() 0 23 5
A getClass() 7 7 2
A getField() 0 3 1
A getDescription() 0 3 1
A getProperties() 9 9 3

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\factory\Factory;
9
use watoki\factory\Injector;
10
use watoki\reflect\PropertyReader;
11
use watoki\reflect\type\ClassType;
12
use watoki\reflect\TypeFactory;
13
14
class ObjectField implements CliField {
15
16
    /** @var TypeFactory */
17
    private $types;
18
19
    /** @var FieldRegistry */
20
    private $fields;
21
22
    /** @var ParameterReader */
23
    private $reader;
24
25
    public function __construct(TypeFactory $types, FieldRegistry $fields, ParameterReader $reader) {
26
        $this->types = $types;
27
        $this->fields = $fields;
28
        $this->reader = $reader;
29
    }
30
31
    /**
32
     * @param Parameter $parameter
33
     * @return bool
34
     */
35
    public function handles(Parameter $parameter) {
36
        return $parameter->getType() instanceof ClassType;
37
    }
38
39
    /**
40
     * @param Parameter $parameter
41
     * @param null $serialized
42
     * @return object
43
     */
44
    public function inflate(Parameter $parameter, $serialized) {
45
        $properties = [];
46
        foreach ($this->getProperties($parameter) as $property) {
47
            $propertyParameter = new Parameter($parameter->getName() . '-' . $property->name(), $property->type());
48
49
            $field = $this->getField($propertyParameter);
50
            $properties[$property->name()] = $field->inflate($propertyParameter, $this->reader->read($propertyParameter));
51
        }
52
53
        $injector = new Injector(new Factory());
54
        $instance = $injector->injectConstructor($this->getClass($parameter), $properties, function () {
55
            return false;
56
        });
57
58
        foreach ($this->getProperties($parameter) as $property) {
59
            $value = $properties[$property->name()];
60
            if (!is_null($value) && $property->canSet()) {
61
                $property->set($instance, $value);
62
            }
63
        }
64
65
        return $instance;
66
    }
67
68 View Code Duplication
    private function getClass(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...
69
        $type = $parameter->getType();
70
        if (!($type instanceof ClassType)) {
71
            throw new \InvalidArgumentException("[$type] is not a ClassType");
72
        }
73
        return $type->getClass();
74
    }
75
76
    /**
77
     * @param Parameter $param
78
     * @return CliField
79
     */
80
    private function getField(Parameter $param) {
81
        return $this->fields->getField($param);
82
    }
83
84
    /**
85
     * @param Parameter $parameter
86
     * @return string
87
     */
88
    public function getDescription(Parameter $parameter) {
89
        return '(press enter)';
90
    }
91
92 View Code Duplication
    private function getProperties(Parameter $parameter, $object = null) {
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...
93
        $reader = new PropertyReader($this->types, $this->getClass($parameter));
94
95
        foreach ($reader->readInterface($object) as $property) {
96
            if ($property->canSet()) {
97
                yield $property;
98
            }
99
        }
100
    }
101
}