Completed
Push — dev ( 137ae2...762bae )
by James Ekow Abaka
01:17
created

HelpMessageGenerator   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 97.78%

Importance

Changes 0
Metric Value
wmc 17
dl 0
loc 94
ccs 44
cts 45
cp 0.9778
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A formatOptionHelp() 0 10 2
A generate() 0 11 2
A getUsageMessage() 0 3 1
A wrapHelp() 0 6 2
B formatArgument() 0 18 5
A formatValue() 0 4 3
A getOptionHelpMessages() 0 7 2
1
<?php
2
3
namespace clearice\argparser;
4
5
6
class HelpMessageGenerator
7
{
8 1
    public function generate($name, $command, $options, $description, $footer)
9
    {
10 1
        if($command) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

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