Configuration::configsNode()   B
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 89
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 62
CRAP Score 6.2249

Importance

Changes 0
Metric Value
cc 6
eloc 76
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 89
ccs 62
cts 76
cp 0.8158
crap 6.2249
rs 7.9014

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
use Translation\Bundle\EditInPlace\Activator;
19
20
/**
21
 * This is the class that validates and merges configuration from your app/config files.
22
 */
23
class Configuration implements ConfigurationInterface
24
{
25
    private $container;
26
27 8
    public function __construct(ContainerBuilder $container)
28
    {
29 8
        $this->container = $container;
30 8
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 8
    public function getConfigTreeBuilder(): TreeBuilder
36
    {
37 8
        $treeBuilder = new TreeBuilder('translation');
38
        // Keep compatibility with symfony/config < 4.2
39 8
        if (!\method_exists($treeBuilder, 'getRootNode')) {
40
            $root = $treeBuilder->root('translation');
0 ignored issues
show
Bug introduced by
The method root() does not exist on Symfony\Component\Config...ion\Builder\TreeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            /** @scrutinizer ignore-call */ 
41
            $root = $treeBuilder->root('translation');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
        } else {
42 8
            $root = $treeBuilder->getRootNode();
43
        }
44
45 8
        $this->configsNode($root);
46 8
        $this->addAutoTranslateNode($root);
47 8
        $this->addEditInPlaceNode($root);
48 8
        $this->addWebUINode($root);
49
50 8
        $isProfilerEnabled = $this->container->getParameter('kernel.debug') && $this->container->has('profiler');
51
52
        $root
53 8
            ->children()
54 8
                ->arrayNode('locales')
55 8
                    ->prototype('scalar')->end()
56 8
                ->end()
57 8
                ->scalarNode('default_locale')->info('Your default language or fallback locale. Default will be kernel.default_locale')->end()
58 8
                ->arrayNode('symfony_profiler')
59 8
                    ->addDefaultsIfNotSet()
60 8
                    ->treatFalseLike(['enabled' => false])
61 8
                    ->treatTrueLike(['enabled' => true])
62 8
                    ->treatNullLike(['enabled' => $isProfilerEnabled])
63 8
                    ->info('Extend the debug profiler with information about requests.')
64 8
                    ->children()
65 8
                        ->booleanNode('enabled')
66 8
                            ->info('Turn the symfony profiler integration on or off. Defaults to kernel debug mode.')
67 8
                            ->defaultValue($isProfilerEnabled)
68 8
                        ->end()
69 8
                        ->scalarNode('formatter')->defaultNull()->end()
70 8
                        ->integerNode('captured_body_length')
71 8
                            ->defaultValue(0)
72 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).')
73 8
                        ->end()
74 8
                    ->end()
75 8
                    ->children()
76 8
                        ->booleanNode('allow_edit')->defaultTrue()->end()
77 8
                    ->end()
78 8
                ->end()
79 8
                ->arrayNode('auto_add_missing_translations')
80 8
                    ->canBeEnabled()
81 8
                    ->children()
82 8
                        ->scalarNode('config_name')->defaultValue('default')->end()
83 8
                    ->end()
84 8
                ->end()
85 8
                ->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end()
86 8
                ->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end()
87 8
            ->end();
88
89 8
        return $treeBuilder;
90
    }
91
92 8
    private function configsNode(ArrayNodeDefinition $root): void
93
    {
94 8
        $container = $this->container;
95 8
        $root->children()
96 8
            ->arrayNode('configs')
97 8
            ->addDefaultChildrenIfNoneSet('default')
98 8
                ->useAttributeAsKey('name')
99 8
                ->prototype('array')
100 8
                    ->fixXmlConfig('dir', 'dirs')
0 ignored issues
show
Bug introduced by
The method fixXmlConfig() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
                    ->/** @scrutinizer ignore-call */ fixXmlConfig('dir', 'dirs')
Loading history...
101 8
                    ->fixXmlConfig('excluded_dir')
102 8
                    ->fixXmlConfig('excluded_name')
103 8
                    ->fixXmlConfig('blacklist_domain')
104 8
                    ->fixXmlConfig('external_translations_dir')
105 8
                    ->fixXmlConfig('whitelist_domain')
106 8
                    ->children()
107 8
                        ->arrayNode('dirs')
108 8
                            ->info('Directories we should scan for translations')
109 8
                            ->prototype('scalar')
110 8
                                ->validate()
111
                                    ->always(function ($value) use ($container) {
112
                                        $value = \str_replace(\DIRECTORY_SEPARATOR, '/', $value);
113
114
                                        if ('@' === $value[0]) {
115
                                            if (false === $pos = \strpos($value, '/')) {
116
                                                $bundleName = \substr($value, 1);
117
                                            } else {
118
                                                $bundleName = \substr($value, 1, $pos - 2);
119
                                            }
120
121
                                            $bundles = $container->getParameter('kernel.bundles');
122
                                            if (!isset($bundles[$bundleName])) {
123
                                                throw new \Exception(\sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, \array_keys($bundles)));
0 ignored issues
show
Bug introduced by
array_keys($bundles) of type array is incompatible with the type double|integer|string expected by parameter $values of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
                                                throw new \Exception(\sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, /** @scrutinizer ignore-type */ \array_keys($bundles)));
Loading history...
Bug introduced by
$bundles of type null is incompatible with the type array expected by parameter $array of array_keys(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
                                                throw new \Exception(\sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, \array_keys(/** @scrutinizer ignore-type */ $bundles)));
Loading history...
124
                                            }
125
126
                                            $ref = new \ReflectionClass($bundles[$bundleName]);
127
                                            $value = false === $pos ? \dirname($ref->getFileName()) : \dirname($ref->getFileName()).\substr($value, $pos);
128
                                        }
129
130
                                        if (!\is_dir($value)) {
131
                                            throw new \Exception(\sprintf('The directory "%s" does not exist.', $value));
132
                                        }
133
134
                                        return $value;
135 8
                                    })
