Completed
Push — master ( 54a589...ba00a5 )
by Klaus
07:44
created

CollectionNode::hasChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types=1);
3
4
namespace Linio\Component\Input\Node;
5
6
use Linio\Component\Input\Exception\RequiredFieldException;
7
8
class CollectionNode extends BaseNode
9
{
10
    public function getValue(string $field, $value)
11
    {
12
        $this->checkConstraints($field, $value);
13
14
        $items = [];
15
16
        foreach ($value as $collectionValue) {
17
            $items[] = $this->instantiator->instantiate($this->type, $collectionValue);
18
        }
19
20
        return $items;
21
    }
22
23
    public function walk($input)
1 ignored issue
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
24
    {
25
        $result = [];
26
27
        if (!$this->hasChildren()) {
28
            return $input;
29
        }
30
31
        foreach ($input as $inputItem) {
32
            $itemResult = [];
33
34 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...
35
                if (!array_key_exists($field, $inputItem)) {
36
                    if ($config->isRequired()) {
37
                        throw new RequiredFieldException($field);
38
                    }
39
40
                    if (!$config->hasDefault()) {
41
                        continue;
42
                    }
43
44
                    $inputItem[$field] = $config->getDefault();
45
                }
46
47
                $itemResult[$field] = $config->getValue($field, $config->walk($inputItem[$field]));
48
            }
49
50
            $result[] = $itemResult;
51
        }
52
53
        return $result;
54
    }
55
}
56