Completed
Push — master ( a9ca49...edb286 )
by Maximilian
119:25 queued 54:19
created

Configuration::createStylesNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
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 FOS\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
    public function getConfigTreeBuilder()
27
    {
28
        $treeBuilder = $this->createTreeBuilder();
29
        $treeBuilder
30
            ->root('fos_ck_editor')
31
            ->children()
32
                ->booleanNode('enable')->end()
33
                ->booleanNode('async')->end()
34
                ->booleanNode('auto_inline')->end()
35
                ->booleanNode('inline')->end()
36
                ->booleanNode('autoload')->end()
37
                ->booleanNode('jquery')->end()
38
                ->booleanNode('require_js')->end()
39
                ->booleanNode('input_sync')->end()
40
                ->scalarNode('base_path')->end()
41
                ->scalarNode('js_path')->end()
42
                ->scalarNode('jquery_path')->end()
43
                ->scalarNode('default_config')->end()
44
                ->append($this->createConfigsNode())
45
                ->append($this->createPluginsNode())
46
                ->append($this->createStylesNode())
47
                ->append($this->createTemplatesNode())
48
                ->append($this->createFilebrowsersNode())
49
                ->append($this->createToolbarsNode())
50
            ->end();
51
52
        return $treeBuilder;
53
    }
54
55
    /**
56
     * @return ArrayNodeDefinition
57
     */
58
    private function createConfigsNode()
59
    {
60
        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
            ->prototype('array')
62
                ->normalizeKeys(false)
63
                ->useAttributeAsKey('name')
64
                ->prototype('variable')->end()
65
            ->end();
66
    }
67
68
    /**
69
     * @return ArrayNodeDefinition
70
     */
71
    private function createPluginsNode()
72
    {
73
        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
            ->prototype('array')
75
                ->children()
76
                    ->scalarNode('path')->end()
77
                    ->scalarNode('filename')->end()
78
                ->end()
79
            ->end();
80
    }
81
82
    /**
83
     * @return ArrayNodeDefinition
84
     */
85
    private function createStylesNode()
86
    {
87
        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
            ->prototype('array')
89
                ->prototype('array')
90
                    ->children()
91
                        ->scalarNode('name')->end()
92
                        ->scalarNode('type')->end()
93
                        ->scalarNode('widget')->end()
94
                        ->variableNode('element')->end()
95
                        ->append($this->createPrototypeNode('styles')->prototype('scalar')->end())
96
                        ->append($this->createPrototypeNode('attributes')->prototype('scalar')->end())
97
                    ->end()
98
                ->end()
99
            ->end();
100
    }
101
102
    /**
103
     * @return ArrayNodeDefinition
104
     */
105
    private function createTemplatesNode()
106
    {
107
        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
            ->prototype('array')
109
                ->children()
110
                    ->scalarNode('imagesPath')->end()
111
                    ->arrayNode('templates')
112
                        ->prototype('array')
113
                            ->children()
114
                                ->scalarNode('title')->end()
115
                                ->scalarNode('image')->end()
116
                                ->scalarNode('description')->end()
117
                                ->scalarNode('html')->end()
118
                                ->scalarNode('template')->end()
119
                                ->append($this->createPrototypeNode('template_parameters')->prototype('scalar')->end())
120
                            ->end()
121
                        ->end()
122
                    ->end()
123
                ->end()
124
            ->end();
125
    }
126
127
    /**
128
     * @return ArrayNodeDefinition
129
     */
130
    private function createFilebrowsersNode()
131
    {
132
        return $this->createNode('filebrowsers')
133
            ->useAttributeAsKey('name')
134
            ->prototype('scalar')
135
            ->end();
136
    }
137
138
    /**
139
     * @return ArrayNodeDefinition
140
     */
141
    private function createToolbarsNode()
142
    {
143
        return $this->createNode('toolbars')
144
            ->addDefaultsIfNotSet()
145
            ->children()
146
                ->arrayNode('configs')
147
                    ->useAttributeAsKey('name')
148
                    ->prototype('array')
149
                        ->prototype('variable')->end()
150
                    ->end()
151
                ->end()
152
                ->arrayNode('items')
153
                    ->useAttributeAsKey('name')
154
                    ->prototype('array')
155
                        ->prototype('variable')->end()
156
                    ->end()
157
                ->end()
158
            ->end();
159
    }
160
161
    /**
162
     * @param string $name
163
     *
164
     * @return ArrayNodeDefinition
165
     */
166
    private function createPrototypeNode($name)
167
    {
168
        return $this->createNode($name)
169
            ->normalizeKeys(false)
170
            ->useAttributeAsKey('name');
171
    }
172
173
    /**
174
     * @param string $name
175
     *
176
     * @return ArrayNodeDefinition
177
     */
178
    private function createNode($name)
179
    {
180
        return $this->createTreeBuilder()->root($name);
181
    }
182
183
    /**
184
     * @return TreeBuilder
185
     */
186
    private function createTreeBuilder()
187
    {
188
        return new TreeBuilder();
189
    }
190
}
191