Completed
Push — multiple-paths ( fe1fe4 )
by Erin
01:52
created

Configuration::getDsl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Peridot;
3
4
/**
5
 * Configuration stores configured values used through the Peridot application
6
 * lifecycle.
7
 *
8
 * @package Peridot
9
 */
10
class Configuration
11
{
12
    /**
13
     * @var boolean
14
     */
15
    protected $colorsEnabled = true;
16
17
    /**
18
     * @var boolean
19
     */
20
    protected $colorsEnableExplicit = false;
21
22
    /**
23
     * @var string
24
     */
25
    protected $grep = '*.spec.php';
26
27
    /**
28
     * @var string
29
     */
30
    protected $reporter = 'spec';
31
32
    /**
33
     * @var string
34
     */
35
    protected $path;
36
37
    /**
38
     * @var array
39
     */
40
    protected $paths;
41
42
    /**
43
     * @var string
44
     */
45
    protected $configurationFile;
46
47
    /**
48
     * @var string
49
     */
50
    protected $dsl;
51
52
    /**
53
     * @var bool
54
     */
55
    protected $stopOnFailure = false;
56
57
    public function __construct()
58
    {
59
        $this->path = getcwd();
60
        $this->paths = [$this->path];
61
        $this->configurationFile = getcwd() . DIRECTORY_SEPARATOR . 'peridot.php';
62
        $this->dsl = __DIR__ . DIRECTORY_SEPARATOR . 'Dsl.php';
63
    }
64
65
    /**
66
     * Set the pattern used to load tests
67
     *
68
     * @param string $grep
69
     * @return $this
70
     */
71
    public function setGrep($grep)
72
    {
73
        return $this->write('grep', $grep);
74
    }
75
76
    /**
77
     * Returns the pattern used to load tests
78
     *
79
     * @return string
80
     */
81
    public function getGrep()
82
    {
83
        return $this->grep;
84
    }
85
86
    /**
87
     * Set the name of the reporter to use
88
     *
89
     * @param string $reporter
90
     * @return $this
91
     */
92
    public function setReporter($reporter)
93
    {
94
        return $this->write('reporter', $reporter);
95
    }
96
97
    /**
98
     * Return the name of the reporter configured for use
99
     *
100
     * @return string
101
     */
102
    public function getReporter()
103
    {
104
        return $this->reporter;
105
    }
106
107
    /**
108
     * Set the path to load tests from
109
     *
110
     * @param string $path
111
     * @return $this
112
     */
113
    public function setPath($path)
114
    {
115
        return $this->writePaths([$path]);
116
    }
117
118
    /**
119
     * Return the path being searched for tests
120
     *
121
     * @return string
122
     */
123
    public function getPath()
124
    {
125
        return $this->paths[0];
126
    }
127
128
    /**
129
     * Set the paths to load tests from
130
     *
131
     * @param array $paths
132
     * @return $this
133
     */
134
    public function setPaths(array $paths)
135
    {
136
        if (empty($paths)) {
137
            throw new \InvalidArgumentException('Paths cannot be empty.');
138
        }
139
140
        return $this->writePaths($paths);
141
    }
142
143
    /**
144
     * Return the paths being searched for tests
145
     *
146
     * @return array
147
     */
148
    public function getPaths()
149
    {
150
        return $this->paths;
151
    }
152
153
    /**
154
     * Disable output colors
155
     *
156
     * @return $this
157
     */
158
    public function disableColors()
159
    {
160
        if ( $this->colorsEnableExplicit ) {
161
            return $this;
162
        }
163
164
        return $this->write('colorsEnabled', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
    }
166
167
    /**
168
     * Force output colors even without TTY support.
169
     *
170
     * @return $this
171
     */
172
    public function enableColorsExplicit()
173
    {
174
        return $this
175
            ->write('colorsEnableExplicit', true)
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
176
            ->write('colorsEnabled', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
177
    }
178
179
    /**
180
     * Check if output colors are disabled
181
     *
182
     * @return boolean
183
     */
184
    public function areColorsEnabled()
185
    {
186
        return $this->colorsEnableExplicit || $this->colorsEnabled;
187
    }
188
189
    /**
190
     * Check if output colors are explicitly enabled.
191
     *
192
     * @return boolean
193
     */
194
    public function areColorsEnabledExplicit()
195
    {
196
        return $this->colorsEnableExplicit;
197
    }
198
199
    /**
200
     * Stop the suite runner when a failure occurs
201
     *
202
     * @return $this
203
     */
204
    public function stopOnFailure()
205
    {
206
        return $this->write('stopOnFailure', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
207
    }
208
209
    /**
210
     * Check if the suite runner should stop on failure
211
     *
212
     * @return bool
213
     */
214
    public function shouldStopOnFailure()
215
    {
216
        return $this->stopOnFailure;
217
    }
218
219
    /**
220
     * Set the path to a Peridot configuration file
221
     *
222
     * @param string $configurationFile
223
     * @return $this
224
     */
225
    public function setConfigurationFile($configurationFile)
226
    {
227
        $search = [$configurationFile, getcwd() . DIRECTORY_SEPARATOR . $configurationFile];
228
        $found = array_filter($search, 'file_exists');
229
230
        if (count($found) == 0) {
231
            throw new \RuntimeException("Configuration file specified but does not exist");
232
        }
233
234
        $this->write('configurationFile', $found[0]);
235
236
        return $this;
237
    }
238
239
    /**
240
     * Return the path to the Peridot configuration file. Returns a relative
241
     * path if it exists, otherwise return the provided value
242
     *
243
     * @return string
244
     */
245
    public function getConfigurationFile()
246
    {
247
        return $this->configurationFile;
248
    }
249
250
    /**
251
     * Set the path to a DSL file for defining
252
     * the test language used
253
     *
254
     * @param string $dsl
255
     * @return $this
256
     */
257
    public function setDsl($dsl)
258
    {
259
        return $this->write('dsl', $dsl);
260
    }
261
262
    /**
263
     * Get the path to a DSL file containing
264
     * test functions to use
265
     *
266
     * @return string
267
     */
268
    public function getDsl()
269
    {
270
        return $this->dsl;
271
    }
272
273
    /**
274
     * Write a configuration value and persist it to the current
275
     * environment.
276
     *
277
     * @param string $varName
278
     * @param string $value
279
     * @return $this
280
     */
281
    protected function write($varName, $value)
282
    {
283
        $this->$varName = $value;
284
        $parts = preg_split('/(?=[A-Z])/', $varName);
285
        $env = 'PERIDOT_' . strtoupper(join('_', $parts));
286
        putenv($env . '=' . $value);
287
        return $this;
288
    }
289
290
    /**
291
     * Write the paths and persist them to the current environment.
292
     *
293
     * @param array $paths
294
     * @return $this
295
     */
296
    protected function writePaths(array $paths)
297
    {
298
        $this->paths = $paths;
299
        putenv('PERIDOT_PATH=' . $paths[0]);
300
        putenv('PERIDOT_PATHS=' . implode(PATH_SEPARATOR, $paths));
301
        return $this;
302
    }
303
}
304