Passed
Push — code-quality-badges ( 22ddc0...88262e )
by Christian
02:49
created

ArgumentDescriber::formatDefaultValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Console\Documentation;
4
5
use Symfony\Component\Console\Formatter\OutputFormatter;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
final class ArgumentDescriber
10
{
11
12
    /**
13
     * @var OutputInterface
14
     */
15
    private $output;
16
17
    public function __construct(OutputInterface $output)
18
    {
19
        $this->output = $output;
20
    }
21
22
    public function describe(InputArgument $argument)
23
    {
24
        $description = $this->removeDecoration($argument->getDescription());
25
        $default = $this->formatDefaultValue($argument);
26
27
        $this->write(
28
            '#### `'.$this->argumentName($argument)."`\n\n"
29
            .$this->formatArgumentDescription($description)
30
            .'* Is required: '.$this->argumentIsRequired($argument)."\n"
31
            .'* Is array: '.$this->argumentIsArray($argument)."\n"
32
            .'* Default: `'.str_replace("\n", '', var_export($default, true)).'`'
33
        );
34
    }
35
36
    private function removeDecoration(string $string): string
37
    {
38
        $f = new OutputFormatter();
39
40
        return $f->format($string);
41
    }
42
43
    /**
44
     * Writes content to output.
45
     *
46
     * @param string $content
47
     * @param bool $decorated
48
     */
49
    private function write($content, $decorated = false)
50
    {
51
        $this->output->write($content,
52
            false,
53
            $decorated ? OutputInterface::OUTPUT_NORMAL : OutputInterface::OUTPUT_RAW);
54
    }
55
56
    /**
57
     * @param InputArgument $argument
58
     * @return string
59
     */
60
    private function argumentName(InputArgument $argument): string
61
    {
62
        return ($argument->getName() ?: '<none>');
63
    }
64
65
    /**
66
     * @param $description
67
     * @return string
68
     */
69
    private function formatArgumentDescription($description): string
70
    {
71
        return ($description ? preg_replace('/\s*[\r\n]\s*/',
72
                "\n",
73
                $description)."\n\n" : '');
74
    }
75
76
    /**
77
     * @param InputArgument $argument
78
     * @return string
79
     */
80
    private function argumentIsRequired(InputArgument $argument): string
81
    {
82
        return ($argument->isRequired() ? 'yes' : 'no');
83
    }
84
85
    /**
86
     * @param InputArgument $argument
87
     * @return string
88
     */
89
    private function argumentIsArray(InputArgument $argument): string
90
    {
91
        return ($argument->isArray() ? 'yes' : 'no');
92
    }
93
94
    /**
95
     * @param InputArgument $argument
96
     * @return mixed|string
97
     */
98
    private function formatDefaultValue(InputArgument $argument)
99
    {
100
        $default = $argument->getDefault();
101
        if (is_string($default)) {
102
            $default = $this->removeDecoration($argument->getDefault());
103
        }
104
105
        return $default;
106
    }
107
}