Conditions | 17 |
Paths | 18 |
Total Lines | 92 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
173 | public function processTasks($log_level = 0, $isCron = false) |
||
174 | { |
||
175 | $helper = \XoopsModules\Wgevents\Helper::getInstance(); |
||
176 | |||
177 | // get limit_hour from primary account |
||
178 | $accountHandler = $helper->getHandler('Account'); |
||
179 | $limitHour = $accountHandler->getLimitHour(); |
||
180 | |||
181 | $resProcess = ''; |
||
182 | |||
183 | $crTaskPending = new \CriteriaCompo(); |
||
184 | $crTaskPending->add(new \Criteria('status', Constants::STATUS_PENDING)); |
||
185 | $tasksCountPending = $this->getCount($crTaskPending); |
||
186 | |||
187 | // if all works properly there shouldn't be a task type 'processing' left |
||
188 | $crTaskProcessing = new \CriteriaCompo(); |
||
189 | $crTaskProcessing->add(new \Criteria('status', Constants::STATUS_PROCESSING)); |
||
190 | $tasksCountProcessing = $this->getCount($crTaskProcessing); |
||
191 | |||
192 | $crTaskDone = new \CriteriaCompo(); |
||
193 | $crTaskDone->add(new \Criteria('status', Constants::STATUS_DONE)); |
||
194 | $crTaskDone->add(new \Criteria('datedone', time() - 3600, '>')); |
||
195 | $tasksCountDone = $this->getCount($crTaskDone); |
||
196 | |||
197 | $counterDone = 0; |
||
198 | if ($log_level > 0) { |
||
199 | $resProcess .= '<br>Start processTasks'; |
||
200 | $resProcess .= '<br>time - 3600: ' . \formatTimestamp(time() - 3600, 'm'); |
||
201 | if ($tasksCountProcessing > 0) { |
||
202 | $resProcess .= '<br><span style="color:#ff0000;font-weight:700">Count processing at start: ' . $tasksCountProcessing . '</span>'; |
||
203 | } |
||
204 | $resProcess .= '<br>Count pending at start: ' . $tasksCountPending; |
||
205 | $resProcess .= '<br>Count done at start: ' . $tasksCountDone; |
||
206 | } |
||
207 | if (($tasksCountPending > 0) && ($tasksCountDone < $limitHour || 0 == $limitHour)) { |
||
208 | if ($limitHour > 0) { |
||
209 | $crTaskPending->setLimit($limitHour); |
||
210 | } |
||
211 | $tasksAll = $this->getAll($crTaskPending); |
||
212 | foreach (\array_keys($tasksAll) as $i) { |
||
213 | // check whether task is still pending |
||
214 | // ignore it if meanwhile another one started to process the task |
||
215 | if ($log_level > 1) { |
||
216 | $resProcess .= '<br>Task key: ' . $i; |
||
217 | } |
||
218 | if ((Constants::STATUS_PENDING == (int)$tasksAll[$i]->getVar('status')) |
||
219 | && ($tasksCountDone < $limitHour || 0 == $limitHour)) { |
||
220 | $taskProcessObj = $this->get($i); |
||
221 | $taskProcessObj->setVar('status', Constants::STATUS_PROCESSING); |
||
222 | if ($this->insert($taskProcessObj)) { |
||
223 | $mailsHandler = new MailHandler(); |
||
224 | $mailParams = json_decode($tasksAll[$i]->getVar('params', 'n'), true); |
||
225 | $mailParams['recipients'] = $tasksAll[$i]->getVar('recipient'); |
||
226 | $mailParams['taskId'] = $i; |
||
227 | $mailsHandler->setParams($mailParams); |
||
228 | $mailsHandler->setType($tasksAll[$i]->getVar('type')); |
||
229 | // send mails |
||
230 | $result = $mailsHandler->execute(); |
||
231 | unset($mailsHandler); |
||
232 | //update task list corresponding the result |
||
233 | if ($result) { |
||
234 | $taskProcessObj->setVar('status', Constants::STATUS_DONE); |
||
235 | $taskProcessObj->setVar('datedone', time()); |
||
236 | $counterDone++; |
||
237 | if ($log_level > 1) { |
||
238 | $resProcess .= ' - done'; |
||
239 | } |
||
240 | } else { |
||
241 | $taskProcessObj->setVar('status', Constants::STATUS_PENDING); |
||
242 | if ($log_level > 1) { |
||
243 | $resProcess .= ' - failed'; |
||
244 | } |
||
245 | } |
||
246 | $this->insert($taskProcessObj); |
||
247 | } else { |
||
248 | $resProcess .= ' - skipped'; |
||
249 | } |
||
250 | } |
||
251 | // check once more number of done |
||
252 | $tasksCountDone = $this->getCount($crTaskDone); |
||
253 | } |
||
254 | } |
||
255 | // check once more number of open tasks |
||
256 | $crTaskOpen = new \CriteriaCompo(); |
||
257 | $crTaskOpen->add(new \Criteria('status', Constants::STATUS_DONE, '<')); |
||
258 | $tasksCountOpen = $this->getCount($crTaskOpen); |
||
259 | |||
260 | if ($log_level > 0) { |
||
261 | $resProcess .= '<br>End processTasks'; |
||
262 | } |
||
263 | |||
264 | return ['pending' => $tasksCountPending, 'done' => $counterDone, 'resprocess' => $resProcess, 'still_open' => $tasksCountOpen]; |
||
265 | } |
||
267 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: