Conditions | 8 |
Paths | 17 |
Total Lines | 56 |
Code Lines | 36 |
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 |
||
58 | protected function runJob(CronJob $job, OutputInterface $output) |
||
59 | { |
||
60 | $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
61 | $output->write('Running '.$job->getCommand().': '); |
||
62 | |||
63 | try { |
||
64 | $commandToRun = $this->getApplication()->get($job->getCommand()); |
||
65 | } catch (\Symfony\Component\Console\Exception\InvalidArgumentException $ex) { |
||
66 | $output->writeln(' skipped (command no longer exists)'); |
||
67 | $this->recordJobResult($entityManager, $job, 0, 'Command no longer exists', CronJobResult::SKIPPED); |
||
|
|||
68 | |||
69 | // No need to reschedule non-existant commands |
||
70 | return; |
||
71 | } |
||
72 | |||
73 | $emptyInput = new ArgvInput(); |
||
74 | $jobOutput = new MemoryWriter(); |
||
75 | |||
76 | $jobStart = microtime(true); |
||
77 | try { |
||
78 | $returnCode = $commandToRun->execute($emptyInput, $jobOutput); |
||
79 | } catch (\Exception $ex) { |
||
80 | $returnCode = CronJobResult::FAILED; |
||
81 | $jobOutput->writeln(''); |
||
82 | $jobOutput->writeln('Job execution failed with exception '.get_class($ex).':'); |
||
83 | $jobOutput->writeln($ex->__toString()); |
||
84 | } |
||
85 | $jobEnd = microtime(true); |
||
86 | |||
87 | // Clamp the result to accepted values |
||
88 | if ($returnCode < CronJobResult::RESULT_MIN || $returnCode > CronJobResult::RESULT_MAX) { |
||
89 | $returnCode = CronJobResult::FAILED; |
||
90 | } |
||
91 | |||
92 | // Output the result |
||
93 | $statusStr = 'unknown'; |
||
94 | if ($returnCode == CronJobResult::SKIPPED) { |
||
95 | $statusStr = 'skipped'; |
||
96 | } elseif ($returnCode == CronJobResult::SUCCEEDED) { |
||
97 | $statusStr = 'succeeded'; |
||
98 | } elseif ($returnCode == CronJobResult::FAILED) { |
||
99 | $statusStr = 'failed'; |
||
100 | } |
||
101 | |||
102 | $durationStr = sprintf('%0.2f', $jobEnd - $jobStart); |
||
103 | $output->writeln("$statusStr in $durationStr seconds"); |
||
104 | |||
105 | // Record the result |
||
106 | $this->recordJobResult($job, $jobEnd - $jobStart, $jobOutput->getOutput(), $returnCode); |
||
107 | |||
108 | // And update the job with it's next scheduled time |
||
109 | $interval = new \DateInterval($job->getInterval()); |
||
110 | $newTime = clone $job->getNextRun(); |
||
111 | $newTime->add($interval); |
||
112 | $job->setNextRun($newTime); |
||
113 | } |
||
114 | |||
131 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.