ConfigurationBuilder   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 20
c 5
b 0
f 0
lcom 1
cbo 4
dl 0
loc 208
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getDriver() 0 4 1
A getReporter() 0 4 1
A getIncludeFiles() 0 4 1
A getExcludeFiles() 0 4 1
A getCoverageBounds() 0 4 1
A getReportDirectory() 0 4 1
A __construct() 0 5 1
A driver() 0 5 1
A reporter() 0 5 1
A includeFile() 0 5 1
A excludeFile() 0 5 1
A includeFiles() 0 7 2
A excludeFiles() 0 7 2
A reportDirectory() 0 5 1
A coverageBounds() 0 5 1
A detectDriver() 0 14 2
A build() 0 15 1
1
<?php
2
3
/**
4
 * This file is part of cloak.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace cloak\configuration;
13
14
use cloak\AnalyzerConfiguration;
15
use cloak\value\CoverageBounds;
16
use cloak\analyzer\Analyzer;
17
use cloak\analyzer\AnalyzeDriver;
18
use cloak\analyzer\AdapterResolver;
19
use cloak\reporter\Reporter;
20
21
22
/**
23
 * Class ConfigurationBuilder
24
 * @package cloak\configuration
25
 */
26
class ConfigurationBuilder
27
{
28
29
    /**
30
     * @var \cloak\analyzer\AnalyzeDriver
31
     */
32
    private $driver;
33
34
    /**
35
     * @var \cloak\reporter\Reporter
36
     */
37
    private $reporter;
38
39
    /**
40
     * @var string[]
41
     */
42
    private $includeFiles;
43
44
    /**
45
     * @var string[]
46
     */
47
    private $excludeFiles;
48
49
    /**
50
     * @var \cloak\value\CoverageBounds
51
     */
52
    private $coverageBounds;
53
54
    /**
55
     * @var string
56
     */
57
    private $reportDirectory;
58
59
60
    public function __construct()
61
    {
62
        $this->includeFiles = [];
63
        $this->excludeFiles = [];
64
    }
65
66
    /**
67
     * @param AnalyzeDriver $driver
68
     * @return $this
69
     */
70
    public function driver(AnalyzeDriver $driver)
71
    {
72
        $this->driver = $driver;
73
        return $this;
74
    }
75
76
    /**
77
     * @param Reporter $reporter
78
     * @return $this
79
     */
80
    public function reporter(Reporter $reporter)
81
    {
82
        $this->reporter = $reporter;
83
        return $this;
84
    }
85
86
    /**
87
     * @param string $pattern
88
     * @return $this
89
     */
90
    public function includeFile($pattern)
91
    {
92
        $this->includeFiles[] = $pattern;
93
        return $this;
94
    }
95
96
    /**
97
     * @param string $pattern
98
     * @return $this
99
     */
100
    public function excludeFile($pattern)
101
    {
102
        $this->excludeFiles[] = $pattern;
103
        return $this;
104
    }
105
106
    /**
107
     * @param string[] $patterns
108
     * @return $this
109
     */
110
    public function includeFiles(array $patterns)
111
    {
112
        foreach ($patterns as $pattern) {
113
            $this->includeFile($pattern);
114
        }
115
        return $this;
116
    }
117
118
    /**
119
     * @param string[] $patterns
120
     * @return $this
121
     */
122
    public function excludeFiles(array $patterns)
123
    {
124
        foreach ($patterns as $pattern) {
125
            $this->excludeFile($pattern);
126
        }
127
        return $this;
128
    }
129
130
    /**
131
     * @param string $directoryPath
132
     * @return $this
133
     */
134
    public function reportDirectory($directoryPath)
135
    {
136
        $this->reportDirectory = $directoryPath;
137
        return $this;
138
    }
139
140
    /**
141
     * @param float $critical
142
     * @param float $satisfactory
143
     * @return $this
144
     */
145
    public function coverageBounds($critical, $satisfactory)
146
    {
147
        $this->coverageBounds = new CoverageBounds($critical, $satisfactory);
148
        return $this;
149
    }
150
151
    protected function detectDriver()
152
    {
153
        if ($this->driver instanceof AnalyzeDriver) {
154
            return;
155
        }
156
157
        $resolver = new AdapterResolver([
158
            '\cloak\analyzer\adapter\XdebugAdapter',
159
            '\cloak\analyzer\adapter\HHVMAdapter'
160
        ]);
161
        $adapter = $resolver->resolve();
162
163
        $this->driver = new Analyzer($adapter);
0 ignored issues
show
Bug introduced by
It seems like $adapter defined by $resolver->resolve() on line 161 can be null; however, cloak\analyzer\Analyzer::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
164
    }
165
166
    /**
167
     * @return AnalyzeDriver
168
     */
169
    public function getDriver()
170
    {
171
        return $this->driver;
172
    }
173
174
    /**
175
     * @return Reporter
176
     */
177
    public function getReporter()
178
    {
179
        return $this->reporter;
180
    }
181
182
    /**
183
     * @return string[]
184
     */
185
    public function getIncludeFiles()
186
    {
187
        return $this->includeFiles;
188
    }
189
190
    /**
191
     * @return string[]
192
     */
193
    public function getExcludeFiles()
194
    {
195
        return $this->excludeFiles;
196
    }
197
198
    /**
199
     * @return CoverageBounds
200
     */
201
    public function getCoverageBounds()
202
    {
203
        return $this->coverageBounds;
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public function getReportDirectory()
210
    {
211
        return $this->reportDirectory;
212
    }
213
214
    /**
215
     * @return AnalyzerConfiguration
216
     */
217
    public function build()
218
    {
219
        $this->detectDriver();
220
221
        $values = [
222
            'driver' => $this->driver,
223
            'reporter' => $this->reporter,
224
            'includeFiles' => $this->includeFiles,
225
            'excludeFiles' => $this->excludeFiles,
226
            'coverageBounds' => $this->coverageBounds,
227
            'reportDirectory' => $this->reportDirectory
228
        ];
229
230
        return new AnalyzerConfiguration($values);
231
    }
232
233
}
234