Completed
Push — master ( 5bfa5e...03b9ab )
by Richard
03:43
created

Config::add_rules_node()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 1.0003

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 39
cts 42
cp 0.9286
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 38
nc 1
nop 1
crap 1.0003
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received 
8
 * a copy of the license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\App;
12
13
use Lechimp\Dicto\Report;
14
15
use Symfony\Component\Config\Definition\Processor;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
19
/**
20
 * Configuration for the app and engine.
21
 */
22
class Config implements ConfigurationInterface {
23
    /**
24
     * @var string
25
     */
26
    protected $path;
27
28
    /**
29
     * @var array
30
     */
31
    protected $values;
32
33
    /**
34
     * @var Report\Config[]|null
35
     */
36
    protected $reports = null;
37
38
    /**
39
     * Build the configuration from nested arrays using a processor.
40
     *
41
     * @param   string  $path
42
     */
43 32
    public function __construct($path, array $values) {
44 32
        assert('is_string($path)');
45 32
        if (substr($path, strlen($path) - 1, 1) == "/") {
46 1
            $path = substr($path, 0, strlen($path) - 1);
47 1
        }
48 32
        $this->path = $path;
49 32
        $processor = new \Symfony\Component\Config\Definition\Processor();
50 32
        $this->values = $processor->processConfiguration($this, $values);
51 32
    }
52
53
    /**
54
     * Definition of configuration for symfony.
55
     *
56
     * @inheritdocs
57
     */
58 32
    public function getConfigTreeBuilder() {
59 32
        $tree_builder = new TreeBuilder();
60 32
        $root = $tree_builder->root("dicto");
61 32
        $c = $root->children();
62 32
        $this->add_project_node($c);
63 32
        $this->add_analysis_node($c);
64 32
        $this->add_rules_node($c);
65 32
        $this->add_runtime_node($c);
66 32
        $this->add_reports_node($c);
67 32
        $c->end();
68 32
        return $tree_builder;
69
    }
70
71 32
    protected function add_project_node($c) {
72 32
        $c->arrayNode("project")
73 32
            ->children()
74 32
                ->scalarNode("root")
75 32
                    ->isRequired()
76 32
                ->end()
77 32
                ->scalarNode("storage")
78 32
                    ->defaultValue(".")
79 32
                ->end()
80 32
                ->scalarNode("rules")
81 32
                    ->isRequired()
82 32
                ->end()
83 32
            ->end()
84 32
        ->end();
85 32
    }
86
87 32
    protected function add_analysis_node($c) {
88 32
        $c->arrayNode("analysis")
89 32
            ->children()
90 32
                ->arrayNode("ignore")
91 32
                    ->prototype("scalar")
92 32
                    ->end()
93 32
                    ->defaultValue([])
94 32
                ->end()
95 32
                ->booleanNode("store_index")
96 32
                    ->defaultValue(false)
97 32
                ->end()
98 32
                ->booleanNode("store_results")
99 32
                    ->defaultValue(true)
100 32
                ->end()
101 32
            ->end()
102 32
            ->addDefaultsIfNotSet()
103 32
        ->end();
104 32
    }
105
106 32
    protected function add_rules_node($c) {
107 32
        $c->arrayNode("rules")
108 32
            ->children()
109 32
                ->arrayNode("schemas")
110 32
                    ->prototype("scalar")
111 32
                    ->end()
112
                    ->defaultValue
113 32
                        ([\Lechimp\Dicto\Rules\DependOn::class
114 32
                        , \Lechimp\Dicto\Rules\Invoke::class
115 32
                        , \Lechimp\Dicto\Rules\ContainText::class
116 32
                        ])
117 32
                ->end()
118 32
                ->arrayNode("properties")
119 32
                    ->prototype("scalar")
120 32
                    ->end()
121
                    ->defaultValue
122 32
                        ([\Lechimp\Dicto\Variables\Name::class
123 32
                        , \Lechimp\Dicto\Variables\In::class
124 32
                        ])
125 32
                ->end()
126 32
                ->arrayNode("variables")
127 32
                    ->prototype("scalar")
128 32
                    ->end()
129
                    ->defaultValue
130 32
                        ([\Lechimp\Dicto\Variables\Namespaces::class
131 32
                        , \Lechimp\Dicto\Variables\Classes::class
132 32
                        , \Lechimp\Dicto\Variables\Interfaces::class
133 32
                        , \Lechimp\Dicto\Variables\Traits::class
134 32
                        , \Lechimp\Dicto\Variables\Functions::class
135 32
                        , \Lechimp\Dicto\Variables\Globals::class
136 32
                        , \Lechimp\Dicto\Variables\Files::class
137 32
                        , \Lechimp\Dicto\Variables\Methods::class
138 32
                        , \Lechimp\Dicto\Variables\ErrorSuppressor::class
139 32
                        , \Lechimp\Dicto\Variables\Exit_::class
140 32
                        , \Lechimp\Dicto\Variables\Die_::class
141 32
                        , \Lechimp\Dicto\Variables\Eval_::class
142 32
                        ])
143 32
                ->end()
144 32
            ->end()
145 32
            ->addDefaultsIfNotSet()
146 32
        ->end();
147 32
    }
148
149 32
    protected function add_runtime_node($c) {
150 32
        $c->arrayNode("runtime")
151 32
            ->children()
152 32
                ->booleanNode("check_assertions")
153 32
                    ->defaultValue(false)
154 32
                ->end()
155 32
            ->end()
156 32
            ->addDefaultsIfNotSet()
157 32
        ->end();
158 32
    }
159
160 32
    protected function add_reports_node($c) {
161 32
        $c->arrayNode("reports")
162 32
            ->prototype("array")
163 32
                ->children()
164 32
                    ->scalarNode("name")
165 32
                    ->defaultValue(null)
166 32
                ->end()
167 32
                ->scalarNode("class")
168 32
                    ->isRequired()
169 32
                ->end()
170 32
                ->scalarNode("target")
171 32
                    ->isRequired()
172 32
                ->end()
173 32
                ->scalarNode("source")
174 32
                    ->defaultValue(null)
175 32
                ->end()
176 32
                ->variableNode("config")
177 32
                    ->defaultValue([])
178 32
                ->end()
179 32
            ->end()
180 32
        ->end();
181 32
    }
182
183
    /**
184
     * @return  string
185
     */
186 5
    public function path() {
187 5
        return $this->path;
188
    }
189
190 16
    protected function maybe_prepend_path($path) {
191 16
        assert('is_string($path)');
192 16
        if (substr($path, 0, 2) === "./") {
193 3
            return $this->path()."/".substr($path, 2);
194
        }
195 14
        return $path;
196
    }
197
198
    /**
199
     * @return  string
200
     */
201 4
    public function project_rules() {
202 4
        return $this->maybe_prepend_path($this->values["project"]["rules"]);
203
    }
204
205
    /**
206
     * @return  string
207
     */
208 12
    public function project_root() {
209 12
        return $this->maybe_prepend_path($this->values["project"]["root"]);
210
    }
211
212
    /**
213
     * @return  string
214
     */
215 15
    public function project_storage() {
216 15
        return $this->maybe_prepend_path($this->values["project"]["storage"]);
217
    }
218
219
    /**
220
     * @return  string[]
221
     */
222 10
    public function analysis_ignore() {
223 10
        return $this->values["analysis"]["ignore"];
224
    }
225
226
    /**
227
     * @return  bool
228
     */
229 9
    public function analysis_store_index() {
230 9
        return $this->values["analysis"]["store_index"];
231
    }
232
233
    /**
234
     * @return  bool
235
     */
236 4
    public function analysis_store_results() {
237 4
        return $this->values["analysis"]["store_results"];
238
    }
239
240
    /**
241
     * @return  string[]
242
     */
243 5
    public function rules_schemas() {
244 5
        return $this->values["rules"]["schemas"];
245
    }
246
247
    /**
248
     * @return  string[]
249
     */
250 4
    public function rules_properties() {
251 4
        return $this->values["rules"]["properties"];
252
    }
253
254
    /**
255
     * @return  string[]
256
     */
257 4
    public function rules_variables() {
258 4
        return $this->values["rules"]["variables"];
259
    }
260
261
    /**
262
     * @return  bool
263
     */
264 2
    public function runtime_check_assertions() {
265 2
        return $this->values["runtime"]["check_assertions"];
266
    }
267
268
    /**
269
     * @return Report\Config[]
270
     */
271 2
    public function reports() {
272 2
        if ($this->reports !== null) {
273 2
            return $this->reports;
274
        }
275 1
        $this->reports = [];
276 1
        foreach ($this->values["reports"] as $rep) {
277 1
            $this->reports[] = new Report\Config
278 1
                ( $this->path()
279 1
                , $rep["class"]
280 1
                , $rep["target"]
281 1
                , $rep["config"]
282 1
                , $rep["name"]
283 1
                , $rep["source"]
284 1
                );
285 1
        }
286 1
        return $this->reports;
287
    }
288
}
289