1
|
|
|
<?php |
2
|
|
|
namespace Peridot\Console; |
3
|
|
|
|
4
|
|
|
use Peridot\Configuration; |
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The ConfigurationReader is responsible for building a Configuration |
9
|
|
|
* object from an InputInterface. |
10
|
|
|
* |
11
|
|
|
* @package Peridot\Console |
12
|
|
|
*/ |
13
|
|
|
class ConfigurationReader |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
17
|
|
|
*/ |
18
|
|
|
protected $input; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param InputInterface $input |
22
|
|
|
*/ |
23
|
|
|
public function __construct(InputInterface $input) |
24
|
|
|
{ |
25
|
|
|
$this->input = $input; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Read configuration information from input |
30
|
|
|
* |
31
|
|
|
* @return Configuration |
32
|
|
|
*/ |
33
|
|
|
public function read() |
34
|
|
|
{ |
35
|
|
|
$configuration = new Configuration(); |
36
|
|
|
|
37
|
|
|
$options = [ |
38
|
|
|
'focus' => [$configuration, 'setFocusPattern'], |
39
|
|
|
'skip' => [$configuration, 'setSkipPattern'], |
40
|
|
|
'grep' => [$configuration, 'setGrep'], |
41
|
|
|
'no-colors' => [$configuration, 'disableColors'], |
42
|
|
|
'force-colors' => [$configuration, 'enableColorsExplicit'], |
43
|
|
|
'bail' => [$configuration, 'stopOnFailure'], |
44
|
|
|
'configuration' => [$configuration, 'setConfigurationFile'] |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
if ($path = $this->input->getArgument('path')) { |
48
|
|
|
$configuration->setPath($path); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
foreach ($options as $option => $callable) { |
52
|
|
|
$this->callForOption($option, $callable); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $configuration; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Static access to reader |
60
|
|
|
* |
61
|
|
|
* @param InputInterface $input |
62
|
|
|
* @return Configuration |
63
|
|
|
*/ |
64
|
|
|
public static function readInput(InputInterface $input) |
65
|
|
|
{ |
66
|
|
|
$reader = new static($input); |
67
|
|
|
|
68
|
|
|
return $reader->read(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Execute a callback if the input object has a value for the |
73
|
|
|
* given option name. |
74
|
|
|
* |
75
|
|
|
* @param string $optionName |
76
|
|
|
* @param callable $callable |
77
|
|
|
*/ |
78
|
|
|
protected function callForOption($optionName, callable $callable) |
79
|
|
|
{ |
80
|
|
|
$value = $this->input->getOption($optionName); |
81
|
|
|
if ($value) { |
82
|
|
|
call_user_func_array($callable, [$value]); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.