Passed
Push — master ( cc91a3...057c43 )
by Christian
02:20
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 71
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 65
nc 1
nop 0
dl 0
loc 71
ccs 0
cts 65
cp 0
crap 2
rs 8.7636
c 0
b 0
f 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 Scheb\Tombstone\Analyzer\Config;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
9
class Configuration implements ConfigurationInterface
10
{
11
    public const CONFIG_ROOT = 'analyzer';
12
13
    public function getConfigTreeBuilder()
14
    {
15
        $treeBuilder = new TreeBuilder(self::CONFIG_ROOT);
16
        $rootNode = $treeBuilder->getRootNode();
17
18
        /* @var ArrayNodeDefinition $rootNode */
19
        $rootNode
20
            ->ignoreExtraKeys(false)
21
            ->children()
22
                ->arrayNode('source')
23
                    ->children()
24
                        ->arrayNode('directories')
25
                            ->scalarPrototype()
26
                                ->validate()
27
                                    ->ifTrue($this->isNoDirectory())
28
                                    ->thenInvalid('Must be a valid directory path, given: %s')
29
                                ->end()
30
                            ->end()
31
                        ->end()
0 ignored issues
show
Bug introduced by Christian Scheb
The method end() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Config...ion\Builder\TreeBuilder. Are you sure you never get one of those? ( Ignorable by Annotation )

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

31
                        ->/** @scrutinizer ignore-call */ end()
Loading history...
32
                        ->arrayNode('excludes')
33
                            ->scalarPrototype()->end()
34
                        ->end()
35
                        ->arrayNode('names')
36
                            ->scalarPrototype()->end()
37
                        ->end()
38
                        ->arrayNode('notNames')
39
                            ->scalarPrototype()->end()
40
                        ->end()
41
                    ->end()
42
                ->end()
43
                ->scalarNode('rootDir')
44
                    ->isRequired()
45
                    ->validate()
46
                        ->ifTrue($this->isNoDirectory())
47
                        ->thenInvalid('Must be a valid directory path, given: %s')
48
                    ->end()
49
                ->end()
50
                ->arrayNode('logs')
51
                    ->children()
52
                        ->scalarNode('directory')
53
                            ->isRequired()
54
                            ->validate()
55
                                ->ifTrue($this->isNoDirectory())
56
                                ->thenInvalid('Must be a valid directory path, given: %s')
57
                            ->end()
58
                        ->end()
59
                    ->end()
60
                ->end()
61
                ->arrayNode('report')
62
                    ->children()
63
                        ->scalarNode('php')
64
                            ->defaultNull()
65
                            ->validate()
66
                                ->ifTrue($this->isNotWritable())
67
                                ->thenInvalid('Must be a writable file path, given: %s')
68
                            ->end()
69
                        ->end()
70
                        ->scalarNode('html')
71
                            ->defaultNull()
72
                            ->validate()
73
                                ->ifTrue($this->isNoWritableDirectory())
74
                                ->thenInvalid('Must be a writable directory, given: %s')
75
                            ->end()
76
                        ->end()
77
                        ->scalarNode('console')->defaultFalse()->end()
78
                    ->end()
79
                ->end()
80
            ->end()
81
        ;
82
83
        return $treeBuilder;
84
    }
85
86
    private function isNoDirectory(): callable
87
    {
88
        return function (string $path): bool {
89
            $path = realpath($path);
90
91
            return !(false !== $path && is_dir($path));
92
        };
93
    }
94
95
    private function isNotWritable(): callable
96
    {
97
        return function (string $path): bool {
98
            $directory = dirname($path);
99
            $directory = realpath($directory);
100
101
            return !(false !== $directory && is_writeable($directory));
102
        };
103
    }
104
105
    private function isNoWritableDirectory(): callable
106
    {
107
        return function (string $path): bool {
108
            $path = realpath($path);
109
110
            return !(false !== $path && is_dir($path) && is_writeable($path));
111
        };
112
    }
113
}
114