Conditions | 13 |
Paths | 59 |
Total Lines | 65 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
40 | protected function execute(InputInterface $input, OutputInterface $output) |
||
41 | { |
||
42 | $this->detectMagento($output, true); |
||
43 | if ($this->initMagento()) { |
||
44 | |||
45 | $jobCode = $input->getArgument('job'); |
||
46 | if (!$jobCode) { |
||
47 | $this->writeSection($output, 'Cronjob'); |
||
48 | $jobCode = $this->askJobCode($output, $this->getJobs()); |
||
49 | } |
||
50 | |||
51 | $jobsRoot = \Mage::getConfig()->getNode('crontab/jobs'); |
||
52 | $defaultJobsRoot = \Mage::getConfig()->getNode('default/crontab/jobs'); |
||
53 | |||
54 | $jobConfig = $jobsRoot->{$jobCode}; |
||
55 | if (!$jobConfig || !$jobConfig->run) { |
||
56 | $jobConfig = $defaultJobsRoot->{$jobCode}; |
||
57 | if (!$jobConfig || !$jobConfig->run) { |
||
58 | throw new RuntimeException('No job config found!'); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | $runConfig = $jobConfig->run; |
||
63 | |||
64 | if ($runConfig->model) { |
||
65 | |||
66 | if (!preg_match(self::REGEX_RUN_MODEL, (string) $runConfig->model, $run)) { |
||
67 | throw new RuntimeException('Invalid model/method definition, expecting "model/class::method".'); |
||
68 | } |
||
69 | if (!($model = \Mage::getModel($run[1])) || !method_exists($model, $run[2])) { |
||
70 | throw new RuntimeException(sprintf('Invalid callback: %s::%s does not exist', $run[1], $run[2])); |
||
71 | } |
||
72 | $callback = array($model, $run[2]); |
||
73 | |||
74 | $output->write('<info>Run </info><comment>' . get_class($model) . '::' . $run[2] . '</comment> '); |
||
75 | |||
76 | try { |
||
77 | $schedule = \Mage::getModel('cron/schedule'); |
||
78 | $schedule |
||
79 | ->setJobCode($jobCode) |
||
80 | ->setStatus(\Mage_Cron_Model_Schedule::STATUS_RUNNING) |
||
81 | ->setExecutedAt(strftime('%Y-%m-%d %H:%M:%S', time())) |
||
82 | ->save(); |
||
83 | |||
84 | call_user_func_array($callback, array($schedule)); |
||
85 | |||
86 | $schedule |
||
87 | ->setStatus(\Mage_Cron_Model_Schedule::STATUS_SUCCESS) |
||
88 | ->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', time())) |
||
89 | ->save(); |
||
90 | } catch (Exception $e) { |
||
91 | $schedule |
||
92 | ->setStatus(\Mage_Cron_Model_Schedule::STATUS_ERROR) |
||
93 | ->setMessages($e->getMessage()) |
||
94 | ->setFinishedAt(strftime('%Y-%m-%d %H:%M:%S', time())) |
||
95 | ->save(); |
||
96 | } |
||
97 | |||
98 | $output->writeln('<info>done</info>'); |
||
99 | } |
||
100 | if (empty($callback)) { |
||
101 | \Mage::throwException(Mage::helper('cron')->__('No callbacks found')); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | |||
140 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.