Conditions | 16 |
Paths | 12 |
Total Lines | 74 |
Code Lines | 53 |
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 |
||
160 | public function processTasks($log_level = 0) |
||
161 | { |
||
162 | $helper = \XoopsModules\Wgevents\Helper::getInstance(); |
||
163 | |||
164 | // get limit_hour from primary account |
||
165 | $accountHandler = $helper->getHandler('Account'); |
||
166 | $limitHour = $accountHandler->getLimitHour(); |
||
167 | |||
168 | $crTaskPending = new \CriteriaCompo(); |
||
169 | $crTaskPending->add(new \Criteria('status', Constants::STATUS_PENDING)); |
||
170 | $crTaskDone = new \CriteriaCompo(); |
||
171 | $crTaskDone->add(new \Criteria('status', Constants::STATUS_DONE)); |
||
172 | $crTaskDone->add(new \Criteria('datedone', time() - 3600, '>')); |
||
173 | $tasksCountPending = $this->getCount($crTaskPending); |
||
174 | $tasksCountDone = $this->getCount($crTaskDone); |
||
175 | $counterDone = 0; |
||
176 | if ($log_level > 0) { |
||
177 | echo '<br>Sart processTasks'; |
||
178 | echo '<br>time - 3600: ' . \formatTimestamp(time() - 3600, 'm'); |
||
179 | echo '<br>tasksCountPending: ' . $tasksCountPending; |
||
180 | echo '<br>tasksCountDone: ' . $tasksCountDone; |
||
181 | } |
||
182 | if (($tasksCountPending > 0) && ($tasksCountDone < $limitHour || 0 == $limitHour)) { |
||
183 | if ($limitHour > 0) { |
||
184 | $crTaskPending->setLimit($limitHour); |
||
185 | } |
||
186 | $tasksAll = $this->getAll($crTaskPending); |
||
187 | foreach (\array_keys($tasksAll) as $i) { |
||
188 | // check whether task is still pending |
||
189 | // ignore it if meanwhile another one started to process the task |
||
190 | if ($log_level > 0) { |
||
191 | echo '<br>tasksAll key: ' . $i; |
||
192 | } |
||
193 | if ((Constants::STATUS_PENDING == (int)$tasksAll[$i]->getVar('status')) |
||
194 | && ($tasksCountDone < $limitHour || 0 == $limitHour)) { |
||
195 | $taskProcessObj = $this->get($i); |
||
196 | $taskProcessObj->setVar('status', Constants::STATUS_PROCESSING); |
||
197 | if ($this->insert($taskProcessObj)) { |
||
198 | $mailsHandler = new MailHandler(); |
||
199 | $mailParams = json_decode($tasksAll[$i]->getVar('params', 'n'), true); |
||
200 | $mailParams['recipients'] = $tasksAll[$i]->getVar('recipient'); |
||
201 | $mailParams['taskId'] = $i; |
||
202 | $mailsHandler->setParams($mailParams); |
||
203 | $mailsHandler->setType($tasksAll[$i]->getVar('type')); |
||
204 | // send mails |
||
205 | $result = $mailsHandler->execute(); |
||
206 | unset($mailsHandler); |
||
207 | //update task list corresponding the result |
||
208 | if ($result) { |
||
209 | $taskProcessObj->setVar('status', Constants::STATUS_DONE); |
||
210 | $taskProcessObj->setVar('datedone', time()); |
||
211 | $counterDone++; |
||
212 | if ($log_level > 0) { |
||
213 | echo ' - done'; |
||
214 | } |
||
215 | } else { |
||
216 | $taskProcessObj->setVar('status', Constants::STATUS_PENDING); |
||
217 | if ($log_level > 0) { |
||
218 | echo ' - failed'; |
||
219 | } |
||
220 | } |
||
221 | $this->insert($taskProcessObj); |
||
222 | } else { |
||
223 | echo ' - skipped'; |
||
224 | } |
||
225 | } |
||
226 | // check once more number of done |
||
227 | $tasksCountDone = $this->getCount($crTaskDone); |
||
228 | } |
||
229 | } |
||
230 | if ($log_level > 0) { |
||
231 | echo '<br>End processTasks'; |
||
232 | } |
||
233 | return ['pending' => $tasksCountPending, 'done' => $counterDone]; |
||
234 | } |
||
236 |
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: