Complex classes like Job 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 Job, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Job extends AbstractJob |
||
26 | { |
||
27 | /** |
||
28 | * Document actions. |
||
29 | */ |
||
30 | const ACTION_CREATE = 0; |
||
31 | const ACTION_UPDATE = 1; |
||
32 | const ACTION_DELETE_COLLECTION = 2; |
||
33 | const ACTION_DELETE_FILE = 3; |
||
34 | const ACTION_ADD_SHARE = 4; |
||
35 | const ACTION_DELETE_SHARE = 5; |
||
36 | |||
37 | /** |
||
38 | * Filesystem. |
||
39 | * |
||
40 | * @var Filesystem |
||
41 | */ |
||
42 | protected $fs; |
||
43 | |||
44 | /** |
||
45 | * Elasticsearch. |
||
46 | * |
||
47 | * @var Elasticsearch |
||
48 | */ |
||
49 | protected $es; |
||
50 | |||
51 | /** |
||
52 | * Logger. |
||
53 | * |
||
54 | * @var LoggerInterface |
||
55 | */ |
||
56 | protected $logger; |
||
57 | |||
58 | /** |
||
59 | * Node attribute decorator. |
||
60 | * |
||
61 | * @var NodeAttributeDecorator |
||
62 | */ |
||
63 | protected $decorator; |
||
64 | |||
65 | /** |
||
66 | * File size limit. |
||
67 | * |
||
68 | * @var int |
||
69 | */ |
||
70 | protected $size_limit = 52428800; |
||
71 | |||
72 | /** |
||
73 | * Constructor. |
||
74 | * |
||
75 | * @param Elasticsarch $es |
||
76 | * @param Storage $storage |
||
|
|||
77 | * @param Server $server |
||
78 | * @param NodeAttributeDecorator $decorator |
||
79 | * @param LoggerInterface $logger |
||
80 | * @param iterable $config |
||
81 | */ |
||
82 | public function __construct(Elasticsearch $es, Server $server, NodeAttributeDecorator $decorator, LoggerInterface $logger, Iterable $config = null) |
||
91 | |||
92 | /** |
||
93 | * Set options. |
||
94 | * |
||
95 | * @param iterable $config |
||
96 | * |
||
97 | * @return Job |
||
98 | */ |
||
99 | public function setOptions(?Iterable $config = null): self |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function start(): bool |
||
159 | |||
160 | /** |
||
161 | * Create document. |
||
162 | * |
||
163 | * @param NodeInterface $node |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function createDocument(NodeInterface $node): bool |
||
184 | |||
185 | /** |
||
186 | * Update document. |
||
187 | * |
||
188 | * @param NodeInterface $node |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function updateDocument(NodeInterface $node, ?string $hash): bool |
||
212 | |||
213 | /** |
||
214 | * Delete collection document. |
||
215 | * |
||
216 | * @param ObjectId $node |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function deleteCollectionDocument(ObjectId $node): bool |
||
236 | |||
237 | /** |
||
238 | * Create document. |
||
239 | * |
||
240 | * @param ObjectId $node |
||
241 | * @param string $hash |
||
242 | * |
||
243 | * @return bool |
||
244 | */ |
||
245 | public function deleteFileDocument(ObjectId $node, ?string $hash): bool |
||
263 | |||
264 | /** |
||
265 | * Add share. |
||
266 | * |
||
267 | * @param Collection $collection |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | protected function addShare(Collection $collection): bool |
||
284 | |||
285 | /** |
||
286 | * Delete share. |
||
287 | * |
||
288 | * @param Collection $collection |
||
289 | * |
||
290 | * @return bool |
||
291 | */ |
||
292 | protected function deleteShare(Collection $collection): bool |
||
309 | |||
310 | /** |
||
311 | * Delete blob reference. |
||
312 | * |
||
313 | * @param string $id |
||
314 | * @param string $hash |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | protected function deleteBlobReference(string $id, string $hash): bool |
||
349 | |||
350 | /** |
||
351 | * Set memory limit. |
||
352 | * |
||
353 | * @return Job |
||
354 | */ |
||
355 | protected function setMemoryLimit(): self |
||
365 | |||
366 | /** |
||
367 | * Get params. |
||
368 | * |
||
369 | * @param NodeInterface $node |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | protected function getParams(NodeInterface $node): array |
||
381 | |||
382 | /** |
||
383 | * Delete blob. |
||
384 | * |
||
385 | * @param string $id |
||
386 | * |
||
387 | * @return bool |
||
388 | */ |
||
389 | protected function deleteBlob(string $id): bool |
||
401 | |||
402 | /** |
||
403 | * Get stored file. |
||
404 | * |
||
405 | * @param string $hash |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | protected function getFileByHash(?string $hash): ?array |
||
438 | |||
439 | /** |
||
440 | * Add or update blob. |
||
441 | * |
||
442 | * @param File $node |
||
443 | * |
||
444 | * @return bool |
||
445 | */ |
||
446 | protected function storeBlob(File $file): bool |
||
478 | |||
479 | /** |
||
480 | * Prepare references update. |
||
481 | * |
||
482 | * @param array $references |
||
483 | * @param array $new_references |
||
484 | * @param array $new_share_references |
||
485 | * |
||
486 | * @return array |
||
487 | */ |
||
488 | protected function prepareUpdate(File $file, array $references): array |
||
512 | |||
513 | /** |
||
514 | * Add new blob. |
||
515 | * |
||
516 | * @param File $file |
||
517 | * |
||
518 | * @return bool |
||
519 | */ |
||
520 | protected function createNewBlob(File $file): bool |
||
553 | |||
554 | /** |
||
555 | * Update blob. |
||
556 | * |
||
557 | * @param File $file |
||
558 | * @param array $meta |
||
559 | * |
||
560 | * @return bool |
||
561 | */ |
||
562 | protected function updateBlob(string $id, array $meta): bool |
||
579 | } |
||
580 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.