AbstractCommand::withOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Equip\Command;
4
5
/**
6
 * @deprecated since 1.3.0 in favor of Command
7
 */
8
abstract class AbstractCommand implements CommandInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    private $options = [];
14
15
    /**
16
     * Get a list of options that must be defined
17
     *
18
     * @return array
19
     */
20
    abstract public function requiredOptions();
21
22
    /**
23
     * @inheritDoc
24
     *
25
     * @return array
26
     */
27
    public function options()
28
    {
29
        $required = $this->requiredOptions();
30
31
        if ($required) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $required of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
32
            $missing = array_diff($required, array_keys($this->options));
33
            if ($missing) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $missing of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
34
                throw CommandException::missingOptions($missing);
35
            }
36
        }
37
38
        $this->options += $this->defaultOptions();
39
40
        return $this->options;
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function withOptions(array $options)
47
    {
48
        $copy = clone $this;
49
        $copy->options = $options;
50
51
        return $copy;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function addOptions(array $options)
58
    {
59
        $copy = clone $this;
60
        $copy->options = array_replace($copy->options, $options);
61
62
        return $copy;
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function hasOption($name)
69
    {
70
        return isset($this->options[$name]);
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76
    public function defaultOptions()
77
    {
78
        return [];
79
    }
80
}
81