Total Complexity | 47 |
Total Lines | 272 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DataTransferObject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataTransferObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class DataTransferObject implements DtoContract |
||
22 | { |
||
23 | /** @var array */ |
||
24 | protected $onlyKeys = []; |
||
25 | |||
26 | /** @var Property[] | array */ |
||
27 | protected $properties = []; |
||
28 | |||
29 | /** @var bool */ |
||
30 | protected $immutable = false; |
||
31 | |||
32 | public function __construct(array $parameters) |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Boot the dto and process all parameters. |
||
39 | * @param array $parameters |
||
40 | * @throws \ReflectionException |
||
41 | */ |
||
42 | protected function boot(array $parameters): void |
||
68 | } |
||
69 | |||
70 | protected function determineImmutability() |
||
71 | { |
||
72 | if ($this instanceof immutable) { |
||
73 | $this->setImmutable(); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | protected function setImmutable() |
||
82 | } |
||
83 | } |
||
84 | |||
85 | protected function chainPropertiesImmutable(PropertyContract $property) |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Get all public properties from the current object through reflection. |
||
101 | * @return Property[] |
||
102 | * @throws \ReflectionException |
||
103 | */ |
||
104 | protected function getPublicProperties(): array |
||
105 | { |
||
106 | $class = new ReflectionClass(static::class); |
||
107 | |||
108 | $properties = []; |
||
109 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
||
110 | $properties[$reflectionProperty->getName()] = new Property($reflectionProperty); |
||
111 | } |
||
112 | |||
113 | return $properties; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Check if property passes the basic conditions. |
||
118 | * @param PropertyContract $property |
||
119 | * @param array $parameters |
||
120 | */ |
||
121 | protected function validateProperty(PropertyContract $property, array $parameters): void |
||
122 | { |
||
123 | if (! array_key_exists($property->getName(), $parameters) |
||
124 | && is_null($property->getDefault()) |
||
125 | && ! $property->nullable() |
||
126 | ) { |
||
127 | throw new UninitialisedPropertyDtoException($property); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Set the value if it's present in the array. |
||
133 | * @param PropertyContract $property |
||
134 | * @param array $parameters |
||
135 | */ |
||
136 | protected function setPropertyValue(PropertyContract $property, array $parameters): void |
||
137 | { |
||
138 | if (array_key_exists($property->getName(), $parameters)) { |
||
139 | $property->set($parameters[$property->getName()]); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Set the value if it's present in the array. |
||
145 | * @param PropertyContract $property |
||
146 | */ |
||
147 | protected function setPropertyDefaultValue(PropertyContract $property): void |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Allows to mutate the property before it gets processed. |
||
154 | * @param PropertyContract $property |
||
155 | * @return PropertyContract |
||
156 | */ |
||
157 | protected function mutateProperty(PropertyContract $property): PropertyContract |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Check if there are additional parameters left. |
||
164 | * Throw error if there are. |
||
165 | * Additional properties are not allowed in a dto. |
||
166 | * @param array $parameters |
||
167 | * @throws UnknownPropertiesDtoException |
||
168 | */ |
||
169 | protected function processRemainingProperties(array $parameters) |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Immutable behavior |
||
178 | * Throw error if a user tries to set a property. |
||
179 | * @param $name |
||
180 | * @param $value |
||
181 | * @throws ImmutableDtoException|ImmutablePropertyDtoException|PropertyNotFoundDtoException |
||
182 | */ |
||
183 | public function __set($name, $value) |
||
184 | { |
||
185 | if ($this->immutable) { |
||
186 | throw new ImmutableDtoException($name); |
||
187 | } |
||
188 | if (! isset($this->properties[$name])) { |
||
189 | throw new PropertyNotFoundDtoException($name, get_class($this)); |
||
190 | } |
||
191 | |||
192 | if ($this->properties[$name]->immutable()) { |
||
193 | throw new ImmutablePropertyDtoException($name); |
||
194 | } |
||
195 | $this->$name = $value; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Proxy through to the properties array. |
||
200 | * @param $name |
||
201 | * @return mixed |
||
202 | */ |
||
203 | public function __get($name) |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @return static |
||
210 | */ |
||
211 | public function immutable(): DtoContract |
||
212 | { |
||
213 | if (! $this->isImmutable()) { |
||
214 | $this->setImmutable(); |
||
215 | } |
||
216 | |||
217 | return $this; |
||
218 | } |
||
219 | |||
220 | public function isImmutable(): bool |
||
221 | { |
||
222 | return $this->immutable; |
||
223 | } |
||
224 | |||
225 | public function all(): array |
||
226 | { |
||
227 | $data = []; |
||
228 | |||
229 | foreach ($this->properties as $property) { |
||
230 | $data[$property->getName()] = $property->getValue(); |
||
231 | } |
||
232 | |||
233 | return $data; |
||
234 | } |
||
235 | |||
236 | public function only(string ...$keys): DtoContract |
||
237 | { |
||
238 | $this->onlyKeys = array_merge($this->onlyKeys, $keys); |
||
239 | |||
240 | return $this; |
||
241 | } |
||
242 | |||
243 | public function except(string ...$keys): DtoContract |
||
244 | { |
||
245 | foreach ($keys as $key) { |
||
246 | $property = $this->properties[$key] ?? null; |
||
247 | if (isset($property)) { |
||
248 | $property->setVisible(false); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | return $this; |
||
253 | } |
||
254 | |||
255 | public function toArray(): array |
||
271 | } |
||
272 | |||
273 | protected function parseArray(array $array): array |
||
274 | { |
||
275 | foreach ($array as $key => $value) { |
||
293 | } |
||
294 | } |
||
295 |