Summary   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 3

Complexity

Total Complexity 22

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Test Coverage

Coverage 98.41%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 0
loc 205
ccs 62
cts 63
cp 0.9841
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setClimate() 0 6 1
A setDescription() 0 6 1
A setCommand() 0 6 1
A setFilter() 0 7 1
A output() 0 16 3
A short() 0 4 1
A argument() 0 16 4
A argumentBracketed() 0 4 1
A getOrderedArguments() 0 4 1
A outputArguments() 0 16 4
A prefixedArguments() 0 21 4
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 ($argument->defaultValue()) {
132 4
            $summary .= " (default: {$argument->defaultValue()})";
133 4
        }
134
135 8
        return $summary;
136
    }
137
138
    /**
139
     * Build argument summary surrounded by brackets
140
     *
141
     * @param Argument $argument
142
     *
143
     * @return string
144
     */
145 8
    protected function argumentBracketed(Argument $argument)
146
    {
147 8
        return '[' . $this->argument($argument) . ']';
148
    }
149
150
    /**
151
     * Get the arguments ordered by whether or not they have a prefix
152
     *
153
     * @return Argument[]
154
     */
155 4
    protected function getOrderedArguments()
156
    {
157 4
        return array_merge($this->filter->withPrefix(), $this->filter->withoutPrefix());
158
    }
159
160
    /**
161
     * Print out the argument list
162
     *
163
     * @param array $arguments
164
     * @param string $type
165
     */
166 4
    protected function outputArguments($arguments, $type)
167
    {
168 4
        if (count($arguments) == 0) {
169
            return;
170
        }
171
172 4
        $this->climate->br()->out(mb_convert_case($type, MB_CASE_TITLE) . ' Arguments:');
173
174 4
        foreach ($arguments as $argument) {
175 4
            $this->climate->tab()->out($this->argument($argument));
176
177 4
            if ($argument->description()) {
178 4
                $this->climate->tab(2)->out($argument->description());
179 4
            }
180 4
        }
181 4
    }
182
183
    /**
184
     * Builds the summary for any prefixed arguments
185
     *
186
     * @param Argument $argument
187
     *
188
     * @return string
189
     */
190 8
    protected function prefixedArguments(Argument $argument)
191
    {
192 8
        $prefixes = [$argument->prefix(), $argument->longPrefix()];
193 8
        $summary  = [];
194
195 8
        foreach ($prefixes as $key => $prefix) {
196 8
            if (!$prefix) {
197 8
                continue;
198
            }
199
200 8
            $sub = str_repeat('-', $key + 1) . $prefix;
201
202 8
            if (!$argument->noValue()) {
203 8
                $sub .= " {$argument->name()}";
204 8
            }
205
206 8
            $summary[] = $sub;
207 8
        }
208
209 8
        return implode(', ', $summary);
210
    }
211
}
212