Completed
Pull Request — master (#6)
by Woody
03:19
created

OptionsTrait::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
use Equip;
6
use Equip\Data\Traits\ProtectedValueObjectTrait;
7
8
trait OptionsTrait /* implements OptionsInterface */
9
{
10
    use ProtectedValueObjectTrait;
11
12
    /**
13
     * Check that all required options are defined, then hydrate.
14
     *
15
     * @param array $values
16
     */
17 2
    public function __construct(array $values)
18
    {
19 2
        $values = Equip\grab($values, $this->valid());
20
21 2
        $this->ensureRequired($values);
22
23 1
        foreach ($values as $key => $value) {
24 1
            $this->$key = $value;
25 1
        }
26 1
    }
27
28
    /**
29
     * Ensure that all required options are included in the given values.
30
     *
31
     * @param array $values
32
     *
33
     * @return void
34
     *
35
     * @throws CommandException
36
     *  If any required options have not been defined.
37
     */
38
    private function ensureRequired(array $values)
39
    {
40 2
        $defined = array_filter($values, static function ($value) {
41 1
            return $value !== null;
42 2
        });
43
44 2
        $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...
45
46 2
        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...
47 1
            throw CommandException::missingOptions($missing);
48
        }
49 1
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 2
    public function valid()
55
    {
56 2
        return array_keys($this->toArray());
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 2
    public function toArray()
63
    {
64 2
        return get_object_vars($this);
65
    }
66
}
67