Complex classes like Error 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Error, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Error implements IMutableError |
||
32 | { |
||
33 | /** |
||
34 | * @var int|string|NULL |
||
35 | */ |
||
36 | private $id = NULL; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $links = []; |
||
42 | |||
43 | /** |
||
44 | * @var string|NULL |
||
45 | */ |
||
46 | private $status = NULL; |
||
47 | |||
48 | /** |
||
49 | * @var string|NULL |
||
50 | */ |
||
51 | private $code = NULL; |
||
52 | |||
53 | /** |
||
54 | * @var string|NULL |
||
55 | */ |
||
56 | private $title = NULL; |
||
57 | |||
58 | /** |
||
59 | * @var string|NULL |
||
60 | */ |
||
61 | private $detail = NULL; |
||
62 | |||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | private $source = []; |
||
67 | |||
68 | /** |
||
69 | * @var array|NULL |
||
70 | */ |
||
71 | private $meta = NULL; |
||
72 | |||
73 | /** |
||
74 | * @param $error |
||
75 | * |
||
76 | * @return Error |
||
77 | */ |
||
78 | public static function cast($error) : Error |
||
98 | |||
99 | /** |
||
100 | * Create an error object from an array |
||
101 | * |
||
102 | * @param array $input |
||
103 | * |
||
104 | * @return Error |
||
105 | */ |
||
106 | public static function create(array $input = []) : Error |
||
113 | |||
114 | /** |
||
115 | * Create an error collection from an array of error arrays. |
||
116 | * |
||
117 | * @param array $input |
||
118 | * |
||
119 | * @return ErrorCollection |
||
120 | */ |
||
121 | public static function createMany(array $input) : ErrorCollection |
||
131 | |||
132 | /** |
||
133 | * @param int|string|NULL $id |
||
134 | * @param array|NULL $links |
||
135 | * @param int|string|NULL $status |
||
136 | * @param int|string|NULL $code |
||
137 | * @param string|NULL $title |
||
138 | * @param string|NULL $detail |
||
139 | * @param array|NULL $source |
||
140 | * @param array|NULL $meta |
||
141 | */ |
||
142 | public function __construct( |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function setId($id) : void |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function getId() |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function hasId() : bool |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function setLinks(array $links = NULL) : void |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | public function addLinks(array $links = NULL) : void |
||
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | public function addLink(string $key, LinkInterface $link) : void |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | public function setAboutLink(LinkInterface $link) : void |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | public function getLinks() : ?array |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function setStatus($status) : void |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function getStatus() : ?string |
||
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | */ |
||
267 | public function hasStatus() : bool |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | */ |
||
275 | public function setCode($code) : void |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function getCode() : ?string |
||
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | public function hasCode() : bool |
||
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | public function setTitle(?string $title) : void |
||
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | */ |
||
315 | public function getTitle() : ?string |
||
319 | |||
320 | /** |
||
321 | * {@inheritdoc} |
||
322 | */ |
||
323 | public function hasTitle() : bool |
||
327 | |||
328 | /** |
||
329 | * {@inheritdoc} |
||
330 | */ |
||
331 | public function setDetail(?string $detail) : void |
||
339 | |||
340 | /** |
||
341 | * {@inheritdoc} |
||
342 | */ |
||
343 | public function getDetail() : ?string |
||
347 | |||
348 | /** |
||
349 | * {@inheritdoc} |
||
350 | */ |
||
351 | public function hasDetail() : bool |
||
355 | |||
356 | /** |
||
357 | * {@inheritdoc} |
||
358 | */ |
||
359 | public function setSource(array $source = NULL) : void |
||
363 | |||
364 | /** |
||
365 | * {@inheritdoc} |
||
366 | */ |
||
367 | public function getSource() : ?array |
||
371 | |||
372 | /** |
||
373 | * {@inheritdoc} |
||
374 | */ |
||
375 | public function setSourcePointer(?string $pointer) : void |
||
388 | |||
389 | /** |
||
390 | * {@inheritdoc} |
||
391 | */ |
||
392 | public function getSourcePointer() : ?string |
||
396 | |||
397 | /** |
||
398 | * {@inheritdoc} |
||
399 | */ |
||
400 | public function hasSourcePointer() : bool |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function setSourceParameter(?string $parameter) : void |
||
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | public function getSourceParameter() : ?string |
||
429 | |||
430 | /** |
||
431 | * {@inheritdoc} |
||
432 | */ |
||
433 | public function hasSourceParameter() : bool |
||
437 | |||
438 | /** |
||
439 | * {@inheritdoc} |
||
440 | */ |
||
441 | public function setMeta(?array $meta = []) : void |
||
445 | |||
446 | /** |
||
447 | * {@inheritdoc} |
||
448 | */ |
||
449 | public function addMeta(array $meta) : void |
||
453 | |||
454 | /** |
||
455 | * {@inheritdoc} |
||
456 | */ |
||
457 | public function getMeta() : ?array |
||
461 | |||
462 | /** |
||
463 | * {@inheritdoc} |
||
464 | */ |
||
465 | public function merge(ErrorInterface $error) : void |
||
507 | |||
508 | /** |
||
509 | * {@inheritdoc} |
||
510 | */ |
||
511 | public function exchangeArray(array $input) : void |
||
568 | } |
||
569 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.