ConfigurationReader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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);
0 ignored issues
show
Bug introduced by
It seems like $path defined by $this->input->getArgument('path') on line 47 can also be of type array<integer,string>; however, Peridot\Configuration::setPath() does only seem to accept string, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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