Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

DependencyInjection/Configuration.php (7 issues)

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 2
    public function getConfigTreeBuilder()
21
    {
22 2
        $treeBuilder = new TreeBuilder('kunstmaan_page_part');
23 2
        if (method_exists($treeBuilder, 'getRootNode')) {
24 2
            $rootNode = $treeBuilder->getRootNode();
25
        } else {
26
            // BC layer for symfony/config 4.1 and older
27
            $rootNode = $treeBuilder->root('kunstmaan_page_part');
28
        }
29
30 2
        $rootNode->children()
31 2
            ->booleanNode('extended_pagepart_chooser')
32 2
                ->defaultFalse()
33 2
            ->end();
34
35
        /** @var ArrayNodeDefinition $pageparts */
36 2
        $pageparts = $rootNode->children()->arrayNode('pageparts')->useAttributeAsKey('index')->prototype('array');
37 2
        $pageparts->children()->scalarNode('name')->isRequired();
38 2
        $pageparts->children()->scalarNode('context')->isRequired();
39 2
        $pageparts->children()->scalarNode('extends');
40 2
        $pageparts->children()->scalarNode('widget_template');
41
42
        /** @var ArrayNodeDefinition $types */
43 2
        $types = $pageparts->children()->arrayNode('types')->defaultValue([])->prototype('array');
44 2
        $types->children()->scalarNode('name')->isRequired();
45 2
        $types->children()->scalarNode('class')->isRequired();
46 2
        $types->children()->scalarNode('preview');
47 2
        $types->children()->scalarNode('pagelimit');
48
49
        // *************************************************************************************************************
50
51
        /** @var ArrayNodeDefinition $pagetemplates */
52 2
        $pagetemplates = $rootNode->children()->arrayNode('pagetemplates')->useAttributeAsKey('index')->defaultValue([])->prototype('array');
53
54 2
        $pagetemplates->children()->scalarNode('template')->isRequired();
55 2
        $pagetemplates->children()->scalarNode('name')->isRequired();
56
57
        /** @var ArrayNodeDefinition $rows */
58 2
        $rows = $pagetemplates->children()->arrayNode('rows')->prototype('array');
59
60
        /** @var ArrayNodeDefinition $regions */
61 2
        $regions = $rows->children()->arrayNode('regions')->prototype('array');
62
63
        // no subregions this way, sorry. feel free to implement it: https://gist.github.com/Lumbendil/3249173
64
        $regions
65 2
            ->children()
66 2
                ->scalarNode('name')->end()
67 2
                ->scalarNode('span')->defaultValue(12)->end()
68 2
                ->scalarNode('template')->end()
69 2
                ->variableNode('rows')
70
                    ->validate()->ifTrue(function ($element) {
71
                        return !\is_array($element);
72 2
                    })->thenInvalid('The rows element must be an array.')->end()
73
                    ->validate()->always(function ($children) {
74
                        array_walk($children, [$this, 'evaluateRows']);
75
76
                        return $children;
77 2
                    })->end()
78 2
                ->end()
79 2
            ->end();
80
81 2
        return $treeBuilder;
82
    }
83
84
    protected function evaluateRows(&$child, $name)
85
    {
86
        $child = $this->getRowNode($name)->finalize($child);
87
    }
88
89 View Code Duplication
    protected function getRowNode($name = '')
0 ignored issues
show
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...
90
    {
91
        $treeBuilder = new TreeBuilder();
92
        $definition = $treeBuilder->root($name);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
93
        $this->buildRowNode($definition);
94
95
        return $definition->getNode(true);
96
    }
97
98
    protected function buildRowNode(NodeDefinition $node)
0 ignored issues
show
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...
99
    {
100
        return $node
101
                ->validate()->always(function ($children) {
102
                    array_walk($children, [$this, 'evaluateRegions']);
103
104
                    return $children;
105
                })
106
            ->end();
107
    }
108
109
    protected function evaluateRegions(&$child, $name)
110
    {
111
        $child = $this->getRegionNode($name)->finalize($child);
112
    }
113
114 View Code Duplication
    protected function getRegionNode($name = '')
0 ignored issues
show
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...
115
    {
116
        $treeBuilder = new TreeBuilder();
117
        $definition = $treeBuilder->root($name);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
118
        $this->buildRegionNode($definition);
119
120
        return $definition->getNode(true);
121
    }
122
123
    protected function buildRegionNode(NodeDefinition $node)
0 ignored issues
show
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...
124
    {
125
        return $node
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
126
            ->children()
127
                ->scalarNode('name')->isRequired()->end()
128
                ->scalarNode('span')->defaultValue(12)->end()
129
                ->variableNode('rows')
130
                    ->validate()->ifTrue(function ($element) {
131
                        return !\is_array($element);
132
                    })->thenInvalid('The rows element must be an array.')->end()
133
                    ->validate()->always(function ($children) {
134
                        array_walk($children, [$this, 'evaluateRows']);
135
136
                        return $children;
137
                    })->end()
138
                ->end()
139
            ->end();
140
    }
141
}
142