| Conditions | 9 |
| Paths | 35 |
| Total Lines | 90 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 25 | public function expireJobsAction() |
||
| 26 | { |
||
| 27 | |||
| 28 | $services = $this->getServiceLocator(); |
||
| 29 | $repositories = $services->get('repositories'); |
||
| 30 | /* @var \Jobs\Repository\Job $jobsRepo */ |
||
| 31 | $jobsRepo = $repositories->get('Jobs/Job'); |
||
| 32 | $filter = \Zend\Json\Json::decode($this->params('filter', '{}')); |
||
| 33 | $query = array(); |
||
| 34 | $limit = 10; |
||
| 35 | foreach ($filter as $key => $value) { |
||
| 36 | switch ($key) { |
||
| 37 | case "limit": |
||
| 38 | $limit = $value; |
||
| 39 | break; |
||
| 40 | |||
| 41 | case "days": |
||
| 42 | $date = new \DateTime(); |
||
| 43 | $date->modify('-' . (int) $value. ' day'); |
||
| 44 | $q = array('$lt' => $date); |
||
| 45 | if (isset($query['datePublishStart.date'])) { |
||
| 46 | $query['datePublishStart.date']= array_merge( |
||
| 47 | $query['datePublishStart.date'], |
||
| 48 | $q |
||
| 49 | ); |
||
| 50 | } else { |
||
| 51 | $query['datePublishStart.date'] = $q; |
||
| 52 | } |
||
| 53 | break; |
||
| 54 | |||
| 55 | default: |
||
| 56 | $query[$key] = $value; |
||
| 57 | break; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | $query['status.name'] = 'active'; |
||
| 61 | |||
| 62 | $jobs = $jobsRepo->findBy($query,null,$limit); |
||
| 63 | $count = count($jobs); |
||
| 64 | |||
| 65 | if (0 === $count) { |
||
| 66 | return 'No jobs found.'; |
||
| 67 | } |
||
| 68 | |||
| 69 | foreach ($repositories->getEventManager()->getListeners('preUpdate') as $listener) { |
||
| 70 | $repositories->getEventManager()->removeEventListener('preUpdate', $listener); |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | echo "$count jobs found, which have to expire ...\n"; |
||
| 75 | |||
| 76 | $progress = new ProgressBar( |
||
| 77 | new ConsoleAdapter( |
||
| 78 | array( |
||
| 79 | 'elements' => array( |
||
| 80 | ConsoleAdapter::ELEMENT_TEXT, |
||
| 81 | ConsoleAdapter::ELEMENT_BAR, |
||
| 82 | ConsoleAdapter::ELEMENT_PERCENT, |
||
| 83 | ConsoleAdapter::ELEMENT_ETA |
||
| 84 | ), |
||
| 85 | 'textWidth' => 20, |
||
| 86 | 'barLeftChar' => '-', |
||
| 87 | 'barRightChar' => ' ', |
||
| 88 | 'barIndicatorChar' => '>', |
||
| 89 | ) |
||
| 90 | ), |
||
| 91 | 0, |
||
| 92 | count($jobs) |
||
| 93 | ); |
||
| 94 | |||
| 95 | $i = 0; |
||
| 96 | |||
| 97 | /* @var \Jobs\Entity\Job $job */ |
||
| 98 | foreach ($jobs as $job) { |
||
| 99 | $progress->update($i++, 'Job ' . $i . ' / ' . $count); |
||
| 100 | |||
| 101 | $job->changeStatus('expired'); |
||
| 102 | |||
| 103 | if (0 == $i % 500) { |
||
| 104 | $progress->update($i, 'Write to database...'); |
||
| 105 | $repositories->flush(); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | $progress->update($i, 'Write to database...'); |
||
| 109 | $repositories->flush(); |
||
| 110 | $progress->update($i, 'Done'); |
||
| 111 | $progress->finish(); |
||
| 112 | |||
| 113 | return PHP_EOL; |
||
| 114 | } |
||
| 115 | |||
| 163 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.