Completed
Push — master ( 91d599...412a7f )
by Lincoln
21s
created

Args::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.0046

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
crap 1.0046
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2014 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Cli;
9
10
/**
11
 * This class represents the parsed and validated argument list.
12
 */
13
class Args implements \JsonSerializable, \ArrayAccess {
14
    protected $command;
15
    protected $opts;
16
    protected $args;
17
    protected $meta;
18
19
    /**
20
     * Initialize the {@link Args} instance.
21
     *
22
     * @param string $command The name of the command.
23
     * @param array $opts An array of command line options.
24
     * @param array $args A numeric array of command line args.
25
     */
26 10
    public function __construct($command = '', $opts = [], $args = []) {
27 10
        $this->command = $command;
28 10
        $this->opts = $opts;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
29 10
        $this->args = $args;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
30 10
        $this->meta = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
31
    }
32
33
    /**
34
     * Add an argument to the args array.
35
     *
36
     * @param string $value The argument to add.
37
     * @param string|null $index The index to add the arg at.
38
     * @return Args Returns $this for fluent calls.
39
     */
40 2
    public function addArg($value, $index = null) {
41 2
        if ($index !== null) {
42 2
            $this->args[$index] = $value;
43
        } else {
44
            $this->args[] = $value;
45 2
        }
46 2
        return $this;
47
    }
48
49
    /**
50
     * Get an argument at a given index.
51
     *
52
     * Arguments can be accessed by name or index.
53
     *
54
     * @param string|int $index
55
     * @param mixed $default The default value to return if the argument is not found.
56
     * @return mixed Returns the argument at {@link $index} or {@link $default}.
57
     */
58 2
    public function getArg($index, $default = null) {
59
        if (array_key_exists($index, $this->args)) {
60 2
            return $this->args[$index];
61
        } elseif (is_int($index)) {
62
            $values = array_values($this->args);
63
            if (array_key_exists($index, $values)) {
64 1
                return $values[$index];
65
            }
66
        }
67 1
        return $default;
68 1
    }
69
70
    /**
71
     * Set an argument in the args array.
72
     *
73
     * @param string|int $index The index to set at.
74
     * @param mixed $value The value of the arg.
75
     * @return Args $this Returns $this for fluent calls.
76
     */
77 1
    public function setArg($index, $value) {
78 1
        $this->args[$index] = $value;
79 1
        return $this;
80
    }
81
82
    /**
83
     * Get the args array.
84
     *
85
     * @return array Returns the args array.
86
     */
87 25
    public function getArgs() {
88 25
        return $this->args;
89
    }
90
91
    /**
92
     * Set the args array.
93
     *
94
     * @param array $args The new args array.
95
     * @return Args Returns $this for fluent calls.
96
     */
97 28
    public function setArgs(array $args) {
98 28
        $this->args = $args;
99 28
        return $this;
100 28
    }
101
102
    /**
103
     * Get the name of the command associated with the args.
104
     *
105
     * @return string Returns the name of the command.
106
     */
107 1
    public function getCommand() {
108 1
        return $this->command;
109
    }
110
111
    /**
112
     * Set the name of the command associated with the args.
113
     *
114
     * @param string $command The new command.
115
     * @return Args Returns $this for fluent calls.
116
     */
117 4
    public function setCommand($command) {
118 4
        $this->command = $command;
119 4
        return $this;
120
    }
121
122
    /**
123
     * Get a meta value.
124
     *
125
     * @param string $name The name of the meta value.
126
     * @param mixed $default The default value to return if {@link $name} is not found.
127
     * @return mixed Returns the meta value or {@link $default} if it doesn't exist.
128
     */
129 7
    public function getMeta($name, $default = null) {
130
        return Cli::val($name, $this->meta, $default);
131 7
    }
132
133
    /**
134
     * Set a meta value.
135
     *
136
     * @param string $name The name of the meta value.
137
     * @param mixed $value The new meta value.
138
     * @return Args Returns $this for fluent setting.
139
     */
140 2
    public function setMeta($name, $value) {
141 2
        $this->meta[$name] = $value;
142 2
        return $this;
143
    }
144
145
    /**
146
     * Gets the entire options array.
147
     *
148
     * @return array Returns the current options array.
149
     */
150 15
    public function getOpts() {
151 15
        return $this->opts;
152
    }
153
154
    /**
155
     * Sets the entire options array.
156
     *
157
     * @param array $value Pass an array to set a new options array.
158
     * @return Args Returns $this for fluent calls.
159
     */
160 1
    public function setOpts(array $value) {
161 1
        $this->opts = $value;
162 1
        return $this;
163 1
    }
164
165
    /**
166
     * Get the value of a passed option.
167
     *
168
     * @param string $option The name of the option to get.
169
     * @param mixed $default The default value if the option does not exist.
170
     * @return mixed Returns the option or {@link $default} if it does not exist.
171
     */
172 3
    public function getOpt($option, $default = null) {
173
        return Cli::val($option, $this->opts, $default);
174 3
    }
175
176
    /**
177
     * Set an option.
178
     *
179
     * @param string $option The name of the option.
180
     * @param mixed $value The value of the option.
181
     * @return Args Returns $this for fluent calls.
182
     */
183 6
    public function setOpt($option, $value) {
184 6
        if ($value === null) {
185 1
            unset($this->opts[$option]);
186
        } else {
187 6
            $this->opts[$option] = $value;
188 1
        }
189 6
        return $this;
190
    }
191
192
    /**
193
     * Return the json serializable data for the args.
194
     *
195
     * @return array Returns an array of data that can be used to serialize the args to json.
196
     */
197 1
    public function jsonSerialize() {
198
        return [
199 1
            'command' => $this->command,
200 1
            'opts' => $this->opts,
201 1
            'args' => $this->args,
202 1
            'meta' => $this->meta
203 1
        ];
204
    }
205
206
    /**
207
     * Whether a offset exists.
208
     *
209
     * @param mixed $offset An offset to check for.
210
     * @return boolean true on success or false on failure.
211
     * The return value will be casted to boolean if non-boolean was returned.
212
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
213
     */
214 1
    public function offsetExists($offset) {
215 1
        return isset($this->opts[$offset]);
216
    }
217
218
    /**
219
     * Offset to retrieve.
220
     *
221
     * @param mixed $offset The offset to retrieve.
222
     * @return mixed Can return all value types.
223
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
224
     */
225
    public function offsetGet($offset) {
226
        return $this->getOpt($offset, null);
227
    }
228
229
    /**
230
     * Offset to set.
231
     *
232
     * @param mixed $offset The offset to assign the value to.
233
     * @param mixed $value The value to set.
234
     * @return void
235
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
236
     */
237 1
    public function offsetSet($offset, $value) {
238
        $this->setOpt($offset, $value);
239 1
    }
240
241
    /**
242
     * Offset to unset.
243
     *
244
     * @param mixed $offset The offset to unset.
245
     * @return void
246
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
247
     */
248 1
    public function offsetUnset($offset) {
249 1
        unset($this->opts[$offset]);
250 1
    }
251
}
252