Complex classes like IdentifierAndResource 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 IdentifierAndResource, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
38 | class IdentifierAndResource implements ResourceInterface |
||
39 | { |
||
40 | /** @var string */ |
||
41 | public const MSG_NO_SCHEMA_FOUND = 'No Schema found for resource `%s` at path `%s`.'; |
||
42 | |||
43 | /** @var string */ |
||
44 | public const MSG_INVALID_OPERATION = 'Invalid operation.'; |
||
45 | |||
46 | /** |
||
47 | * @var PositionInterface |
||
48 | */ |
||
49 | private $position; |
||
50 | |||
51 | /** |
||
52 | * @var FactoryInterface |
||
53 | */ |
||
54 | private $factory; |
||
55 | |||
56 | /** |
||
57 | * @var SchemaContainerInterface |
||
58 | */ |
||
59 | private $schemaContainer; |
||
60 | |||
61 | /** |
||
62 | * @var SchemaInterface |
||
63 | */ |
||
64 | private $schema; |
||
65 | |||
66 | /** |
||
67 | * @var mixed |
||
68 | */ |
||
69 | private $data; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | private $index; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | private $type; |
||
80 | |||
81 | /** |
||
82 | * @var null|array |
||
83 | */ |
||
84 | private $links = null; |
||
85 | |||
86 | /** |
||
87 | * @var null|array |
||
88 | */ |
||
89 | private $relationshipsCache = null; |
||
90 | |||
91 | /** |
||
92 | * @param PositionInterface $position |
||
93 | * @param FactoryInterface $factory |
||
94 | * @param SchemaContainerInterface $container |
||
95 | * @param mixed $data |
||
96 | */ |
||
97 | 59 | public function __construct( |
|
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | 59 | public function getPosition(): PositionInterface |
|
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | 59 | public function getId(): ?string |
|
127 | |||
128 | /** |
||
129 | * @inheritdoc |
||
130 | */ |
||
131 | 59 | public function getType(): string |
|
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | 31 | public function hasIdentifierMeta(): bool |
|
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | */ |
||
147 | 2 | public function getIdentifierMeta() |
|
148 | { |
||
149 | 2 | return $this->getSchema()->getIdentifierMeta($this->getData()); |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | 56 | public function getAttributes(): iterable |
|
159 | |||
160 | /** |
||
161 | * @inheritdoc |
||
162 | */ |
||
163 | 59 | public function getRelationships(): iterable |
|
208 | |||
209 | /** |
||
210 | * @inheritdoc |
||
211 | */ |
||
212 | 56 | public function hasLinks(): bool |
|
218 | |||
219 | /** |
||
220 | * @inheritdoc |
||
221 | */ |
||
222 | 55 | public function getLinks(): iterable |
|
228 | |||
229 | /** |
||
230 | * @inheritdoc |
||
231 | */ |
||
232 | 56 | public function hasResourceMeta(): bool |
|
236 | |||
237 | /** |
||
238 | * @inheritdoc |
||
239 | */ |
||
240 | 1 | public function getResourceMeta() |
|
241 | { |
||
242 | 1 | return $this->getSchema()->getResourceMeta($this->getData()); |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * @inheritdoc |
||
247 | */ |
||
248 | 59 | protected function setPosition(PositionInterface $position): self |
|
256 | |||
257 | /** |
||
258 | * @return FactoryInterface |
||
259 | */ |
||
260 | 44 | protected function getFactory(): FactoryInterface |
|
264 | |||
265 | /** |
||
266 | * @param FactoryInterface $factory |
||
267 | * |
||
268 | * @return self |
||
269 | */ |
||
270 | 59 | protected function setFactory(FactoryInterface $factory): self |
|
276 | |||
277 | /** |
||
278 | * @return SchemaContainerInterface |
||
279 | */ |
||
280 | 34 | protected function getSchemaContainer(): SchemaContainerInterface |
|
284 | |||
285 | /** |
||
286 | * @param SchemaContainerInterface $container |
||
287 | * |
||
288 | * @return self |
||
289 | */ |
||
290 | 59 | protected function setSchemaContainer(SchemaContainerInterface $container): self |
|
296 | |||
297 | /** |
||
298 | * @return SchemaInterface |
||
299 | */ |
||
300 | 59 | protected function getSchema(): SchemaInterface |
|
304 | |||
305 | /** |
||
306 | * @param SchemaInterface $schema |
||
307 | * |
||
308 | * @return self |
||
309 | */ |
||
310 | 59 | protected function setSchema(SchemaInterface $schema): self |
|
316 | |||
317 | /** |
||
318 | * @return mixed |
||
319 | */ |
||
320 | 59 | protected function getData() |
|
324 | |||
325 | /** |
||
326 | * @param mixed $data |
||
327 | * |
||
328 | * @return self |
||
329 | */ |
||
330 | 59 | protected function setData($data): self |
|
338 | |||
339 | /** |
||
340 | * Read and parse links from schema. |
||
341 | */ |
||
342 | 56 | private function cacheLinks(): void |
|
353 | |||
354 | /** |
||
355 | * @param PositionInterface $position |
||
356 | * @param mixed $data |
||
357 | * |
||
358 | * @return RelationshipDataInterface |
||
359 | */ |
||
360 | 34 | private function parseData( |
|
390 | |||
391 | /** |
||
392 | * @param string $relationshipName |
||
393 | * @param iterable $schemaLinks |
||
394 | * @param bool $addSelfLink |
||
395 | * @param bool $addRelatedLink |
||
396 | * |
||
397 | * @return iterable |
||
398 | */ |
||
399 | 21 | private function parseLinks( |
|
400 | string $relationshipName, |
||
401 | iterable $schemaLinks, |
||
402 | bool $addSelfLink, |
||
403 | bool $addRelatedLink |
||
404 | ): iterable { |
||
405 | 21 | $gotSelf = false; |
|
406 | 21 | $gotRelated = false; |
|
407 | |||
408 | 21 | foreach ($schemaLinks as $name => $link) { |
|
409 | 11 | assert($link instanceof LinkInterface); |
|
410 | 11 | if ($name === LinkInterface::SELF) { |
|
411 | 7 | assert($gotSelf === false); |
|
412 | 7 | $gotSelf = true; |
|
413 | 7 | $addSelfLink = false; |
|
414 | 5 | } elseif ($name === LinkInterface::RELATED) { |
|
415 | 1 | assert($gotRelated === false); |
|
416 | 1 | $gotRelated = true; |
|
417 | 1 | $addRelatedLink = false; |
|
418 | } |
||
419 | |||
420 | 11 | yield $name => $link; |
|
421 | } |
||
422 | |||
423 | 21 | if ($addSelfLink === true) { |
|
424 | 15 | $link = $this->getSchema()->getRelationshipSelfLink($this->getData(), $relationshipName); |
|
425 | 15 | yield LinkInterface::SELF => $link; |
|
426 | 15 | $gotSelf = true; |
|
427 | } |
||
428 | 21 | if ($addRelatedLink === true) { |
|
429 | 12 | $link = $this->getSchema()->getRelationshipRelatedLink($this->getData(), $relationshipName); |
|
430 | 12 | yield LinkInterface::RELATED => $link; |
|
431 | 12 | $gotRelated = true; |
|
432 | } |
||
433 | |||
434 | // spec: check links has at least one of the following: self or related |
||
435 | 21 | assert($gotSelf || $gotRelated); |
|
436 | 21 | } |
|
437 | |||
438 | /** |
||
439 | * @param string $name |
||
440 | * @param array $description |
||
441 | * |
||
442 | * @return bool |
||
443 | */ |
||
444 | 44 | private function assertRelationshipNameAndDescription(string $name, array $description): bool |
|
457 | |||
458 | /** |
||
459 | * @param string $name |
||
460 | * @param array $description |
||
461 | * @param int $nextLevel |
||
462 | * @param string $nextPathPrefix |
||
463 | * |
||
464 | * @return array [has data, parsed data] |
||
465 | */ |
||
466 | 44 | private function parseRelationshipData( |
|
497 | |||
498 | /** |
||
499 | * @param string $name |
||
500 | * @param array $description |
||
501 | * |
||
502 | * @return array |
||
503 | */ |
||
504 | 44 | private function parseRelationshipLinks(string $name, array $description): array |
|
539 | } |
||
540 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.