Completed
Pull Request — master (#6)
by Woody
01:57
created

OptionsTrait::toArray()   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
trait OptionsTrait /* implements OptionsInterface */
6
{
7
    /**
8
     * Check that all required options are defined, then hydrate.
9
     *
10
     * @param array $values
11
     */
12 5
    public function __construct(array $values)
13
    {
14
        // Remove any values that are invalid for this set of options
15 5
        $values = array_intersect_key(
16 5
            $values,
17 5
            array_flip($this->valid())
18 5
        );
19
20 5
        $this->ensureRequired($values);
21
22 4
        foreach ($values as $key => $value) {
23 4
            $this->$key = $value;
24 4
        }
25 4
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30
    abstract public function required();
31
32
    /**
33
     * @inheritdoc
34
     */
35 5
    public function valid()
36
    {
37 5
        return array_keys($this->toArray());
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 5
    public function toArray()
44
    {
45 5
        return get_object_vars($this);
46
    }
47
48
    /**
49
     * Provide read-only access to attributes.
50
     *
51
     * @param string $key
52
     *
53
     * @return mixed
54
     */
55 2
    final public function __get($key)
56
    {
57 2
        return $this->{$key};
58
    }
59
60
    /**
61
     * Prevent modification.
62
     *
63
     * @throws ImmutableException
64
     *
65
     * @param string $key
66
     * @param mixed $value
67
     *
68
     * @return void
69
     */
70 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...
71
    {
72 1
        throw ImmutableException::cannotModify($this);
73
    }
74
75
    /**
76
     * Prevent modification.
77
     *
78
     * @throws ImmutableException
79
     *
80
     * @param string $key
81
     *
82
     * @return void
83
     */
84 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...
85
    {
86 1
        throw ImmutableException::cannotModify($this);
87
    }
88
89
    /**
90
     * Ensure that all required options are included in the given values.
91
     *
92
     * @param array $values
93
     *
94
     * @return void
95
     *
96
     * @throws CommandException
97
     *  If any required options have not been defined.
98
     */
99
    private function ensureRequired(array $values)
100
    {
101 5
        $defined = array_filter($values, static function ($value) {
102 4
            return $value !== null;
103 5
        });
104
105 5
        $missing = array_diff($this->required(), array_keys($defined));
106
107 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...
108 1
            throw CommandException::missingOptions($missing);
109
        }
110 4
    }
111
}
112