Completed
Pull Request — master (#6)
by Woody
02:19
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
     * Provide read-only access to attributes.
29
     *
30
     * @param string $key
31
     *
32
     * @return mixed
33
     */
34 2
    final public function __get($key)
35
    {
36 2
        return $this->{$key};
37
    }
38
39
    /**
40
     * Prevent modification.
41
     *
42
     * @throws ImmutableException
43
     *
44
     * @param string $key
45
     * @param mixed $value
46
     *
47
     * @return void
48
     */
49 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...
50
    {
51 1
        throw ImmutableException::cannotModify($this);
52
    }
53
54
    /**
55
     * Prevent modification.
56
     *
57
     * @throws ImmutableException
58
     *
59
     * @param string $key
60
     *
61
     * @return void
62
     */
63 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...
64
    {
65 1
        throw ImmutableException::cannotModify($this);
66
    }
67
68
    /**
69
     * Ensure that all required options are included in the given values.
70
     *
71
     * @param array $values
72
     *
73
     * @return void
74
     *
75
     * @throws CommandException
76
     *  If any required options have not been defined.
77
     */
78
    private function ensureRequired(array $values)
79
    {
80 5
        $defined = array_filter($values, static function ($value) {
81 4
            return $value !== null;
82 5
        });
83
84 5
        $missing = array_diff($this->required(), array_keys($defined));
0 ignored issues
show
Bug introduced by
The method required() does not exist on Equip\Command\OptionsTrait. Did you maybe mean ensureRequired()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
85
86 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...
87 1
            throw CommandException::missingOptions($missing);
88
        }
89 4
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 5
    public function valid()
95
    {
96 5
        return array_keys($this->toArray());
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102 5
    public function toArray()
103
    {
104 5
        return get_object_vars($this);
105
    }
106
}
107