Completed
Push — master ( af310e...ca9412 )
by Nikola
05:51
created

TwigNodeDefinition::configureTwigGlobals()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 5.0105

Importance

Changes 0
Metric Value
dl 0
loc 51
c 0
b 0
f 0
ccs 37
cts 40
cp 0.925
rs 8.7579
cc 5
nc 1
nop 0
crap 5.0105

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
declare(strict_types=1);
4
5
namespace RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\Configuration;
6
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
9
/**
10
 * Twig environment configuration.
11
 *
12
 * @internal
13
 *
14
 * @psalm-suppress all
15
 */
16
final class TwigNodeDefinition extends ArrayNodeDefinition
17
{
18 9
    public function __construct()
19
    {
20 9
        parent::__construct('twig');
21
22 9
        $this->configureTwigOptions();
23 9
        $this->configureTwigFormatOptions();
24 9
        $this->addDefaultsIfNotSet();
25 9
        $this->configureTwigGlobals();
26 9
    }
27
28
    /**
29
     * Configure Twig options.
30
     */
31 9
    private function configureTwigOptions(): void
32
    {
33
        $this
34 9
            ->fixXmlConfig('path')
35 9
            ->children()
36 9
                ->variableNode('autoescape')->defaultValue(false)->end()
37 9
                ->scalarNode('autoescape_service')->defaultNull()->end()
38 9
                ->scalarNode('autoescape_service_method')->defaultNull()->end()
39 9
                ->scalarNode('base_template_class')->example('Twig_Template')->cannotBeEmpty()->end()
40 9
                ->scalarNode('cache')->defaultValue('%kernel.cache_dir%/query_resources/twig')->end()
41 9
                ->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
42 9
                ->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
43 9
                ->booleanNode('strict_variables')->end()
44 9
                ->scalarNode('auto_reload')->end()
45 9
                ->integerNode('optimizations')->min(-1)->end()
46 9
                ->arrayNode('paths')
47 9
                    ->normalizeKeys(false)
48 9
                    ->useAttributeAsKey('paths')
49 9
                    ->beforeNormalization()
50 9
                    ->always()
51
                    ->then(static function ($paths) {
52 3
                        $normalized = array();
53 3
                        foreach ($paths as $path => $namespace) {
54 3
                            if (\is_array($namespace)) {
55
                                // xml
56 1
                                $path = $namespace['value'];
57 1
                                $namespace = $namespace['namespace'];
58
                            }
59
60
                            // path within the default namespace
61 3
                            if (\ctype_digit((string) $path)) {
62
                                $path = $namespace;
63
                                $namespace = null;
64
                            }
65
66 3
                            $normalized[$path] = $namespace;
67
                        }
68
69 3
                        return $normalized;
70 9
                    })
71 9
                    ->end()
72 9
                    ->prototype('variable')->end()
73 9
                ->end()
74 9
            ->end();
75 9
    }
76
77
    /**
78
     * Configure Twig format options.
79
     */
80 9
    private function configureTwigFormatOptions(): void
81
    {
82
        $this
83 9
            ->children()
84 9
                ->arrayNode('date')
85 9
                    ->info('The default format options used by the date filter')
86 9
                    ->addDefaultsIfNotSet()
87 9
                    ->children()
88 9
                        ->scalarNode('format')->defaultValue('F j, Y H:i')->end()
89 9
                        ->scalarNode('interval_format')->defaultValue('%d days')->end()
90 9
                        ->scalarNode('timezone')
91 9
                            ->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used')
92 9
                            ->defaultNull()
93 9
                        ->end()
94 9
                    ->end()
95 9
                ->end()
96 9
                ->arrayNode('number_format')
97 9
                    ->info('The default format options for the number_format filter')
98 9
                    ->addDefaultsIfNotSet()
99 9
                    ->children()
100 9
                        ->integerNode('decimals')->defaultValue(0)->end()
101 9
                        ->scalarNode('decimal_point')->defaultValue('.')->end()
102 9
                        ->scalarNode('thousands_separator')->defaultValue(',')->end()
103 9
                    ->end()
104 9
                ->end()
105 9
            ->end();
106 9
    }
107
108
    /**
109
     * Configure Twig globals.
110
     */
111 9
    private function configureTwigGlobals(): void
112
    {
113
        $this
114 9
            ->fixXmlConfig('global')
115 9
            ->children()
116 9
                ->arrayNode('globals')
117 9
                    ->normalizeKeys(false)
118 9
                    ->useAttributeAsKey('key')
119 9
                    ->example(['foo' => '"@bar"', 'pi' => 3.14])
120 9
                    ->prototype('array')
121 9
                        ->beforeNormalization()
122
                            ->ifTrue(static function ($v) {
123 3
                                return is_string($v) && 0 === strpos($v, '@');
124 9
                            })
125
                            ->then(static function ($v) {
126
                                if (0 === \strpos($v, '@@')) {
127
                                    return \substr($v, 1);
128
                                }
129
130
                                return ['id' => \substr($v, 1), 'type' => 'service'];
131 9
                            })
132 9
                        ->end()
133 9
                        ->beforeNormalization()
134
                            ->ifTrue(static function ($v) {
135 3
                                if (\is_array($v)) {
136 3
                                    $keys = \array_keys($v);
137 3
                                    \sort($keys);
138
139 3
                                    return $keys !== ['id', 'type'] && $keys !== ['value'];
140
                                }
141
142 2
                                return true;
143 9
                            })
144
                            ->then(static function ($v) {
145 2
                                return array('value' => $v);
146 9
                            })
147 9
                        ->end()
148 9
                        ->children()
149 9
                            ->scalarNode('id')->end()
150 9
                            ->scalarNode('type')
151 9
                                ->validate()
152 9
                                    ->ifNotInArray(['service'])
153 9
                                    ->thenInvalid('The %s type is not supported')
154 9
                                ->end()
155 9
                            ->end()
156 9
                            ->variableNode('value')->end()
157 9
                        ->end()
158 9
                    ->end()
159 9
                ->end()
160 9
            ->end();
161 9
    }
162
}
163