Issues (8)

src/Config/Configuration.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scheb\Tombstone\Analyzer\Config;
6
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
class Configuration implements ConfigurationInterface
12
{
13
    public const CONFIG_ROOT = 'root';
14
15 15
    public function getConfigTreeBuilder()
16
    {
17 15
        $treeBuilder = new TreeBuilder(self::CONFIG_ROOT);
18 15
        if (method_exists($treeBuilder, 'getRootNode')) {
19 15
            $rootNode = $treeBuilder->getRootNode();
20
        } else {
21
            // BC layer for symfony/config 4.1 and older
22
            $rootNode = $treeBuilder->root(self::CONFIG_ROOT);
0 ignored issues
show
Deprecated Code introduced by Christian Scheb
The function Symfony\Component\Config...der\TreeBuilder::root() has been deprecated: since Symfony 4.3, pass the root name to the constructor instead ( Ignorable by Annotation )

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

22
            $rootNode = /** @scrutinizer ignore-deprecated */ $treeBuilder->root(self::CONFIG_ROOT);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
23
        }
24
25
        /* @var ArrayNodeDefinition $rootNode */
26
        $rootNode
27 15
            ->ignoreExtraKeys(false)
28 15
            ->children()
29 15
                ->arrayNode('source')
30 15
                    ->isRequired()
31 15
                    ->children()
32 15
                        ->arrayNode('directories')
33 15
                            ->isRequired()
34 15
                            ->requiresAtLeastOneElement()
35 15
                            ->scalarPrototype()
36 15
                                ->validate()
37 15
                                    ->ifTrue($this->isNoDirectory())
38 15
                                    ->thenInvalid('Must be a valid directory path, given: %s')
39 15
                                ->end()
40 15
                            ->end()
41 15
                        ->end()
0 ignored issues
show
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

41
                        ->/** @scrutinizer ignore-call */ end()
Loading history...
42 15
                        ->arrayNode('excludes')
43 15
                            ->scalarPrototype()->end()
44 15
                        ->end()
45 15
                        ->arrayNode('names')
46 15
                            ->defaultValue(['*.php'])
47 15
                            ->scalarPrototype()->end()
48 15
                        ->end()
49 15
                        ->arrayNode('notNames')
50 15
                            ->scalarPrototype()->end()
51 15
                        ->end()
52 15
                    ->end()
53 15
                ->end()
54 15
                ->scalarNode('rootDir')
55 15
                    ->cannotBeEmpty()
56 15
                    ->validate()
57 15
                        ->ifTrue($this->isNoDirectory())
58 15
                        ->thenInvalid('Must be a valid directory path, given: %s')
59 15
                    ->end()
60 15
                ->end()
61 15
                ->arrayNode('logs')
62 15
                    ->isRequired()
63 15
                    ->validate()
64 15
                        ->ifEmpty()
65 15
                        ->thenInvalid('Must have at least one log provider configured')
66 15
                    ->end()
67 15
                    ->children()
68 15
                        ->scalarNode('directory')
69 15
                            ->cannotBeEmpty()
70 15
                            ->validate()
71 15
                                ->ifTrue($this->isNoDirectory())
72 15
                                ->thenInvalid('Must be a valid directory path, given: %s')
73 15
                            ->end()
74 15
                        ->end()
75 15
                        ->arrayNode('custom')
76 15
                            ->children()
77 15
                                ->scalarNode('file')
78 15
                                    ->isRequired()
79 15
                                    ->validate()
80 15
                                        ->ifTrue($this->isNoFile())
81 15
                                        ->thenInvalid('Must be a valid file path, given: %s')
82 15
                                    ->end()
83 15
                                ->end()
84 15
                                ->scalarNode('class')->isRequired()->end()
85 15
                            ->end()
86 15
                        ->end()
87 15
                    ->end()
88 15
                ->end()
89 15
                ->arrayNode('report')
90 15
                    ->addDefaultsIfNotSet()
91 15
                    ->children()
92 15
                        ->scalarNode('php')
93 15
                            ->defaultNull()
94 15
                            ->validate()
95 15
                                ->ifTrue($this->isNotWritableFile())
96 15
                                ->thenInvalid('Must be a writable file path, given: %s')
97 15
                            ->end()
98 15
                        ->end()
99 15
                        ->scalarNode('html')
100 15
                            ->defaultNull()
101 15
                            ->validate()
102 15
                                ->ifTrue($this->isNoWritableDirectory())
103 15
                                ->thenInvalid('Must be a writable directory, given: %s')
104 15
                            ->end()
105 15
                        ->end()
106 15
                        ->scalarNode('console')->defaultTrue()->end()
107 15
                    ->end()
108 15
                ->end()
109 15
            ->end()
110
        ;
111
112 15
        return $treeBuilder;
113
    }
114
115 15
    private function isNoFile(): callable
116
    {
117
        return function ($path): bool {
118 5
            $path = realpath($path);
119
120 5
            return !(false !== $path && !is_dir($path));
121 15
        };
122
    }
123
124 15
    private function isNoDirectory(): callable
125
    {
126
        return function ($path): bool {
127 12
            $path = realpath($path);
128
129 12
            return !(false !== $path && is_dir($path));
130 15
        };
131
    }
132
133 15
    private function isNotWritableFile(): callable
134
    {
135
        return function ($path): bool {
136 4
            $fileRealPath = realpath($path);
137 4
            $fileDirectory = realpath(dirname($path));
138
139
            return !(
140 4
                (false !== $fileDirectory && is_writeable($fileDirectory)) // Path is within a writable directory
141 4
                && (false === $fileRealPath || (!is_dir($fileRealPath) && is_writeable($fileRealPath))) // Path is a writable file or non-existent
142
            );
143 15
        };
144
    }
145
146 15
    private function isNoWritableDirectory(): callable
147
    {
148
        return function ($path): bool {
149 3
            $path = realpath($path);
150
151 3
            return !(false !== $path && is_dir($path) && is_writeable($path));
152 15
        };
153
    }
154
}
155