Complex classes like JobService 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 JobService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class JobService { |
||
33 | |||
34 | /** |
||
35 | * |
||
36 | * @var ILogger |
||
37 | */ |
||
38 | private $logger; |
||
39 | |||
40 | /** |
||
41 | * |
||
42 | * @var RedisService |
||
43 | */ |
||
44 | private $redisService; |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | * @var OcrJobMapper |
||
49 | */ |
||
50 | private $jobMapper; |
||
51 | |||
52 | /** |
||
53 | * |
||
54 | * @var View |
||
55 | */ |
||
56 | private $view; |
||
57 | |||
58 | /** |
||
59 | * |
||
60 | * @var String |
||
61 | */ |
||
62 | private $userId; |
||
63 | |||
64 | /** |
||
65 | * |
||
66 | * @var IL10N |
||
67 | */ |
||
68 | private $l10n; |
||
69 | |||
70 | /** |
||
71 | * |
||
72 | * @var FileService |
||
73 | */ |
||
74 | private $fileService; |
||
75 | |||
76 | /** |
||
77 | * |
||
78 | * @var ITempManager |
||
79 | */ |
||
80 | private $tempM; |
||
81 | |||
82 | /** |
||
83 | * |
||
84 | * @var AppConfigService |
||
85 | */ |
||
86 | private $appConfigService; |
||
87 | |||
88 | /** |
||
89 | * |
||
90 | * @var PHPUtil |
||
91 | */ |
||
92 | private $phpUtil; |
||
93 | |||
94 | /** |
||
95 | * |
||
96 | * @var FileUtil |
||
97 | */ |
||
98 | private $fileUtil; |
||
99 | |||
100 | /** |
||
101 | * JobService constructor. |
||
102 | * |
||
103 | * @param IL10N $l10n |
||
104 | * @param ILogger $logger |
||
105 | * @param string $userId |
||
106 | * @param View $view |
||
107 | * @param RedisService $queueService |
||
108 | * @param OcrJobMapper $mapper |
||
109 | * @param FileService $fileService |
||
110 | * @param AppConfigService $appConfigService |
||
111 | * @param PHPUtil $phpUtil |
||
112 | * @param FileUtil $fileUtil |
||
113 | */ |
||
114 | 24 | public function __construct(IL10N $l10n, ILogger $logger, $userId, View $view, ITempManager $tempManager, |
|
129 | |||
130 | /** |
||
131 | * Processes and prepares the files for OCR. |
||
132 | * Sends the stuff to the client in order to OCR async. |
||
133 | * |
||
134 | * @param string[] $languages |
||
135 | * @param array $files |
||
136 | * @param boolean $replace |
||
137 | * @return string |
||
138 | */ |
||
139 | 11 | public function process($languages, $files, $replace) { |
|
184 | |||
185 | /** |
||
186 | * Delete an ocr job for a given id and userId. |
||
187 | * |
||
188 | * @param |
||
189 | * $jobId |
||
190 | * @param string $userId |
||
191 | * @return OcrJob |
||
192 | */ |
||
193 | 3 | public function deleteJob($jobId, $userId) { |
|
220 | |||
221 | /** |
||
222 | * Gets all job objects for a specific user. |
||
223 | * |
||
224 | * @param string $userId |
||
225 | * @return OcrJob[] |
||
226 | */ |
||
227 | 1 | public function getAllJobsForUser($userId) { |
|
245 | |||
246 | /** |
||
247 | * The function checks if there are finished jobs to process finally. |
||
248 | * |
||
249 | * @throws NotFoundException |
||
250 | */ |
||
251 | 2 | public function checkForFinishedJobs() { |
|
267 | |||
268 | /** |
||
269 | * Finishes all Processed files by copying them to the right path and deleteing the temp files. |
||
270 | * Returns the number of processed files. |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | 2 | public function handleProcessed() { |
|
296 | |||
297 | /** |
||
298 | * Handles all failed orders of ocr processing queue and returns the job objects. |
||
299 | * |
||
300 | * @return array |
||
301 | */ |
||
302 | 1 | public function handleFailed() { |
|
322 | |||
323 | /** |
||
324 | * Gets the OCR result and puts it to Nextcloud. |
||
325 | * |
||
326 | * @param OcrJob $job |
||
327 | */ |
||
328 | 1 | private function pullResult($job) { |
|
337 | |||
338 | /** |
||
339 | * The function the worker will call in order to set the jobs status. |
||
340 | * The worker should call it automatically after each processing step. |
||
341 | * |
||
342 | * @param integer $jobId |
||
343 | * @param boolean $error |
||
344 | * @param string $log |
||
345 | */ |
||
346 | 1 | private function jobFinished($jobId, $error, $log) { |
|
362 | |||
363 | /** |
||
364 | * Gives a temp file name back depending on the type of the OCR. |
||
365 | * Later in the worker this file is used as an output. |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | 6 | private function getTempFile() { |
|
378 | |||
379 | /** |
||
380 | * Takes care of transforming an incoming finished job into a php readable object. |
||
381 | * |
||
382 | * @param string $job |
||
383 | * @throws NotFoundException |
||
384 | * @return mixed |
||
385 | */ |
||
386 | 1 | private function transformJob($job) { |
|
395 | |||
396 | /** |
||
397 | * Checks if the given languages are supported or not. |
||
398 | * |
||
399 | * @param string[] $languages |
||
400 | * @return boolean |
||
401 | */ |
||
402 | 10 | private function checkForAcceptedLanguages($languages) { |
|
410 | |||
411 | /** |
||
412 | * Checks if the process should be initiated without any language specified. |
||
413 | * |
||
414 | * @param string[] $languages |
||
415 | * @return boolean |
||
416 | */ |
||
417 | 11 | private function noLanguage($languages) { |
|
424 | |||
425 | /** |
||
426 | * Handle the possible thrown Exceptions from all methods of this class. |
||
427 | * |
||
428 | * @param Exception $e |
||
429 | * @throws Exception |
||
430 | * @throws NotFoundException |
||
431 | */ |
||
432 | 12 | private function handleException($e) { |
|
440 | } |
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.