Completed
Push — dev ( 8e5bf0...137ae2 )
by James Ekow Abaka
02:19
created

HelpMessageGenerator::formatOptionHelp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
3
namespace clearice\argparser;
4
5
6
class HelpMessageGenerator
7
{
8
    public function generate($name, $command, $options, $description, $footer)
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

8
    public function generate($name, /** @scrutinizer ignore-unused */ $command, $options, $description, $footer)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
9
    {
10
        return sprintf(
11
            "%s\n\n%s\n\nOptions:\n%s\n%s\n",
12
            wordwrap($description),
13
            $this->getUsageMessage($name),
14
            $this->getOptionHelpMessages($options),
15
            wordwrap($footer)
16
        );
17
    }
18
19
    private function getUsageMessage($name)
20
    {
21
        return sprintf("Usage:\n  %s [OPTIONS] ...", basename($name));
22
    }
23
24
    private function getOptionHelpMessages($options)
25
    {
26
        $message = "";
27
        foreach ($options as $option) {
28
            $message .= $this->formatOptionHelp($option) . "\n";
29
        }
30
        return $message;
31
    }
32
33
    /**
34
     * Formats the help line of a value which is accepted by an option. If a
35
     * value type is provided in the option, it is used if not it uses a generic
36
     * "VALUE" to show that an option can accept a value.
37
     *
38
     * @param array $option
39
     * @return string
40
     */
41
    private function formatValue($option)
42
    {
43
        if (isset($option['type'])) {
44
            return "=" . (isset($option['value']) ? $option['value'] : "VALUE");
45
        }
46
    }
47
48
    private function formatOptionHelp($option)
49
    {
50
        $optionHelp = array();
51
        $help = explode("\n", wordwrap($option['help'], 50));
52
        $argumentPart = $this->formatArgument($option);
53
        $optionHelp[] = $this->wrapHelp($argumentPart, $help);
54
        foreach ($help as $helpLine) {
55
            $optionHelp[] = str_repeat(' ', 29) . "$helpLine";
56
        }
57
        return implode("\n", $optionHelp);
58
    }
59
60
    private function formatArgument($option)
61
    {
62
        $valueHelp = $this->formatValue($option);
63
        $argumentHelp = "";
64
        if (isset($option['name']) && isset($option['short_name'])) {
65
            $argumentHelp = sprintf(
66
                "  %s, %-22s ", "-{$option['short_name']}", "--{$option['name']}$valueHelp"
67
            );
68
        } else if (isset($option['name'])) {
69
            $argumentHelp = sprintf(
70
                "  %-27s", "--{$option['name']}$valueHelp"
71
            );
72
        } else if (isset($option['short_name'])) {
73
            $argumentHelp = sprintf(
74
                "  %-27s", "-{$option['short_name']}"
75
            );
76
        }
77
        return $argumentHelp;
78
    }
79
80
    /**
81
     * Wraps the help message arround the argument by producing two different
82
     * columns. The argument is placed in the first column and the help message
83
     * is placed in the second column.
84
     *
85
     * @param string $argumentPart
86
     * @param array $help
87
     * @param integer $minSize
88
     * @return string
89
     */
90
    private function wrapHelp($argumentPart, &$help, $minSize = 29)
91
    {
92
        if (strlen($argumentPart) <= $minSize) {
93
            return $argumentPart . array_shift($help);
94
        } else {
95
            return $argumentPart;
96
        }
97
    }
98
}