Option::toString()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
nc 8
nop 2
dl 0
loc 36
ccs 19
cts 19
cp 1
crap 8
rs 8.0995
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 35
    public static function toString(
16
        InputInterface $input,
17
        InputOption $option
18
    ): string {
19 35
        $name = $option->getName();
20 35
        $values = $input->getOption($name);
21
22 35
        if (!$option->acceptValue()) {
23 6
            return true === $values
24 5
                ? \sprintf('--%s', $name)
25 6
                : '';
26
        }
27
28 29
        if (!$option->isArray()) {
29 16
            $values = [$values];
30
        }
31
32 29
        $isValueRequired = $option->isValueRequired();
33
        /** @var string[] $outputs */
34 29
        $outputs = [];
35 29
        foreach ($values as $value) {
36 29
            if ($isValueRequired && \null === $value) {
37 8
                continue;
38
            }
39 23
            $value = sprintf(
40 23
                '--%s%s%s',
41
                $name,
42 23
                \null === $value ? '' : '=',
43
                $value
44
            );
45
46 23
            $outputs[] = $value;
47
        }
48
49 29
        return \implode(' ', $outputs);
50
    }
51
}
52