Configuration::createNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory CKEditor package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\CKEditorBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
class Configuration implements ConfigurationInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 210
    public function getConfigTreeBuilder()
27
    {
28 210
        $treeBuilder = $this->createTreeBuilder();
29
        $treeBuilder
30 210
            ->root('ivory_ck_editor')
31 210
            ->children()
32 210
                ->booleanNode('enable')->end()
33 210
                ->booleanNode('async')->end()
34 210
                ->booleanNode('auto_inline')->end()
35 210
                ->booleanNode('inline')->end()
36 210
                ->booleanNode('autoload')->end()
37 210
                ->booleanNode('jquery')->end()
38 210
                ->booleanNode('require_js')->end()
39 210
                ->booleanNode('input_sync')->end()
40 210
                ->scalarNode('base_path')->end()
41 210
                ->scalarNode('js_path')->end()
42 210
                ->scalarNode('jquery_path')->end()
43 210
                ->scalarNode('default_config')->end()
44 210
                ->append($this->createConfigsNode())
45 210
                ->append($this->createPluginsNode())
46 210
                ->append($this->createStylesNode())
47 210
                ->append($this->createTemplatesNode())
48 210
                ->append($this->createFilebrowsersNode())
49 210
                ->append($this->createToolbarsNode())
50 210
            ->end();
51
52 210
        return $treeBuilder;
53
    }
54
55
    /**
56
     * @return ArrayNodeDefinition
57
     */
58 210
    private function createConfigsNode()
59
    {
60 210
        return $this->createPrototypeNode('configs')
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method normalizeKeys() 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...
61 210
            ->prototype('array')
62 210
                ->normalizeKeys(false)
63 210
                ->useAttributeAsKey('name')
64 210
                ->prototype('variable')->end()
65 210
            ->end();
66
    }
67
68
    /**
69
     * @return ArrayNodeDefinition
70
     */
71 210
    private function createPluginsNode()
72
    {
73 210
        return $this->createPrototypeNode('plugins')
0 ignored issues
show
Bug introduced by
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...
74 210
            ->prototype('array')
75 210
                ->children()
76 210
                    ->scalarNode('path')->end()
77 210
                    ->scalarNode('filename')->end()
78 210
                ->end()
79 210
            ->end();
80
    }
81
82
    /**
83
     * @return ArrayNodeDefinition
84
     */
85 210
    private function createStylesNode()
86
    {
87 210
        return $this->createPrototypeNode('styles')
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method prototype() 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...
88 210
            ->prototype('array')
89 210
                ->prototype('array')
90 210
                    ->children()
91 210
                        ->scalarNode('name')->end()
92 210
                        ->scalarNode('type')->end()
93 210
                        ->scalarNode('widget')->end()
94 210
                        ->variableNode('element')->end()
95 210
                        ->append($this->createPrototypeNode('styles')->prototype('scalar')->end())
96 210
                        ->append($this->createPrototypeNode('attributes')->prototype('scalar')->end())
97 210
                    ->end()
98 210
                ->end()
99 210
            ->end();
100
    }
101
102
    /**
103
     * @return ArrayNodeDefinition
104
     */
105 210
    private function createTemplatesNode()
106
    {
107 210
        return $this->createPrototypeNode('templates')
0 ignored issues
show
Bug introduced by
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...
108 210
            ->prototype('array')
109 210
                ->children()
110 210
                    ->scalarNode('imagesPath')->end()
111 210
                    ->arrayNode('templates')
112 210
                        ->prototype('array')
113 210
                            ->children()
114 210
                                ->scalarNode('title')->end()
115 210
                                ->scalarNode('image')->end()
116 210
                                ->scalarNode('description')->end()
117 210
                                ->scalarNode('html')->end()
118 210
                                ->scalarNode('template')->end()
119 210
                                ->append($this->createPrototypeNode('template_parameters')->prototype('scalar')->end())
120 210
                            ->end()
121 210
                        ->end()
122 210
                    ->end()
123 210
                ->end()
124 210
            ->end();
125
    }
126
127
    /**
128
     * @return ArrayNodeDefinition
129
     */
130 210
    private function createFilebrowsersNode()
131
    {
132 210
        return $this->createNode('filebrowsers')
133 210
            ->useAttributeAsKey('name')
134 210
            ->prototype('scalar')
135 210
            ->end();
136
    }
137
138
    /**
139
     * @return ArrayNodeDefinition
140
     */
141 210
    private function createToolbarsNode()
142
    {
143 210
        return $this->createNode('toolbars')
144 210
            ->addDefaultsIfNotSet()
145 210
            ->children()
146 210
                ->arrayNode('configs')
147 210
                    ->useAttributeAsKey('name')
148 210
                    ->prototype('array')
149 210
                        ->prototype('variable')->end()
150 210
                    ->end()
151 210
                ->end()
152 210
                ->arrayNode('items')
153 210
                    ->useAttributeAsKey('name')
154 210
                    ->prototype('array')
155 210
                        ->prototype('variable')->end()
156 210
                    ->end()
157 210
                ->end()
158 210
            ->end();
159
    }
160
161
    /**
162
     * @param string $name
163
     *
164
     * @return ArrayNodeDefinition
165
     */
166 210
    private function createPrototypeNode($name)
167
    {
168 210
        return $this->createNode($name)
169 210
            ->normalizeKeys(false)
170 210
            ->useAttributeAsKey('name');
171
    }
172
173
    /**
174
     * @param string $name
175
     *
176
     * @return ArrayNodeDefinition
177
     */
178 210
    private function createNode($name)
179
    {
180 210
        return $this->createTreeBuilder()->root($name);
181
    }
182
183
    /**
184
     * @return TreeBuilder
185
     */
186 210
    private function createTreeBuilder()
187
    {
188 210
        return new TreeBuilder();
189
    }
190
}
191