Configuration::getConfigTreeBuilder()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 8.72
c 0
b 0
f 0
cc 3
nc 4
nop 0

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
namespace MyBuilder\Bundle\CronosBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6
use Symfony\Component\Config\Definition\ConfigurationInterface;
7
use Symfony\Component\HttpKernel\Kernel;
8
9
class Configuration implements ConfigurationInterface
10
{
11
    public function getConfigTreeBuilder(): TreeBuilder
12
    {
13
        if (method_exists(TreeBuilder::class, 'getRootNode')) {
14
            // Symfony 4
15
            $treeBuilder = new TreeBuilder('my_builder_cronos');
16
            $rootNode = $treeBuilder->getRootNode();
17
        } else {
18
            // Symfony 3
19
            $treeBuilder = new TreeBuilder();
0 ignored issues
show
Bug introduced by
The call to TreeBuilder::__construct() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
20
            $rootNode = $treeBuilder->root('my_builder_cronos');
0 ignored issues
show
Bug introduced by
The method root() does not seem to exist on object<Symfony\Component...on\Builder\TreeBuilder>.

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...
21
        }
22
23
        if (method_exists(Kernel::class, 'getProjectDir')) {
24
            // `kernel.project_dir` available since Symfony 3.3
25
            $pathToConsole = '%kernel.project_dir%/bin/console';
26
        } else {
27
            // `kernel.root_dir` dropped in Symfony 5
28
            $pathToConsole = '%kernel.root_dir%/../bin/console';
29
        }
30
31
        $rootNode
32
            ->children()
33
                ->arrayNode('exporter')
34
                    ->addDefaultsIfNotSet()
35
                    ->children()
36
                        ->scalarNode('key')
37
                            ->isRequired()
38
                            ->cannotBeEmpty()
39
                            ->info('Unique key that wraps all the cron configured for the current application.')
40
                            ->example('my_symfony_app')
41
                        ->end()
42
                        ->scalarNode('mailto')
43
                            ->cannotBeEmpty()
44
                            ->info('Sets the default email address for all cron output to go to.')
45
                            ->example('[email protected]')
46
                        ->end()
47
                        ->scalarNode('path')
48
                            ->example('/usr/local/bin:/usr/bin:/bin')
49
                            ->info('Sets the path for all commands in the crontab.')
50
                        ->end()
51
                        ->scalarNode('executor')
52
                            ->cannotBeEmpty()
53
                            ->defaultValue('php')
54
                            ->example('php')
55
                            ->info('Allows you to specify a program that all commands should be passed to such as "/usr/local/bin/php".')
56
                        ->end()
57
                        ->scalarNode('console')
58
                            ->cannotBeEmpty()
59
                            ->defaultValue($pathToConsole)
60
                            ->example('%kernel.project_dir%/bin/console')
61
                            ->info('Allows you to specify the console that all commands should be passed to such as "bin/console".')
62
                        ->end()
63
                        ->scalarNode('shell')
64
                            ->cannotBeEmpty()
65
                            ->example('/bin/sh')
66
                            ->info('Allows you to specify which shell each program should be run with.')
67
                        ->end()
68
                        ->scalarNode('timezone')
69
                            ->example('Europe/Paris')
70
                            ->info('Allows you to add CRON_TZ which specifies the time zone specific for the cron table.')
71
                        ->end()
72
                    ->end()
73
                ->end()
74
            ->end();
75
76
        return $treeBuilder;
77
    }
78
}
79