Completed
Pull Request — master (#6)
by Woody
02:53
created

Options::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Equip\Command;
4
5
/**
6
 * A general purpose value object for command options.
7
 *
8
 * When constructed, all required options must be passed.
9
 *
10
 * @since 1.3.0
11
 */
12
abstract class Options
13
{
14
    /**
15
     * Check that all required options are defined, then hydrate.
16
     *
17
     * @param array $values
18
     */
19 5
    final public function __construct(array $values)
20
    {
21
        // Remove any values that are invalid for this set of options
22 5
        $values = array_intersect_key(
23 5
            $values,
24 5
            array_flip($this->valid())
25 5
        );
26
27 5
        $this->ensureRequired($values);
28
29 4
        foreach ($values as $key => $value) {
30 4
            $this->$key = $value;
31 4
        }
32 4
    }
33
34
    /**
35
     * Get a list of all required options.
36
     *
37
     *     return [
38
     *         'email',
39
     *         'password',
40
     *     ];
41
     *
42
     * @return array
43
     */
44
    abstract public function required();
45
46
    /**
47
     * Get a list of all valid options.
48
     *
49
     * @return array
50
     */
51 5
    public function valid()
52
    {
53 5
        return array_keys($this->toArray());
54
    }
55
56
    /**
57
     * Get all options as an array.
58
     *
59
     * @return array
60
     */
61 5
    public function toArray()
62
    {
63 5
        return get_object_vars($this);
64
    }
65
66
    /**
67
     * Provide read-only access to attributes.
68
     *
69
     * @param string $key
70
     *
71
     * @return mixed
72
     */
73 2
    final public function __get($key)
74
    {
75 2
        return $this->{$key};
76
    }
77
78
    /**
79
     * Prevent modification.
80
     *
81
     * @throws ImmutableException
82
     *
83
     * @param string $key
84
     * @param mixed $value
85
     *
86
     * @return void
87
     */
88 1
    final public function __set($key, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90 1
        throw ImmutableException::cannotModify($this);
91
    }
92
93
    /**
94
     * Prevent modification.
95
     *
96
     * @throws ImmutableException
97
     *
98
     * @param string $key
99
     *
100
     * @return void
101
     */
102 1
    final public function __unset($key)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104 1
        throw ImmutableException::cannotModify($this);
105
    }
106
107
    /**
108
     * Ensure that all required options are included in the given values.
109
     *
110
     * @param array $values
111
     *
112
     * @return void
113
     *
114
     * @throws CommandException
115
     *  If any required options have not been defined.
116
     */
117
    private function ensureRequired(array $values)
118
    {
119 5
        $defined = array_filter($values, static function ($value) {
120 4
            return $value !== null;
121 5
        });
122
123 5
        $missing = array_diff($this->required(), array_keys($defined));
124
125 5
        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...
126 1
            throw CommandException::missingOptions($missing);
127
        }
128 4
    }
129
}
130