Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

DependencyInjection/Configuration.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\PagePartBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
/**
11
 * This is the class that validates and merges configuration from your app/config files
12
 *
13
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
14
 */
15
class Configuration implements ConfigurationInterface
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function getConfigTreeBuilder()
21
    {
22
        $treeBuilder = new TreeBuilder();
23
        $root = $treeBuilder->root('kunstmaan_page_part');
24
        $root->children()
0 ignored issues
show
The call to the method Symfony\Component\Config...anNodeDefinition::end() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
25
            ->booleanNode('extended_pagepart_chooser')
26
                ->defaultFalse()
27
            ->end();
28
29
        /** @var ArrayNodeDefinition $pageparts */
30
        $pageparts = $root->children()->arrayNode('pageparts')->useAttributeAsKey('index')->prototype('array');
31
        $pageparts->children()->scalarNode('name')->isRequired();
32
        $pageparts->children()->scalarNode('context')->isRequired();
33
        $pageparts->children()->scalarNode('extends');
34
        $pageparts->children()->scalarNode('widget_template');
35
36
        /** @var ArrayNodeDefinition $types */
37
        $types = $pageparts->children()->arrayNode('types')->defaultValue([])->prototype('array');
38
        $types->children()->scalarNode('name')->isRequired();
39
        $types->children()->scalarNode('class')->isRequired();
40
        $types->children()->scalarNode('preview');
41
        $types->children()->scalarNode('pagelimit');
42
43
        // *************************************************************************************************************
44
45
        /** @var ArrayNodeDefinition $pagetemplates */
46
        $pagetemplates = $root->children()->arrayNode('pagetemplates')->useAttributeAsKey('index')->defaultValue([])->prototype('array');
47
48
        $pagetemplates->children()->scalarNode('template')->isRequired();
49
        $pagetemplates->children()->scalarNode('name')->isRequired();
50
51
        /** @var ArrayNodeDefinition $rows */
52
        $rows = $pagetemplates->children()->arrayNode('rows')->prototype('array');
53
54
        /** @var ArrayNodeDefinition $regions */
55
        $regions = $rows->children()->arrayNode('regions')->prototype('array');
56
57
        // no subregions this way, sorry. feel free to implement it: https://gist.github.com/Lumbendil/3249173
58
        $regions
59
            ->children()
60
                ->scalarNode('name')->end()
61
                ->scalarNode('span')->defaultValue(12)->end()
62
                ->scalarNode('template')->end()
63
                ->variableNode('rows')
64
                    ->validate()->ifTrue(function ($element) {
65
                        return !is_array($element);
66
                    })->thenInvalid('The rows element must be an array.')->end()
67
                    ->validate()->always(function ($children) {
68
                        array_walk($children, array($this, 'evaluateRows'));
69
70
                        return $children;
71
                    })->end()
72
                ->end()
73
            ->end();
74
75
        return $treeBuilder;
76
    }
77
78
    protected function evaluateRows(&$child, $name)
79
    {
80
        $child = $this->getRowNode($name)->finalize($child);
81
    }
82
83 View Code Duplication
    protected function getRowNode($name = '')
84
    {
85
        $treeBuilder = new TreeBuilder();
86
        $definition = $treeBuilder->root($name);
87
        $this->buildRowNode($definition);
88
89
        return $definition->getNode(true);
90
    }
91
92
    protected function buildRowNode(NodeDefinition $node)
93
    {
94
        return $node
95
                ->validate()->always(function ($children) {
96
                    array_walk($children, array($this, 'evaluateRegions'));
97
98
                    return $children;
99
                })
100
            ->end();
101
    }
102
103
    protected function evaluateRegions(&$child, $name)
104
    {
105
        $child = $this->getRegionNode($name)->finalize($child);
106
    }
107
108 View Code Duplication
    protected function getRegionNode($name = '')
109
    {
110
        $treeBuilder = new TreeBuilder();
111
        $definition = $treeBuilder->root($name);
112
        $this->buildRegionNode($definition);
113
114
        return $definition->getNode(true);
115
    }
116
117
    protected function buildRegionNode(NodeDefinition $node)
118
    {
119
        return $node
120
            ->children()
121
                ->scalarNode('name')->isRequired()->end()
122
                ->scalarNode('span')->defaultValue(12)->end()
123
                ->variableNode('rows')
124
                    ->validate()->ifTrue(function ($element) {
125
                        return !is_array($element);
126
                    })->thenInvalid('The rows element must be an array.')->end()
127
                    ->validate()->always(function ($children) {
128
                        array_walk($children, array($this, 'evaluateRows'));
129
130
                        return $children;
131
                    })->end()
132
                ->end()
133
            ->end();
134
    }
135
}
136