Completed
Push — master ( b77c5c...4fbeac )
by Jeroen
10s
created

StrategyNodeDefinition::createNode()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 14
nc 8
nop 0
crap 4.0058
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webcreate\Conveyor\Config\Definition\Builder;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
16
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
17
use Symfony\Component\Config\Definition\Builder\NodeParentInterface;
18
use Symfony\Component\Config\Definition\Builder\ParentNodeDefinitionInterface;
19
use Webcreate\Conveyor\Config\Definition\StrategyNode;
20
use Webcreate\Conveyor\Factory\StrategyFactory;
21
22
/**
23
 * This class provides a fluent interface for defining an transporter node.
24
 *
25
 * @author Jeroen Fiege
26
 */
27
class StrategyNodeDefinition extends ArrayNodeDefinition implements
28
        ParentNodeDefinitionInterface
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 8 found
Loading history...
29
{
30
    protected $nodeBuilder;
31
    protected $children;
32
33
    /**
34
     * @var StrategyFactory
35
     */
36
    protected $strategyFactory;
37
38 2
    public function __construct($name, NodeParentInterface $parent = null)
39
    {
40 2
        parent::__construct($name, $parent);
41 2
    }
42
43
    /**
44
     * Returns a node builder to be used to add children and prototype
45
     *
46
     * @return NodeBuilder The node builder
47
     */
48 2
    protected function getNodeBuilder()
49
    {
50 2
        if (null === $this->nodeBuilder) {
51
            $this->nodeBuilder = new NodeBuilder();
52
        }
53
54 2
        return $this->nodeBuilder->setParent($this);
55
    }
56
57
    /**
58
     * (non-PHPdoc)
59
     * @see Symfony\Component\Config\Definition\Builder.NodeDefinition::createNode()
60
     */
61 2
    protected function createNode()
62
    {
63 2
        $node = new StrategyNode($this->name, $this->parent);
0 ignored issues
show
Bug introduced by
It seems like $this->parent can also be of type object<Symfony\Component...er\NodeParentInterface>; however, Webcreate\Conveyor\Confi...tegyNode::__construct() does only seem to accept null|object<Symfony\Comp...finition\NodeInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
64 2
        $node->setStrategyFactory($this->strategyFactory);
65
66 2
        $node->setAddIfNotSet($this->addDefaults);
67 2
        $node->setRequired($this->required);
68
69 2
        foreach ($this->children as $child) {
70 2
            $child->parent = $node;
0 ignored issues
show
Documentation Bug introduced by
It seems like $node of type object<Webcreate\Conveyo...efinition\StrategyNode> is incompatible with the declared type object<Symfony\Component...deParentInterface>|null of property $parent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71 2
            $node->addChild($child->getNode());
72
        }
73
74 2
        if (null !== $this->normalization) {
75 2
            $node->setNormalizationClosures($this->normalization->before);
76 2
            $node->setXmlRemappings($this->normalization->remappings);
77
        }
78
79 2
        if (null !== $this->validation) {
80
            $node->setFinalValidationClosures($this->validation->rules);
81
        }
82
83 2
        return $node;
84
    }
85
86
    /**
87
     * (non-PHPdoc)
88
     * @see Symfony\Component\Config\Definition\Builder.ParentNodeDefinitionInterface::children()
89
     */
90 2
    public function children()
91
    {
92 2
        return $this->getNodeBuilder();
93
    }
94
95
    /**
96
     * (non-PHPdoc)
97
     * @see Symfony\Component\Config\Definition\Builder.ParentNodeDefinitionInterface::append()
98
     */
99 2
    public function append(NodeDefinition $node)
100
    {
101 2
        $this->children[$node->name] = $node->setParent($this);
102
103 2
        return $this;
104
    }
105
106
    /**
107
     * (non-PHPdoc)
108
     * @see Symfony\Component\Config\Definition\Builder.ParentNodeDefinitionInterface::setBuilder()
109
     */
110 2
    public function setBuilder(NodeBuilder $builder)
111
    {
112 2
        $this->nodeBuilder = $builder;
113 2
    }
114
115
    /**
116
     * Set StrategyFactory
117
     *
118
     * @param  StrategyFactory|null                                                    $factory
119
     * @return StrategyNodeDefinition
120
     */
121 2
    public function setStrategyFactory($factory)
122
    {
123 2
        $this->strategyFactory = $factory;
124
125 2
        return $this;
126
    }
127
}
128