Passed
Pull Request — master (#75)
by Dave
02:21 queued 11s
created

CliConfigReader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 60
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getOption() 0 11 2
A getStdin() 0 16 3
A getOptionWithDefaultValue() 0 6 1
A getArgument() 0 6 1
A getBooleanOption() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\SarbException;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\StreamableInputInterface;
10
use Webmozart\Assert\Assert;
11
12
class CliConfigReader
13
{
14
    public static function getArgument(InputInterface $input, string $argumentName): string
15
    {
16
        $value = $input->getArgument($argumentName);
17
        Assert::string($value);
18
19
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value could return the type null|string[] which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
20
    }
21
22
    public static function getOption(InputInterface $input, string $optionName): ?string
23
    {
24
        $value = $input->getOption($optionName);
25
26
        if (null === $value) {
27
            return null;
28
        }
29
30
        Assert::string($value);
31
32
        return $value;
33
    }
34
35
    public static function getOptionWithDefaultValue(InputInterface $input, string $optionName): string
36
    {
37
        $value = self::getOption($input, $optionName);
38
        Assert::notNull($value);
39
40
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
41
    }
42
43
    /**
44
     * Returns STDIN as a string.
45
     *
46
     * @throws SarbException
47
     */
48
    public static function getStdin(InputInterface $input): string
49
    {
50
        // If testing this will get input added by `CommandTester::setInputs` method.
51
        $inputSteam = ($input instanceof StreamableInputInterface) ? $input->getStream() : null;
52
53
        // If nothing from input stream use STDIN instead.
54
        $inputSteam = $inputSteam ?? \STDIN;
55
56
        $input = stream_get_contents($inputSteam);
57
58
        if (false === $input) {
59
            // No way of easily testing this
60
            throw new SarbException('Can not read input stream'); // @codeCoverageIgnore
61
        }
62
63
        return $input;
64
    }
65
66
    public static function getBooleanOption(InputInterface $input, string $argument): bool
67
    {
68
        $option = $input->getOption($argument);
69
        Assert::boolean($option);
70
71
        return $option;
72
    }
73
}
74