Completed
Push — dev ( a3b92c...7759a2 )
by James Ekow Abaka
01:26
created

HelpMessageGenerator::getOptionHelpMessages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace clearice\argparser;
4
5
6
class HelpMessageGenerator
7
{
8 1
    public function generate($name, $options, $description, $footer)
9
    {
10 1
        return sprintf(
11 1
            "%s\n\n%s\n\nOptions:\n%s\n%s\n",
12 1
            wordwrap($description),
13 1
            $this->getUsageMessage($name),
14 1
            $this->getOptionHelpMessages($options),
15 1
            wordwrap($footer)
16
        );
17
    }
18
19 1
    public function getUsageMessage($name)
20
    {
21 1
        return sprintf("Usage:\n  %s [OPTIONS] ...", basename($name));
22
    }
23
24 1
    public function getOptionHelpMessages($options)
25
    {
26 1
        $message = "";
27 1
        foreach ($options as $option) {
28 1
            $message .= $this->formatOptionHelp($option) . "\n";
29
        }
30 1
        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 1
    private function formatValue($option)
42
    {
43 1
        if (isset($option['type'])) {
44 1
            return "=" . (isset($option['value']) ? $option['value'] : "VALUE");
45
        }
46 1
    }
47
48 1
    private function formatOptionHelp($option)
49
    {
50 1
        $optionHelp = array();
51 1
        $help = explode("\n", wordwrap($option['help'], 50));
52 1
        $argumentPart = $this->formatArgument($option);
53 1
        $optionHelp[] = $this->wrapHelp($argumentPart, $help);
0 ignored issues
show
Documentation introduced by
$help is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54 1
        foreach ($help as $helpLine) {
0 ignored issues
show
Bug introduced by
The expression $help of type string is not traversable.
Loading history...
55 1
            $optionHelp[] = str_repeat(' ', 29) . "$helpLine";
56
        }
57 1
        return implode("\n", $optionHelp);
58
    }
59
60 1
    private function formatArgument($option)
61
    {
62 1
        $valueHelp = $this->formatValue($option);
63 1
        $argumentHelp = "";
64 1
        if (isset($option['name']) && isset($option['short_name'])) {
65 1
            $argumentHelp = sprintf(
66 1
                "  %s, %-22s ", "-{$option['short_name']}", "--{$option['name']}$valueHelp"
67
            );
68 1
        } else if (isset($option['name'])) {
69 1
            $argumentHelp = sprintf(
70 1
                "  %-27s", "--{$option['name']}$valueHelp"
71
            );
72 1
        } else if (isset($option['short_name'])) {
73 1
            $argumentHelp = sprintf(
74 1
                "  %-27s", "-{$option['short_name']}"
75
            );
76
        }
77 1
        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 string $help
87
     * @param integer $minSize
88
     * @return string
89
     */
90 1
    private function wrapHelp($argumentPart, &$help, $minSize = 29)
91
    {
92 1
        if (strlen($argumentPart) <= $minSize) {
93 1
            return $argumentPart . array_shift($help);
94
        } else {
95 1
            return $argumentPart;
96
        }
97
    }
98
}