136 8
                                ->end()
137 8
                            ->end()
138 8
                        ->end()
139 8
                        ->arrayNode('excluded_dirs')
140 8
                            ->prototype('scalar')->end()
141 8
                        ->end()
142 8
                        ->arrayNode('excluded_names')
143 8
                            ->prototype('scalar')->end()
144 8
                        ->end()
145 8
                        ->arrayNode('external_translations_dirs')
146 8
                            ->prototype('scalar')->end()
147 8
                        ->end()
148 8
                        ->scalarNode('output_format')->defaultValue('xlf')->end()
149 8
                        ->arrayNode('blacklist_domains')
150 8
                            ->prototype('scalar')->end()
151 8
                        ->end()
152 8
                        ->arrayNode('whitelist_domains')
153 8
                            ->prototype('scalar')->end()
154 8
                        ->end()
155 8
                        ->arrayNode('remote_storage')
156 8
                            ->info('Service ids with to classes that supports remote storage of translations.')
157 8
                            ->prototype('scalar')->end()
158 8
                        ->end()
159 8
                        ->arrayNode('local_storage')
160 8
                            ->defaultValue(['php_translation.local_file_storage.abstract'])
161 8
                            ->info('Service ids with to classes that supports local storage of translations.')
162 8
                            ->prototype('scalar')->end()
163 8
                        ->end()
164 8
                        ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.project_dir%/Resources/translations')->end()
165 8
                        ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end()
166 8
                        ->scalarNode('xliff_version')->info('The version of XLIFF XML you want to use (if dumping to this format).')->defaultValue('2.0')->end()
167 8
                        ->variableNode('local_file_storage_options')
168 8
                            ->info('Options passed to the local file storage\'s dumper.')
169 8
                            ->defaultValue([])
170 8
                            ->validate()
171
                                ->ifTrue(function ($value) {
172
                                    return !\is_array($value);
173 8
                                })
174 8
                                ->thenInvalid('"local_file_storage_options" must be an array.')
175 8
                            ->end()
176 8
                        ->end()
177 8
                    ->end()
178 8
                ->end()
179 8
            ->end()
180 8
        ->end();
181 8
    }
182
183 8
    private function addAutoTranslateNode(ArrayNodeDefinition $root): void
184
    {
185 8
        $root->children()
186 8
            ->arrayNode('fallback_translation')
187 8
                ->canBeEnabled()
188 8
                ->children()
189 8
                    ->enumNode('service')->values(['google', 'yandex', 'bing'])->defaultValue('google')->end()
190 8
                    ->scalarNode('api_key')->defaultNull()->end()
0 ignored issues
show
Bug introduced by
The method scalarNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
                    ->/** @scrutinizer ignore-call */ scalarNode('api_key')->defaultNull()->end()
Loading history...
191 8
                ->end()
192 8
            ->end()
193 8
        ->end();
194 8
    }
195
196 8
    private function addEditInPlaceNode(ArrayNodeDefinition $root): void
197
    {
198 8
        $root->children()
199 8
            ->arrayNode('edit_in_place')
200 8
                ->canBeEnabled()
201 8
                ->children()
202 8
                    ->scalarNode('config_name')->defaultValue('default')->end()
203 8
                    ->scalarNode('activator')->cannotBeEmpty()->defaultValue(Activator::class)->end()
204 8
                    ->scalarNode('show_untranslatable')->defaultTrue()->end()
205 8
                ->end()
206 8
            ->end()
207 8
        ->end();
208 8
    }
209
210 8
    private function addWebUINode(ArrayNodeDefinition $root): void
211
    {
212 8
        $root->children()
213 8
            ->arrayNode('webui')
214 8
                ->canBeEnabled()
215 8
                ->children()
216 8
                    ->booleanNode('allow_create')->defaultTrue()->end()
217 8
                    ->booleanNode('allow_delete')->defaultTrue()->end()
0 ignored issues
show
Bug introduced by
The method booleanNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

217
                    ->/** @scrutinizer ignore-call */ booleanNode('allow_delete')->defaultTrue()->end()
Loading history...
218 8
                    ->scalarNode('file_base_path')->defaultNull()->info('Base path for SourceLocation\'s. Defaults to "%kernel.project_dir%".')->end()
219 8
                ->end()
220 8
            ->end()
221 8
        ->end();
222 8
    }
223
}
224