Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
12 | class JobManager extends DoctrineJobManager |
||
13 | { |
||
14 | use CommonTrait; |
||
15 | const REDUCE_FUNCTION = 'function(k, vals) { |
||
16 | var result = {}; |
||
17 | for (var index in vals) { |
||
18 | var val = vals[index]; |
||
19 | for (var i in val) { |
||
20 | if (result.hasOwnProperty(i)) { |
||
21 | result[i] += val[i]; |
||
22 | } |
||
23 | else { |
||
24 | result[i] = val[i]; |
||
25 | } |
||
26 | } |
||
27 | } |
||
28 | return result; |
||
29 | }'; |
||
30 | |||
31 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
32 | { |
||
33 | /** @var DocumentManager $objectManager */ |
||
34 | 3 | $objectManager = $this->getObjectManager(); |
|
35 | 3 | $qb = $objectManager->createQueryBuilder($objectName); |
|
36 | $qb |
||
37 | 3 | ->find() |
|
38 | 3 | ->field('status')->equals($status); |
|
39 | |||
40 | 3 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
41 | 3 | $query = $qb->getQuery(); |
|
42 | |||
43 | 3 | return $query->count(); |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param string|null $workerName |
||
48 | * @param string|null $method |
||
49 | */ |
||
50 | 1 | public function pruneExceptionJobs($workerName = null, $method = null) |
|
51 | { |
||
52 | /** @var DocumentManager $objectManager */ |
||
53 | 1 | $objectManager = $this->getObjectManager(); |
|
54 | 1 | $qb = $objectManager->createQueryBuilder($this->getJobArchiveClass()); |
|
55 | 1 | $qb = $qb->remove(); |
|
56 | 1 | $qb->field('status')->equals(BaseJob::STATUS_EXCEPTION); |
|
57 | 1 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
58 | |||
59 | 1 | $query = $qb->getQuery(); |
|
60 | 1 | $result = $query->execute(); |
|
61 | 1 | if (isset($result['n'])) { |
|
62 | 1 | return $result['n']; |
|
63 | } |
||
64 | |||
65 | return 0; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param Builder $builder |
||
70 | * @param string|null $workerName |
||
71 | * @param string|null $method |
||
72 | */ |
||
73 | 18 | protected function addWorkerNameCriterion(Builder $builder, $workerName = null, $method = null) |
|
74 | { |
||
75 | 18 | if (null !== $workerName) { |
|
76 | 6 | $builder->field('workerName')->equals($workerName); |
|
77 | 6 | } |
|
78 | |||
79 | 18 | if (null !== $method) { |
|
80 | 5 | $builder->field('method')->equals($method); |
|
81 | 5 | } |
|
82 | 18 | } |
|
83 | |||
84 | /** |
||
85 | * @param null $workerName |
||
86 | * @param null $method |
||
87 | * |
||
88 | * @return int |
||
89 | */ |
||
90 | 2 | protected function updateExpired($workerName = null, $method = null) |
|
108 | |||
109 | /** |
||
110 | * Removes archived jobs older than $olderThan. |
||
111 | * |
||
112 | * @param \DateTime $olderThan |
||
113 | * |
||
114 | * @return int |
||
115 | */ |
||
116 | 1 | public function pruneArchivedJobs(\DateTime $olderThan) |
|
120 | |||
121 | 2 | View Code Duplication | public function getWaitingJobCount($workerName = null, $method = null) |
136 | |||
137 | /** |
||
138 | * Get Status Jobs. |
||
139 | * |
||
140 | * @param string $documentName |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | 3 | protected function getStatusByDocument($documentName) |
|
172 | |||
173 | 3 | public function getStatus() |
|
195 | |||
196 | /** |
||
197 | * Get the next job to run (can be filtered by workername and method name). |
||
198 | * |
||
199 | * @param string $workerName |
||
200 | * @param string $methodName |
||
201 | * @param bool $prioritize |
||
202 | * @param string|null $runId |
||
203 | * |
||
204 | * @return \Dtc\QueueBundle\Model\Job |
||
205 | */ |
||
206 | 12 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
226 | |||
227 | /** |
||
228 | * @param string|null $workerName |
||
229 | * @param string|null $methodName |
||
230 | * @param bool $prioritize |
||
231 | * |
||
232 | * @return Builder |
||
233 | */ |
||
234 | 13 | public function getJobQueryBuilder($workerName = null, $methodName = null, $prioritize = true) |
|
255 | |||
256 | 1 | protected function updateNearestBatch(\Dtc\QueueBundle\Model\Job $job) |
|
298 | |||
299 | /** |
||
300 | * @param mixed $builder |
||
301 | */ |
||
302 | 14 | protected function addStandardPredicates($builder) |
|
312 | |||
313 | 1 | public function getWorkersAndMethods() |
|
348 | |||
349 | /** |
||
350 | * @param string $workerName |
||
351 | * @param string $methodName |
||
352 | */ |
||
353 | 2 | public function countLiveJobs($workerName = null, $methodName = null) |
|
365 | |||
366 | /** |
||
367 | * @param string $workerName |
||
368 | * @param string $methodName |
||
369 | * @param callable|null $progressCallback |
||
370 | */ |
||
371 | 1 | public function archiveAllJobs($workerName = null, $methodName = null, callable $progressCallback = null) |
|
398 | } |
||
399 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.