Summary::short()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace League\CLImate\Argument;
4
5
use League\CLImate\CLImate;
6
7
class Summary
8
{
9
    /**
10
     * @var \League\CLImate\CLImate $climate
11
     */
12
    protected $climate;
13
14
    /**
15
     * @var string $description
16
     */
17
    protected $description;
18
19
    /**
20
     * @var string $command
21
     */
22
    protected $command;
23
24
    /**
25
     * @var Filter $filter
26
     */
27
    protected $filter;
28
29
    /**
30
     * @param \League\CLImate\CLImate $climate
31
     *
32
     * @return \League\CLImate\Argument\Summary
33
     */
34 4
    public function setClimate(CLImate $climate)
35
    {
36 4
        $this->climate = $climate;
37
38 4
        return $this;
39
    }
40
41
    /**
42
     * @param string $description
43
     *
44
     * @return \League\CLImate\Argument\Summary
45
     */
46 4
    public function setDescription($description)
47
    {
48 4
        $this->description = $description;
49
50 4
        return $this;
51
    }
52
53
    /**
54
     * @param string $command
55
     *
56
     * @return \League\CLImate\Argument\Summary
57
     */
58 4
    public function setCommand($command)
59
    {
60 4
        $this->command = $command;
61
62 4
        return $this;
63
    }
64
65
    /**
66
     * @param Filter $filter
67
     * @param Argument[] $arguments
68
     *
69
     * @return \League\CLImate\Argument\Summary
70
     */
71 4
    public function setFilter($filter, $arguments)
72
    {
73 4
        $this->filter = $filter;
74 4
        $this->filter->setArguments($arguments);
75
76 4
        return $this;
77
    }
78
79
    /**
80
     * Output the full summary for the program
81
     */
82 4
    public function output()
83
    {
84
        // Print the description if it's defined.
85 4
        if ($this->description) {
86 4
            $this->climate->out($this->description)->br();
87 4
        }
88
89
        // Print the usage statement with the arguments without a prefix at the end.
90 4
        $this->climate->out("Usage: {$this->command} "
91 4
                            . $this->short($this->getOrderedArguments()));
92
93
        // Print argument details.
94 4
        foreach (['required', 'optional'] as $type) {
95 4
            $this->outputArguments($this->filter->{$type}(), $type);
96 4
        }
97 4
    }
98
99
    /**
100
     * Build a short summary of a list of arguments.
101
     *
102
     * @param Argument[] $arguments
103
     *
104
     * @return string
105
     */
106 8
    public function short($arguments)
107
    {
108 8
        return implode(' ', array_map([$this, 'argumentBracketed'], $arguments));
109
    }
110
111
    /**
112
     * Build an argument's summary for use in a usage statement.
113
     *
114
     * For example, "-u username, --user username", "--force", or
115
     * "-c count (default: 7)".
116
     *
117
     * @param Argument $argument
118
     *
119
     * @return string
120
     */
121 8
    public function argument(Argument $argument)
122
    {
123 8
        $summary     = $this->prefixedArguments($argument);
124 8
        $printedName = mb_strstr($summary, ' ' . $argument->name());
125
126
        // Print the argument name if it's not printed yet.
127 8
        if (!$printedName && !$argument->noValue()) {
128 4
            $summary .= $argument->name();
129 4
        }
130
131 8
        if ($defaults = $argument->defaultValue()) {
132 4
            if (count($defaults) == 1) {
133 4
                $summary .= " (default: {$defaults[0]})";
134
            } else {
135 8
                $summary .= ' (defaults: ' . implode(', ', $defaults) . ')';
136
            }
137
        }
138
139
        return $summary;
140
    }
141
142
    /**
143
     * Build argument summary surrounded by brackets
144
     *
145 8
     * @param Argument $argument
146
     *
147 8
     * @return string
148
     */
149
    protected function argumentBracketed(Argument $argument)
150
    {
151
        return '[' . $this->argument($argument) . ']';
152
    }
153
154
    /**
155 4
     * Get the arguments ordered by whether or not they have a prefix
156
     *
157 4
     * @return Argument[]
158
     */
159
    protected function getOrderedArguments()
160
    {
161
        return array_merge($this->filter->withPrefix(), $this->filter->withoutPrefix());
162
    }
163
164
    /**
165
     * Print out the argument list
166 4
     *
167
     * @param array $arguments
168 4
     * @param string $type
169
     */
170
    protected function outputArguments($arguments, $type)
171
    {
172 4
        if (count($arguments) == 0) {
173
            return;
174 4
        }
175 4
176
        $this->climate->br()->out(mb_convert_case($type, MB_CASE_TITLE) . ' Arguments:');
177 4
178 4
        foreach ($arguments as $argument) {
179 4
            $this->climate->tab()->out($this->argument($argument));
180 4
181 4
            if ($argument->description()) {
182
                $this->climate->tab(2)->out($argument->description());
183
            }
184
        }
185
    }
186
187
    /**
188
     * Builds the summary for any prefixed arguments
189
     *
190 8
     * @param Argument $argument
191
     *
192 8
     * @return string
193 8
     */
194
    protected function prefixedArguments(Argument $argument)
195 8
    {
196 8
        $prefixes = [$argument->prefix(), $argument->longPrefix()];
197 8
        $summary  = [];
198
199
        foreach ($prefixes as $key => $prefix) {
200 8
            if (!$prefix) {
201
                continue;
202 8
            }
203 8
204 8
            $sub = str_repeat('-', $key + 1) . $prefix;
205
206 8
            if (!$argument->noValue()) {
207 8
                $sub .= " {$argument->name()}";
208
            }
209 8
210
            $summary[] = $sub;
211
        }
212
213
        return implode(', ', $summary);
214
    }
215
}
216