Passed
Pull Request — master (#69)
by Dave
01:53
created

CliConfigReader::getOption()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal;
6
7
use _HumbugBoxa35debbd0202\Symfony\Component\Console\Exception\LogicException;
0 ignored issues
show
Bug introduced by
The type _HumbugBoxa35debbd0202\S...xception\LogicException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\SarbException;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\StreamableInputInterface;
11
12
class CliConfigReader
13
{
14
    /**
15
     * @throws InvalidConfigException
16
     */
17
    public static function getArgument(InputInterface $input, string $argumentName): string
18
    {
19
        $value = $input->getArgument($argumentName);
20
21
        if (is_string($value)) {
22
            return $value;
23
        }
24
25
        // Should never happen. Configured option should only return string or null.
26
        throw new LogicException("Incorrectly configured option [$argumentName]"); // @codeCoverageIgnore
27
    }
28
29
    /**
30
     * @throws InvalidConfigException
31
     */
32
    public static function getOption(InputInterface $input, string $optionName): ?string
33
    {
34
        $value = $input->getOption($optionName);
35
36
        if (null === $value) {
37
            return null;
38
        }
39
40
        if (is_string($value)) {
41
            return $value;
42
        }
43
44
        // Should never happen. Configured option should only return string or null.
45
        throw new LogicException("Incorrectly configured option [$optionName]"); // @codeCoverageIgnore
46
    }
47
48
    /**
49
     * @throws InvalidConfigException
50
     */
51
    public static function getOptionWithDefaultValue(InputInterface $input, string $optionName): string
52
    {
53
        $value = self::getOption($input, $optionName);
54
        if (null === $value) {
55
            // Should never happen. Configured option should always have a default value.
56
            throw new LogicException("Incorreclty configured option. No default value set for option {$optionName}"); // @codeCoverageIgnore
57
        }
58
59
        return $value;
60
    }
61
62
    /**
63
     * Returns STDIN as a string.
64
     *
65
     * @throws SarbException
66
     */
67
    public static function getStdin(InputInterface $input): string
68
    {
69
        // If testing this will get input added by `CommandTester::setInputs` method.
70
        $inputSteam = ($input instanceof StreamableInputInterface) ? $input->getStream() : null;
71
72
        // If nothing from input stream use STDIN instead.
73
        $inputSteam = $inputSteam ?? STDIN;
74
75
        $input = stream_get_contents($inputSteam);
76
77
        if (false === $input) {
78
            // No way of easily testing this
79
            throw new SarbException('Can not read input stream'); // @codeCoverageIgnore
80
        }
81
82
        return $input;
83
    }
84
}
85