| Conditions | 19 |
| Paths | 18 |
| Total Lines | 104 |
| Code Lines | 72 |
| 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 |
||
| 171 | public function processTasks($log_level = 0) |
||
| 172 | { |
||
| 173 | $helper = \XoopsModules\Wgevents\Helper::getInstance(); |
||
| 174 | |||
| 175 | // get limit_hour from primary account |
||
| 176 | $accountHandler = $helper->getHandler('Account'); |
||
| 177 | $limitHour = $accountHandler->getLimitHour(); |
||
| 178 | |||
| 179 | $resProcess = ''; |
||
| 180 | |||
| 181 | $crTaskPending = new \CriteriaCompo(); |
||
| 182 | $crTaskPending->add(new \Criteria('status', Constants::STATUS_PENDING)); |
||
| 183 | $tasksCountPending = $this->getCount($crTaskPending); |
||
| 184 | $crTaskPending->setSort('type ASC, id'); |
||
| 185 | $crTaskPending->setOrder('ASC'); |
||
| 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 status PROCESSING at start: ' . $tasksCountProcessing . '</span>'; |
||
| 203 | } |
||
| 204 | $resProcess .= '<br>Count status PENDING at start: ' . $tasksCountPending; |
||
| 205 | $resProcess .= '<br>Count status 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 | $resultMH = 0; |
||
| 213 | foreach (\array_keys($tasksAll) as $i) { |
||
| 214 | // check whether task is still pending |
||
| 215 | // ignore it if meanwhile another one started to process the task |
||
| 216 | if ($log_level > 1) { |
||
| 217 | $resProcess .= '<br>Task key: ' . $i; |
||
| 218 | } |
||
| 219 | if (554 === $resultMH) { |
||
| 220 | $resProcess .= '<br>Skipped Error 554: SMTP limit exceeded'; |
||
| 221 | } elseif ((Constants::STATUS_PENDING == (int)$tasksAll[$i]->getVar('status')) |
||
| 222 | && ($tasksCountDone < $limitHour || 0 == $limitHour)) { |
||
| 223 | $taskProcessObj = $this->get($i); |
||
| 224 | $taskProcessObj->setVar('status', Constants::STATUS_PROCESSING); |
||
| 225 | if ($this->insert($taskProcessObj)) { |
||
| 226 | $mailsHandler = new MailHandler(); |
||
| 227 | $mailParams = json_decode($tasksAll[$i]->getVar('params', 'n'), true); |
||
| 228 | $mailParams['recipients'] = $tasksAll[$i]->getVar('recipient'); |
||
| 229 | $mailParams['taskId'] = $i; |
||
| 230 | $mailsHandler->setParams($mailParams); |
||
| 231 | $mailsHandler->setType($tasksAll[$i]->getVar('type')); |
||
| 232 | // send mails |
||
| 233 | $resultMH = $mailsHandler->execute(); |
||
| 234 | unset($mailsHandler); |
||
| 235 | //update task list corresponding the result |
||
| 236 | if (0 === $resultMH) { |
||
| 237 | $taskProcessObj->setVar('status', Constants::STATUS_DONE); |
||
| 238 | $taskProcessObj->setVar('datedone', time()); |
||
| 239 | $counterDone++; |
||
| 240 | if ($log_level > 1) { |
||
| 241 | $resProcess .= ' - done'; |
||
| 242 | } |
||
| 243 | } else { |
||
| 244 | $taskProcessObj->setVar('status', Constants::STATUS_PENDING); |
||
| 245 | if ($log_level > 1) { |
||
| 246 | $resProcess .= ' - failed'; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | $this->insert($taskProcessObj); |
||
| 250 | } else { |
||
| 251 | $resProcess .= ' - error insert taskProcessObj'; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | unset($taskProcessObj); |
||
| 255 | $taskProcessObj = $this->get($i); |
||
| 256 | //check whether process is still pending, what should not be |
||
| 257 | if (Constants::STATUS_PROCESSING === (int)$taskProcessObj->getVar('status')) { |
||
| 258 | $taskProcessObj->setVar('status', Constants::STATUS_PENDING); |
||
| 259 | $this->insert($taskProcessObj); |
||
| 260 | } |
||
| 261 | // check once more number of done |
||
| 262 | $tasksCountDone = $this->getCount($crTaskDone); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | // check once more number of open tasks |
||
| 266 | $crTaskOpen = new \CriteriaCompo(); |
||
| 267 | $crTaskOpen->add(new \Criteria('status', Constants::STATUS_DONE, '<')); |
||
| 268 | $tasksCountOpen = $this->getCount($crTaskOpen); |
||
| 269 | |||
| 270 | if ($log_level > 0) { |
||
| 271 | $resProcess .= '<br>End processTasks'; |
||
| 272 | } |
||
| 273 | |||
| 274 | return ['pending' => $tasksCountPending, 'done' => $counterDone, 'resprocess' => $resProcess, 'still_open' => $tasksCountOpen]; |
||
| 275 | } |
||
| 277 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: