Completed
Pull Request — master (#14)
by Woody
13:14
created

ImmutableValueObjectTrait::__get()   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
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Equip\Data\Traits;
4
5
use Equip\Data\ArraySerializableInterface;
6
use RuntimeException;
7
8
trait ImmutableValueObjectTrait
9
{
10
    use ProtectedValueObjectTrait;
11
12
    /**
13
     * Hydrate the object with new values
14
     *
15 16
     * @param array $data
16
     */
17 16
    public function __construct(array $data = [])
18 14
    {
19 12
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data 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...
20 14
            $this->apply($data);
21
        }
22
    }
23
24
    /**
25
     * Create a copy of the object with new values
26
     *
27
     * @param array $data
28
     *
29 2
     * @return static
30
     */
31 2
    public function withData(array $data)
32 2
    {
33 2
        $copy = clone $this;
34
        $copy->apply($data);
35
        return $copy;
36
    }
37
38
    /**
39
     * Type definitions for object properties
40
     *
41
     *     return [
42
     *         'id'         => 'int',
43
     *         'email'      => 'string',
44
     *         'is_deleted' => 'bool',
45
     *      ];
46
     *
47
     * Overload this method to enable type coercion!
48
     *
49 3
     * @return array
50
     */
51 3
    private function types()
52
    {
53
        return [];
54
    }
55
56
    /**
57
     * Expected types for object properties
58
     *
59
     *     return [
60
     *         'user' => User::class,
61
     *     ];
62
     *
63 7
     * @return array
64
     */
65 7
    private function expects()
66
    {
67
        return [];
68
    }
69
70
    /**
71
     * Validates the current object
72
     *
73
     * @return void
74
     *
75 8
     * @throws \DomainException If the object is not valid
76
     */
77 8
    private function validate()
78
    {
79
    }
80
81
    /**
82
     * Update the current object with new values
83
     *
84
     * NOTE: Be careful not to violate immutability when using this method!
85
     *
86
     * @uses types()
87
     * @uses expects()
88
     * @uses validate()
89
     *
90
     * @param array $data
91
     *
92
     * @return void
93
     *
94 14
     * @throws \DomainException If a data value is not of the expected type
95
     */
96
    private function apply(array $data)
97 14
    {
98
        // Discard any values that do not exist in this object
99
        $data = array_intersect_key($data, get_object_vars($this));
100
101 14
        // Type coercion and class expectations are not run on null values
102 14
        $values = array_filter($data, static function ($value) {
103
            return null !== $value;
104 14
        });
105 14
106
        $types   = array_intersect_key($this->types(), $values);
107 14
        $expects = array_intersect_key($this->expects(), $values);
108 12
109 14
        foreach ($types as $key => $type) {
110
            settype($data[$key], $type);
111 14
        }
112 3
113 1
        foreach ($expects as $key => $class) {
114 1
            if (false === $data[$key] instanceof $class) {
115 1
                throw new \DomainException(sprintf(
116
                    'Expected value of `%s` to be an object of `%s` type',
117 1
                    $key,
118
                    $class
119 13
                ));
120
            }
121 13
        }
122 13
123 13
        foreach ($data as $key => $value) {
124
            $this->$key = $value;
125 13
        }
126 12
127
        $this->validate();
0 ignored issues
show
Unused Code introduced by
The call to the method Equip\Data\Traits\Immuta...ObjectTrait::validate() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
128
    }
129
130
    /**
131
     * Check if the current object has a property
132
     *
133
     * @param string $key
134
     *
135 2
     * @return boolean
136
     */
137 2
    public function has($key)
138
    {
139
        return property_exists($this, $key);
140
    }
141
142
    /**
143
     * Get the current object values as an array
144
     *
145
     * @return array
146
     */
147 8
    public function toArray()
148 8
    {
149 1
        return array_map(static function ($value) {
150 1
            if ($value instanceof ArraySerializableInterface) {
151 8
                $value = $value->toArray();
152 8
            }
153
            return $value;
154
        }, get_object_vars($this));
155
    }
156
}
157