| Conditions | 11 |
| Paths | 9 |
| Total Lines | 39 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 62 | public function registerTimers(array $jobs) |
||
| 63 | { |
||
| 64 | $timerIds = []; |
||
| 65 | foreach ($jobs as $jobClass) { |
||
| 66 | if (is_array($jobClass) && isset($jobClass[0])) { |
||
| 67 | $job = new $jobClass[0](isset($jobClass[1]) ? $jobClass[1] : []); |
||
| 68 | } else { |
||
| 69 | $job = new $jobClass(); |
||
| 70 | } |
||
| 71 | if (!($job instanceof CronJob)) { |
||
| 72 | throw new \InvalidArgumentException(sprintf( |
||
| 73 | '%s must extend the abstract class %s', |
||
| 74 | get_class($job), |
||
| 75 | CronJob::class |
||
| 76 | ) |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | if (empty($job->interval())) { |
||
| 80 | throw new \InvalidArgumentException(sprintf('The interval of %s cannot be empty', get_class($job))); |
||
| 81 | } |
||
| 82 | $runJob = function () use ($job) { |
||
| 83 | $runCallback = function () use ($job) { |
||
| 84 | $this->callWithCatchException(function () use ($job) { |
||
| 85 | if (($job instanceof CheckGlobalTimerAliveCronJob) || $job::isEnable()) { |
||
| 86 | $job->run(); |
||
| 87 | } |
||
| 88 | }); |
||
| 89 | }; |
||
| 90 | class_exists('Swoole\Coroutine') ? \Swoole\Coroutine::create($runCallback) : $runCallback(); |
||
| 91 | }; |
||
| 92 | |||
| 93 | $timerId = Timer::tick($job->interval(), $runJob); |
||
| 94 | $timerIds[] = $timerId; |
||
| 95 | $job->setTimerId($timerId); |
||
| 96 | if ($job->isImmediate()) { |
||
| 97 | Timer::after(1, $runJob); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | return $timerIds; |
||
| 101 | } |
||
| 102 | } |