Passed
Push — master ( 1dba41...0db4d1 )
by Anton
02:05
created

Option::toString()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 39
ccs 15
cts 15
cp 1
crap 5
rs 8.9848
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Console\Input;
9
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
13
final class Option
14
{
15 33
    public static function toString(
16
        InputInterface $input,
17
        InputOption $option
18
    ): string {
19 33
        $name = $option->getName();
20
21 33
        if (!$option->acceptValue()) {
22 5
            return \sprintf(
23 5
                '--%s',
24
                $name
25
            );
26
        }
27
28 28
        if (!$option->isArray()) {
29 16
            return self::generatePartialOption(
30
                $option,
31
                $name,
32 16
                $input->getOption($name)
33
            );
34
        }
35
36
        /** @var string[] $outputs */
37 12
        $outputs = [];
38 12
        foreach ($input->getOption($name) as $value) {
39 12
            $value = self::generatePartialOption(
40
                $option,
41
                $name,
42
                $value
43
            );
44
45 12
            if ($value === '') {
46 4
                continue;
47
            }
48
49 9
            $outputs[] = $value;
50
        }
51
52 12
        return \implode(' ', $outputs);
53
    }
54
55
    /**
56
     * @param null|string $value
57
     */
58 28
    private static function generatePartialOption(
59
        InputOption $option,
60
        string $name,
61
        $value
62
    ): string {
63 28
        if (\null !== $value && \strlen($value) !== 0) {
64 14
            return \sprintf(
65 14
                '--%s=%s',
66
                $name,
67
                $value
68
            );
69
        }
70
71 16
        if ($option->isValueOptional()) {
72 6
            return \sprintf(
73 6
                '--%s',
74
                $name
75
            );
76
        }
77
78 10
        return '';
79
    }
80
}
81