Completed
Push — master ( 9a9938...f96c7c )
by Mathieu
55:39 queued 13s
created

Configuration::configsNode()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 90

Duplication

Lines 5
Ratio 5.56 %

Code Coverage

Tests 63
CRAP Score 6.2163

Importance

Changes 0
Metric Value
dl 5
loc 90
ccs 63
cts 77
cp 0.8182
rs 7.5959
c 0
b 0
f 0
cc 6
nc 1
nop 1
crap 6.2163

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[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 Translation\Bundle\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
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
19
/**
20
 * This is the class that validates and merges configuration from your app/config files.
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    private $container;
25
26 8
    public function __construct(ContainerBuilder $container)
27
    {
28 8
        $this->container = $container;
29 8
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 8
    public function getConfigTreeBuilder()
35
    {
36 8
        $treeBuilder = new TreeBuilder('translation');
37
        // Keep compatibility with symfony/config < 4.2
38 8
        if (!\method_exists($treeBuilder, 'getRootNode')) {
39
            $root = $treeBuilder->root('translation');
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...
40
        } else {
41 8
            $root = $treeBuilder->getRootNode();
42
        }
43
44 8
        $this->configsNode($root);
0 ignored issues
show
Compatibility introduced by
$root of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
45 8
        $this->addAutoTranslateNode($root);
0 ignored issues
show
Compatibility introduced by
$root of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
46 8
        $this->addEditInPlaceNode($root);
0 ignored issues
show
Compatibility introduced by
$root of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
47 8
        $this->addWebUINode($root);
0 ignored issues
show
Compatibility introduced by
$root of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
48
49 8
        $debug = $this->container->getParameter('kernel.debug');
50
        $root
51 8
            ->children()
52 8
                ->arrayNode('locales')
53 8
                    ->prototype('scalar')->end()
54 8
                ->end()
55 8
                ->scalarNode('default_locale')->info('Your default language or fallback locale. Default will be kernel.default_locale')->end()
56 8
                ->arrayNode('symfony_profiler')
57 8
                    ->addDefaultsIfNotSet()
58 8
                    ->treatFalseLike(['enabled' => false])
59 8
                    ->treatTrueLike(['enabled' => true])
60 8
                    ->treatNullLike(['enabled' => $debug])
61 8
                    ->info('Extend the debug profiler with information about requests.')
62 8
                    ->children()
63 8
                        ->booleanNode('enabled')
64 8
                            ->info('Turn the symfony profiler integration on or off. Defaults to kernel debug mode.')
65 8
                            ->defaultValue($debug)
66 8
                        ->end()
67 8
                        ->scalarNode('formatter')->defaultNull()->end()
68 8
                        ->integerNode('captured_body_length')
69 8
                            ->defaultValue(0)
70 8
                            ->info('Limit long HTTP message bodies to x characters. If set to 0 we do not read the message body. Only available with the default formatter (FullHttpMessageFormatter).')
71 8
                        ->end()
72 8
                    ->end()
73 8
                    ->children()
74 8
                        ->booleanNode('allow_edit')->defaultTrue()->end()
75 8
                    ->end()
76 8
                ->end()
77 8
                ->arrayNode('auto_add_missing_translations')
78 8
                    ->canBeEnabled()
79 8
                    ->children()
80 8
                        ->scalarNode('config_name')->defaultValue('default')->end()
81 8
                    ->end()
82 8
                ->end()
83 8
                ->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end()
84 8
                ->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end()
85 8
            ->end();
86
87 8
        return $treeBuilder;
88
    }
89
90 8
    private function configsNode(ArrayNodeDefinition $root)
91
    {
92 8
        $container = $this->container;
93 8
        $root->children()
94 8
            ->arrayNode('configs')
95 8
            ->addDefaultChildrenIfNoneSet('default')
96 8
                ->useAttributeAsKey('name')
97 8
                ->prototype('array')
98 8
                    ->fixXmlConfig('dir', 'dirs')
99 8
                    ->fixXmlConfig('excluded_dir')
100 8
                    ->fixXmlConfig('excluded_name')
101 8
                    ->fixXmlConfig('blacklist_domain')
102 8
                    ->fixXmlConfig('external_translations_dir')
103 8
                    ->fixXmlConfig('whitelist_domain')
104 8
                    ->children()
105 8
                        ->arrayNode('dirs')
106 8
                            ->info('Directories we should scan for translations')
107 8
                            ->prototype('scalar')
108 8
                                ->validate()
109
                                    ->always(function ($value) use ($container) {
110
                                        $value = \str_replace(\DIRECTORY_SEPARATOR, '/', $value);
111
112
                                        if ('@' === $value[0]) {
113 View Code Duplication
                                            if (false === $pos = \strpos($value, '/')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
114
                                                $bundleName = \substr($value, 1);
115
                                            } else {
116
                                                $bundleName = \substr($value, 1, $pos - 2);
117
                                            }
118
119
                                            $bundles = $container->getParameter('kernel.bundles');
120
                                            if (!isset($bundles[$bundleName])) {
121
                                                throw new \Exception(\sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, \array_keys($bundles)));
122
                                            }
123
124
                                            $ref = new \ReflectionClass($bundles[$bundleName]);
125
                                            $value = false === $pos ? \dirname($ref->getFileName()) : \dirname($ref->getFileName()).\substr($value, $pos);
126
                                        }
127
128
                                        if (!\is_dir($value)) {
129
                                            throw new \Exception(\sprintf('The directory "%s" does not exist.', $value));
130
                                        }
131
132
                                        return $value;
133 8
                                    })
134 8
                                ->end()
135 8
                            ->end()
136 8
                        ->end()
137 8
                        ->arrayNode('excluded_dirs')
138 8
                            ->prototype('scalar')->end()
139 8
                        ->end()
140 8
                        ->arrayNode('excluded_names')
141 8
                            ->prototype('scalar')->end()
142 8
                        ->end()
143 8
                        ->arrayNode('external_translations_dirs')
144 8
                            ->prototype('scalar')->end()
145 8
                        ->end()
146 8
                        ->scalarNode('output_format')->defaultValue('xlf')->end()
147 8
                        ->arrayNode('blacklist_domains')
148 8
                            ->prototype('scalar')->end()
149 8
                        ->end()
150 8
                        ->arrayNode('whitelist_domains')
151 8
                            ->prototype('scalar')->end()
152 8
                        ->end()
153 8
                        ->arrayNode('remote_storage')
154 8
                            ->info('Service ids with to classes that supports remote storage of translations.')
155 8
                            ->prototype('scalar')->end()
156 8
                        ->end()
157 8
                        ->arrayNode('local_storage')
158 8
                            ->defaultValue(['php_translation.local_file_storage.abstract'])
159 8
                            ->info('Service ids with to classes that supports local storage of translations.')
160 8
                            ->prototype('scalar')->end()
161 8
                        ->end()
162 8
                        ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end()
163 8
                        ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end()
164 8
                        ->scalarNode('xliff_version')->info('The version of XLIFF XML you want to use (if dumping to this format).')->defaultValue('2.0')->end()
165 8
                        ->variableNode('local_file_storage_options')
166 8
                            ->info('Options passed to the local file storage\'s dumper.')
167 8
                            ->defaultValue([])
168 8
                            ->validate()
169
                                ->ifTrue(function ($value) {
170
                                    return !\is_array($value);
171 8
                                })
172 8
                                ->thenInvalid('"local_file_storage_options" must be an array.')
173 8
                            ->end()
174 8
                        ->end()
175 8
                    ->end()
176 8
                ->end()
177 8
            ->end()
178 8
        ->end();
179 8
    }
180
181 8
    private function addAutoTranslateNode(ArrayNodeDefinition $root)
182
    {
183 8
        $root->children()
184 8
            ->arrayNode('fallback_translation')
185 8
                ->canBeEnabled()
186 8
                ->children()
187 8
                    ->enumNode('service')->values(['google', 'yandex', 'bing'])->defaultValue('google')->end()
188 8
                    ->scalarNode('api_key')->defaultNull()->end()
189 8
                ->end()
190 8
            ->end()
191 8
        ->end();
192 8
    }
193
194 8
    private function addEditInPlaceNode(ArrayNodeDefinition $root)
195
    {
196 8
        $root->children()
197 8
            ->arrayNode('edit_in_place')
198 8
                ->canBeEnabled()
199 8
                ->children()
200 8
                    ->scalarNode('config_name')->defaultValue('default')->end()
201 8
                    ->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
202 8
                    ->scalarNode('show_untranslatable')->defaultTrue()->end()
203 8
                ->end()
204 8
            ->end()
205 8
        ->end();
206 8
    }
207
208 8
    private function addWebUINode(ArrayNodeDefinition $root)
209
    {
210 8
        $root->children()
211 8
            ->arrayNode('webui')
212 8
                ->canBeEnabled()
213 8
                ->children()
214 8
                    ->booleanNode('allow_create')->defaultTrue()->end()
215 8
                    ->booleanNode('allow_delete')->defaultTrue()->end()
216 8
                    ->scalarNode('file_base_path')->defaultNull()->info('Base path for SourceLocation\'s. Defaults to "%kernel.project_dir%".')->end()
217 8
                ->end()
218 8
            ->end()
219 8
        ->end();
220 8
    }
221
}
222