| Conditions | 15 |
| Paths | 5 |
| Total Lines | 45 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 76 | public function run(?callable $callback = null) |
||
| 77 | { |
||
| 78 | $folders = $this->account->getFolders(); |
||
| 79 | $actions = $this->account->getActions(); |
||
| 80 | if ($folders && $actions && ($imap = $this->getImapConnection()) && ($folders = array_intersect($folders, array_keys($imap->getFolders(false))))) { |
||
| 81 | $flagSeen = \App\Mail::getConfig('scanner', 'flag_seen'); |
||
| 82 | foreach ($folders as $folderName) { |
||
| 83 | $this->log->start(); |
||
| 84 | $uid = $this->account->getLastUid($folderName); |
||
| 85 | if (!\is_int($uid)) { |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | |||
| 89 | $messageCollection = $imap->getMessagesGreaterThanUid($folderName, $uid, $this->limit); |
||
| 90 | foreach ($messageCollection as $message) { |
||
| 91 | if (!$this->log->isRunning()) { |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | if (($callback && $callback($this))) { |
||
| 95 | break 2; |
||
| 96 | } |
||
| 97 | --$this->limit; |
||
| 98 | $messageObject = (new Message\Imap())->setMessage($message); |
||
| 99 | try { |
||
| 100 | foreach ($actions as $action) { |
||
| 101 | $this->getAction($action)->setAccount($this->account)->setMessage($messageObject)->process(); |
||
| 102 | } |
||
| 103 | ++$this->count; |
||
| 104 | $this->account->setLastUid($messageObject->getMsgUid(), $folderName); |
||
| 105 | $this->log->updateCount($this->count); |
||
| 106 | if ($flagSeen) { |
||
| 107 | $messageObject->setFlag('Seen'); |
||
| 108 | } |
||
| 109 | } catch (\Throwable $th) { |
||
| 110 | $message = $th->getMessage(); |
||
| 111 | \App\Log::error("Mail Scanner - Account: {$this->account->getSource()->getId()}, folder: {$folderName}, UID: {$messageObject->getMsgUid()}, message: " . $message); |
||
| 112 | if ($th instanceof \App\Exceptions\AppException) { |
||
| 113 | $message = $th->getDisplayMessage(); |
||
| 114 | } |
||
| 115 | $this->log->close(ScannerLog::STATUS_ERROR, $message, $action); |
||
| 116 | break; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | $this->log->close(); |
||
| 121 | } |
||
| 164 |