Passed
Push — master ( 308d02...325a65 )
by Christian
02:19
created

ArgumentDescriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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
8
final class ArgumentDescriber
9
{
10
11
    public function describe(InputArgument $argument)
12
    {
13
        $description = $this->removeDecoration($argument->getDescription());
14
        $default = $this->formatDefaultValue($argument);
15
16
        return
17
            '#### `'.$this->argumentName($argument)."`\n\n"
18
            .$this->formatArgumentDescription($description)
19
            .'* Is required: '.$this->argumentIsRequired($argument)."\n"
20
            .'* Is array: '.$this->argumentIsArray($argument)."\n"
21
            .'* Default: `'.str_replace("\n", '', var_export($default, true)).'`';
22
    }
23
24
    private function removeDecoration(string $string): string
25
    {
26
        $f = new OutputFormatter();
27
28
        return $f->format($string);
29
    }
30
31
    /**
32
     * @param InputArgument $argument
33
     * @return string
34
     */
35
    private function argumentName(InputArgument $argument): string
36
    {
37
        return ($argument->getName() ?: '<none>');
38
    }
39
40
    /**
41
     * @param $description
42
     * @return string
43
     */
44
    private function formatArgumentDescription($description): string
45
    {
46
        return ($description ? preg_replace('/\s*[\r\n]\s*/',
47
                "\n",
48
                $description)."\n\n" : '');
49
    }
50
51
    /**
52
     * @param InputArgument $argument
53
     * @return string
54
     */
55
    private function argumentIsRequired(InputArgument $argument): string
56
    {
57
        return ($argument->isRequired() ? 'yes' : 'no');
58
    }
59
60
    /**
61
     * @param InputArgument $argument
62
     * @return string
63
     */
64
    private function argumentIsArray(InputArgument $argument): string
65
    {
66
        return ($argument->isArray() ? 'yes' : 'no');
67
    }
68
69
    /**
70
     * @param InputArgument $argument
71
     * @return mixed|string
72
     */
73
    private function formatDefaultValue(InputArgument $argument)
74
    {
75
        $default = $argument->getDefault();
76
        if (is_string($default)) {
77
            $default = $this->removeDecoration($argument->getDefault());
78
        }
79
80
        return $default;
81
    }
82
}