RootConfig   B
last analyzed

Complexity

Total Complexity 49

Size/Duplication

Total Lines 240
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.74%

Importance

Changes 26
Bugs 2 Features 6
Metric Value
wmc 49
c 26
b 2
f 6
lcom 1
cbo 2
dl 0
loc 240
ccs 126
cts 133
cp 0.9474
rs 8.5454

28 Methods

Rating   Name   Duplication   Size   Complexity  
A setOverrides() 0 6 2
B setOverride() 0 24 5
A init() 0 16 1
A initTarget() 0 10 2
A initRootHref() 0 6 2
B initCommonMarkExtensions() 0 18 5
A initTemplate() 0 6 2
A initTocDepth() 0 6 2
A initCopyright() 0 6 2
A initConversionProcess() 0 6 2
A initHeadingsProcess() 0 6 2
A initCopyImageProcess() 0 6 2
A initCopyrightProcess() 0 6 2
A initTocProcess() 0 6 2
A initRenderingProcess() 0 6 2
A getConversionProcess() 0 4 1
A getHeadingsProcess() 0 4 1
A getCopyImageProcess() 0 4 1
A getCopyrightProcess() 0 4 1
A getTocProcess() 0 4 1
A getRenderingProcess() 0 4 1
A getTarget() 0 4 1
A getRootHref() 0 4 1
A getTemplate() 0 4 1
A getTocDepth() 0 4 1
A getCopyright() 0 4 1
A get() 0 7 2
A getCommonMarkExtensions() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like RootConfig often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RootConfig, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Bookdown\Bookdown\Config;
3
4
use Bookdown\Bookdown\Exception;
5
6
class RootConfig extends IndexConfig
7
{
8
    protected $target;
9
    protected $conversionProcess;
10
    protected $renderingProcess;
11
    protected $tocProcess;
12
    protected $headingsProcess;
13
    protected $copyImageProcess;
14
    protected $copyrightProcess;
15
    protected $template;
16
    protected $rootHref;
17
    protected $tocDepth;
18
    protected $copyright;
19
20
    /**
21
     * @var array
22
     */
23
    protected $commonMarkExtensions = array();
24
25 5
    public function setOverrides(array $overrides)
26
    {
27 5
        foreach ($overrides as $key => $val) {
28 2
            $this->setOverride($key, $val);
29 5
        }
30 5
    }
31
32 2
    protected function setOverride($key, $val)
33
    {
34 2
        $val = trim($val);
35 2
        if (! $val) {
36
            return;
37
        }
38
39 2
        $key = ltrim($key, '-');
40
41 2
        if ($key === 'template') {
42 1
            $this->template = $val;
43 1
            return;
44
        }
45
46 2
        if ($key === 'target') {
47 1
            $this->target = rtrim($val, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
48 1
            return;
49
        }
50
51 1
        if ($key === 'root-href') {
52
            $this->rootHref = $val;
53
            return;
54
        }
55 1
    }
56
57 29
    protected function init()
58
    {
59 29
        parent::init();
60 29
        $this->initTarget();
61 28
        $this->initRootHref();
62 28
        $this->initCommonMarkExtensions();
63 28
        $this->initTemplate();
64 28
        $this->initTocDepth();
65 28
        $this->initCopyright();
66 28
        $this->initConversionProcess();
67 28
        $this->initHeadingsProcess();
68 28
        $this->initCopyImageProcess();
69 28
        $this->initTocProcess();
70 28
        $this->initRenderingProcess();
71 28
        $this->initCopyrightProcess();
72 28
    }
73
74 29
    protected function initTarget()
75
    {
76 29
        if (empty($this->json->target)) {
77 1
            throw new Exception("No target set in '{$this->file}'.");
78
        }
79
80 28
        $target = $this->json->target;
81 28
        $target = rtrim($this->fixPath($target), DIRECTORY_SEPARATOR);
82 28
        $this->target = $target . DIRECTORY_SEPARATOR;
83 28
    }
84
85 28
    protected function initRootHref()
86
    {
87 28
        $this->rootHref = empty($this->json->rootHref)
88 28
            ? '/'
89 28
            : $this->json->rootHref;
90 28
    }
91
92 28
    protected function initCommonMarkExtensions()
93
    {
94 28
        if (empty($this->json->extensions)
95
            || empty($this->json->extensions->commonmark)
96 28
        ) {
97 13
            return;
98
        }
99
100 15
        if (!is_array($this->json->extensions->commonmark)) {
101
            throw new \InvalidArgumentException(
102
                sprintf('The extension parameter "commonmark" must be of type "array".')
103
            );
104
        }
105
106 15
        foreach ($this->json->extensions->commonmark as $extension) {
107 15
            $this->commonMarkExtensions[] = $extension;
108 15
        }
109 15
    }
110
111 28
    protected function initTemplate()
112
    {
113 28
        $this->template = empty($this->json->template)
114 28
            ? null
115 28
            : $this->fixPath($this->json->template);
116 28
    }
117
118 29
    protected function initTocDepth()
119
    {
120 29
        $this->tocDepth = empty($this->json->tocDepth)
121 29
            ? 0
122 29
            : (int) $this->json->tocDepth;
123 29
    }
124
125 28
    protected function initCopyright()
126
    {
127 28
        $this->copyright = empty($this->json->copyright)
128 28
            ? ''
129 28
            : $this->json->copyright;
130 28
    }
131
132
133 28
    protected function initConversionProcess()
134
    {
135 28
        $this->conversionProcess = empty($this->json->conversionProcess)
136 28
            ? 'Bookdown\Bookdown\Process\Conversion\ConversionProcessBuilder'
137 28
            : $this->json->conversionProcess;
138 28
    }
139
140 28
    protected function initHeadingsProcess()
141
    {
142 28
        $this->headingsProcess = empty($this->json->headingsProcess)
143 28
            ? 'Bookdown\Bookdown\Process\Headings\HeadingsProcessBuilder'
144 28
            : $this->json->headingsProcess;
145 28
    }
146
147 28
    protected function initCopyImageProcess()
148
    {
149 28
        $this->copyImageProcess = empty($this->json->copyImageProcess)
150 28
            ? 'Bookdown\Bookdown\Process\Resource\CopyImageProcessBuilder'
151 28
            : $this->json->copyImageProcess;
152 28
    }
153
154 28
    protected function initCopyrightProcess()
155
    {
156 28
        $this->copyrightProcess = empty($this->json->copyrightProcess)
157 28
            ? 'Bookdown\Bookdown\Process\Info\CopyrightProcessBuilder'
158 28
            : $this->json->copyrightProcess;
159 28
    }
160
161 28
    protected function initTocProcess()
162
    {
163 28
        $this->tocProcess = empty($this->json->tocProcess)
164 28
            ? 'Bookdown\Bookdown\Process\Toc\TocProcessBuilder'
165 28
            : $this->json->tocProcess;
166 28
    }
167
168 28
    protected function initRenderingProcess()
169
    {
170 28
        $this->renderingProcess = empty($this->json->renderingProcess)
171 28
            ? 'Bookdown\Bookdown\Process\Rendering\RenderingProcessBuilder'
172 28
            : $this->json->renderingProcess;
173 28
    }
174
175 19
    public function getConversionProcess()
176
    {
177 19
        return $this->conversionProcess;
178
    }
179
180 16
    public function getHeadingsProcess()
181
    {
182 16
        return $this->headingsProcess;
183
    }
184
185 3
    public function getCopyImageProcess()
186
    {
187 3
        return $this->copyImageProcess;
188
    }
189
190 3
    public function getCopyrightProcess()
191
    {
192 3
        return $this->copyrightProcess;
193
    }
194
195 15
    public function getTocProcess()
196
    {
197 15
        return $this->tocProcess;
198
    }
199
200 5
    public function getRenderingProcess()
201
    {
202 5
        return $this->renderingProcess;
203
    }
204
205 24
    public function getTarget()
206
    {
207 24
        return $this->target;
208
    }
209
210 19
    public function getRootHref()
211
    {
212 19
        return $this->rootHref;
213
    }
214
215 6
    public function getTemplate()
216
    {
217 6
        return $this->template;
218
    }
219
220 6
    public function getTocDepth()
221
    {
222 6
        return $this->tocDepth;
223
    }
224
225 2
    public function getCopyright()
226
    {
227 2
        return $this->copyright;
228
    }
229
230 1
    public function get($key, $alt = null)
231
    {
232 1
        if (isset($this->json->$key)) {
233 1
            return $this->json->$key;
234
        }
235 1
        return $alt;
236
    }
237
238
    /**
239
     * @return array
240
     */
241 18
    public function getCommonMarkExtensions()
242
    {
243 18
        return $this->commonMarkExtensions;
244
    }
245
}
246