Passed
Push — dev ( 762bae...3cf4c5 )
by James Ekow Abaka
01:27
created

HelpMessageGenerator::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 16
cts 16
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 5
crap 2
1
<?php
2
3
namespace clearice\argparser;
4
5
6
class HelpMessageGenerator
7
{
8 2
    public function generate($name, $command, $options, $description, $footer)
9
    {
10 2
        if($command) {
11 1
            return sprintf(
12 1
                "%s\n\n%s\n\nOptions for %s command:\n%s\nOptions:\n%s\n%s\n",
13 1
                wordwrap($description),
14 1
                $this->getUsageMessage($name, " $command"),
15 1
                $command,
16 1
                $this->getOptionHelpMessages($options, $command),
17 1
                $this->getOptionHelpMessages($options),
18 1
                wordwrap($footer)
19
            );
20
        } else {
21 1
            return sprintf(
22 1
                "%s\n\n%s\n\nOptions:\n%s\n%s\n",
23 1
                wordwrap($description),
24 1
                $this->getUsageMessage($name),
25 1
                $this->getOptionHelpMessages($options),
26 1
                wordwrap($footer)
27
            );
28
        }
29
    }
30
31 2
    private function getUsageMessage($name, $command = '')
32
    {
33 2
        return sprintf("Usage:\n  %s%s [OPTIONS] ...", basename($name), $command);
34
    }
35
36 2
    private function getOptionHelpMessages($options, $command = '')
37
    {
38 2
        $message = "";
39 2
        foreach ($options as $option) {
40 2
            if($option['command'] !== $command) {
41 1
                continue;
42
            }
43 2
            $message .= $this->formatOptionHelp($option) . "\n";
44
        }
45 2
        return $message;
46
    }
47
48
    /**
49
     * Formats the help line of a value which is accepted by an option. If a
50
     * value type is provided in the option, it is used if not it uses a generic
51
     * "VALUE" to show that an option can accept a value.
52
     *
53
     * @param array $option
54
     * @return string
55
     */
56 2
    private function formatValue($option)
57
    {
58 2
        if (isset($option['type'])) {
59 2
            return "=" . (isset($option['value']) ? $option['value'] : "VALUE");
60
        }
61 2
    }
62
63 2
    private function formatOptionHelp($option)
64
    {
65 2
        $optionHelp = array();
66 2
        $help = explode("\n", wordwrap($option['help'], 50));
67 2
        $argumentPart = $this->formatArgument($option);
68 2
        $optionHelp[] = $this->wrapHelp($argumentPart, $help);
69 2
        foreach ($help as $helpLine) {
70 1
            $optionHelp[] = str_repeat(' ', 29) . "$helpLine";
71
        }
72 2
        return implode("\n", $optionHelp);
73
    }
74
75 2
    private function formatArgument($option)
76
    {
77 2
        $valueHelp = $this->formatValue($option);
78 2
        $argumentHelp = "";
79 2
        if (isset($option['name']) && isset($option['short_name'])) {
80 2
            $argumentHelp = sprintf(
81 2
                "  %s, %-22s ", "-{$option['short_name']}", "--{$option['name']}$valueHelp"
82
            );
83 1
        } else if (isset($option['name'])) {
84 1
            $argumentHelp = sprintf(
85 1
                "  %-27s", "--{$option['name']}$valueHelp"
86
            );
87 1
        } else if (isset($option['short_name'])) {
88 1
            $argumentHelp = sprintf(
89 1
                "  %-27s", "-{$option['short_name']}"
90
            );
91
        }
92 2
        return $argumentHelp;
93
    }
94
95
    /**
96
     * Wraps the help message arround the argument by producing two different
97
     * columns. The argument is placed in the first column and the help message
98
     * is placed in the second column.
99
     *
100
     * @param string $argumentPart
101
     * @param array $help
102
     * @param integer $minSize
103
     * @return string
104
     */
105 2
    private function wrapHelp($argumentPart, &$help, $minSize = 29)
106
    {
107 2
        if (strlen($argumentPart) <= $minSize) {
108 2
            return $argumentPart . array_shift($help);
109
        } else {
110 1
            return $argumentPart;
111
        }
112
    }
113
}