| Conditions | 10 |
| Paths | 18 |
| Total Lines | 45 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 74 | protected function pruneOldJobs(array $matches, OutputInterface $output) |
||
| 75 | { |
||
| 76 | $durationOrTimestamp = intval($matches[1]); |
||
| 77 | $modifier = isset($matches[2]) ? $matches[2] : null; |
||
| 78 | |||
| 79 | if (!$durationOrTimestamp) { |
||
| 80 | $output->writeln('<error>No duration or timestamp passed in.</error>'); |
||
| 81 | |||
| 82 | return 1; |
||
| 83 | } |
||
| 84 | $olderThan = new \DateTime(); |
||
| 85 | if (!$modifier) { |
||
| 86 | $olderThan->setTimestamp($durationOrTimestamp); |
||
| 87 | } else { |
||
| 88 | switch ($modifier) { |
||
| 89 | case 'd': |
||
| 90 | $interval = new \DateInterval("P${durationOrTimestamp}D"); |
||
| 91 | break; |
||
| 92 | case 'm': |
||
| 93 | $interval = new \DateInterval("P${durationOrTimestamp}M"); |
||
| 94 | break; |
||
| 95 | case 'y': |
||
| 96 | $interval = new \DateInterval("P${durationOrTimestamp}Y"); |
||
| 97 | break; |
||
| 98 | case 'h': |
||
| 99 | $interval = new \DateInterval("PT${durationOrTimestamp}H"); |
||
| 100 | break; |
||
| 101 | case 'i': |
||
| 102 | $seconds = $durationOrTimestamp * 60; |
||
| 103 | $interval = new \DateInterval("PT${seconds}S"); |
||
| 104 | break; |
||
| 105 | case 's': |
||
| 106 | $interval = new \DateInterval("PT${durationOrTimestamp}S"); |
||
| 107 | break; |
||
| 108 | default: |
||
| 109 | throw new \Exception("Unknown duration modifier: $modifier"); |
||
| 110 | } |
||
| 111 | $olderThan->sub($interval); |
||
| 112 | } |
||
| 113 | $container = $this->getContainer(); |
||
| 114 | $count = $container->get('dtc_queue.job_manager')->pruneArchivedJobs($olderThan); |
||
| 115 | $output->writeln("$count Archived Job(s) pruned"); |
||
| 116 | |||
| 117 | return 0; |
||
| 118 | } |
||
| 119 | } |
||
| 120 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.