Issues (48)

src/DtoProperty.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Cerbero\Dto;
4
5
use Cerbero\Dto\Exceptions\UnexpectedValueException;
6
7
/**
8
 * The DTO property.
9
 *
10
 */
11
class DtoProperty
12
{
13
    /**
14
     * The property name.
15
     *
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * The property raw value.
22
     *
23
     * @var mixed
24
     */
25
    protected $rawValue;
26
27
    /**
28
     * The property types.
29
     *
30
     * @var DtoPropertyTypes
31
     */
32
    protected $types;
33
34
    /**
35
     * The DTO flags.
36
     *
37
     * @var int
38
     */
39
    protected $flags;
40
41
    /**
42
     * The property value processor.
43
     *
44
     * @var DtoPropertyValueProcessor
45
     */
46
    protected $valueProcessor;
47
48
    /**
49
     * The processed value.
50
     *
51
     * @var mixed
52
     */
53
    protected $processedValue;
54
55
    /**
56
     * Whether the value has been processed.
57
     *
58
     * @var bool
59
     */
60
    protected $valueIsProcessed = false;
61
62
    /**
63
     * Instantiate the class.
64
     *
65
     * @param string $name
66
     * @param mixed $rawValue
67
     * @param DtoPropertyTypes $types
68
     * @param int $flags
69
     */
70 91
    protected function __construct(string $name, $rawValue, DtoPropertyTypes $types, int $flags)
71
    {
72 91
        $this->name = $name;
73 91
        $this->rawValue = $rawValue;
74 91
        $this->types = $types;
75 91
        $this->flags = $flags;
76 91
        $this->valueProcessor = new DtoPropertyValueProcessor($this);
77 91
    }
78
79
    /**
80
     * Retrieve a DTO property instance after validating it
81
     *
82
     * @param string $name
83
     * @param mixed $rawValue
84
     * @param DtoPropertyTypes $types
85
     * @param int $flags
86
     * @return self
87
     * @throws UnexpectedValueException
88
     */
89 91
    public static function create(string $name, $rawValue, DtoPropertyTypes $types, int $flags): self
90
    {
91 91
        $instance = new static($name, $rawValue, $types, $flags);
92
93 91
        return $instance->validate();
94
    }
95
96
    /**
97
     * Validate the current property value depending on types and flags
98
     *
99
     * @return self
100
     * @throws UnexpectedValueException
101
     */
102 91
    public function validate(): self
103
    {
104 91
        $canBeDto = $this->rawValue instanceof Dto || is_array($this->rawValue);
105
106
        switch (true) {
107 91
            case $this->types->expectedDto && $canBeDto:
108 84
            case $this->rawValue === null && $this->types->includeNull:
109 77
            case $this->types->expectCollection && is_iterable($this->rawValue):
110 74
            case $this->types->match($this->value()):
0 ignored issues
show
Are you sure the usage of $this->value() targeting Cerbero\Dto\DtoProperty::value() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
111 85
                return $this;
112
        }
113
114 6
        throw new UnexpectedValueException($this);
115
    }
116
117
    /**
118
     * Retrieve the processed value
119
     *
120
     * @return void
121
     */
122 82
    public function value()
123
    {
124 82
        if (!$this->valueIsProcessed) {
125 82
            $this->processedValue = $this->valueProcessor->process();
126 82
            $this->valueIsProcessed = true;
127
        }
128
129 82
        return $this->processedValue;
130
    }
131
132
    /**
133
     * Retrieve the property name
134
     *
135
     * @return string
136
     */
137 12
    public function getName(): string
138
    {
139 12
        return $this->name;
140
    }
141
142
    /**
143
     * Retrieve the property raw value
144
     *
145
     * @return mixed
146
     */
147 85
    public function getRawValue()
148
    {
149 85
        return $this->rawValue;
150
    }
151
152
    /**
153
     * Retrieve the property types
154
     *
155
     * @return DtoPropertyTypes
156
     */
157 85
    public function getTypes(): DtoPropertyTypes
158
    {
159 85
        return $this->types;
160
    }
161
162
    /**
163
     * Retrieve the DTO flags
164
     *
165
     * @return int
166
     */
167 74
    public function getFlags(): int
168
    {
169 74
        return $this->flags;
170
    }
171
172
    /**
173
     * Set a new value to this property and validate it
174
     *
175
     * @param mixed $rawValue
176
     * @param int $flags
177
     * @return self
178
     */
179 8
    public function setValue($rawValue, int $flags): self
180
    {
181 8
        $this->rawValue = $rawValue;
182 8
        $this->flags = $flags;
183 8
        $this->valueIsProcessed = false;
184
185 8
        return $this->validate();
186
    }
187
188
    /**
189
     * Determine how to clone the DTO property
190
     *
191
     * @return void
192
     */
193 9
    public function __clone()
194
    {
195 9
        $this->types = clone $this->types;
196 9
        $this->valueProcessor = new DtoPropertyValueProcessor($this);
197 9
    }
198
}
199