CollectionNode   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 31.25 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 15
loc 48
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 12 2
B walk() 15 32 7

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
3
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Node;
6
7
use Linio\Component\Input\Exception\RequiredFieldException;
8
9
class CollectionNode extends BaseNode
10
{
11
    public function getValue(string $field, $value)
12
    {
13
        $this->checkConstraints($field, $value);
14
15
        $items = [];
16
17
        foreach ($value as $collectionValue) {
18
            $items[] = $this->instantiator->instantiate($this->type, $collectionValue);
19
        }
20
21
        return $items;
22
    }
23
24
    public function walk($input)
25
    {
26
        $result = [];
27
28
        if (!$this->hasChildren()) {
29
            return $input;
30
        }
31
32
        foreach ($input as $inputItem) {
33
            $itemResult = [];
34
35 View Code Duplication
            foreach ($this->getChildren() as $field => $config) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
36
                if (!array_key_exists($field, $inputItem)) {
37
                    if ($config->isRequired()) {
38
                        throw new RequiredFieldException($field);
39
                    }
40
41
                    if (!$config->hasDefault()) {
42
                        continue;
43
                    }
44
45
                    $inputItem[$field] = $config->getDefault();
46
                }
47
48
                $itemResult[$field] = $config->getValue($field, $config->walk($inputItem[$field]));
49
            }
50
51
            $result[] = $itemResult;
52
        }
53
54
        return $result;
55
    }
56
}
57