| Total Complexity | 48 |
| Total Lines | 210 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Email often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Email, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Email extends Sender |
||
| 11 | { |
||
| 12 | public function checkConfig() |
||
| 13 | { |
||
| 14 | if (!isset($this->config['host'])) { |
||
| 15 | throw new \Exception('请传入邮箱服务器地址'); |
||
| 16 | } |
||
| 17 | |||
| 18 | if (!isset($this->config['username'])) { |
||
| 19 | throw new \Exception('请传入邮箱服务器认证账号'); |
||
| 20 | } |
||
| 21 | |||
| 22 | if (!isset($this->config['password'])) { |
||
| 23 | throw new \Exception('请传入邮箱服务器认证密码'); |
||
| 24 | } |
||
| 25 | |||
| 26 | if (!isset($this->config['fromEmail'])) { |
||
| 27 | throw new \Exception('请设置发件人邮箱'); |
||
| 28 | } |
||
| 29 | |||
| 30 | if (!isset($this->config['fromName'])) { |
||
| 31 | $this->config['fromName'] = $this->config['fromEmail']; |
||
| 32 | } |
||
| 33 | |||
| 34 | if (!isset($this->config['debug'])) { |
||
| 35 | $this->config['debug'] = 0; |
||
| 36 | } |
||
| 37 | if (!isset($this->config['SMTPSecure'])) { |
||
| 38 | $this->config['SMTPSecure'] = false; |
||
| 39 | $this->config['SMTPOptions'] = array( |
||
| 40 | 'ssl' => array( |
||
| 41 | 'verify_peer' => false, |
||
| 42 | 'verify_peer_name' => false, |
||
| 43 | 'allow_self_signed' => true |
||
| 44 | ) |
||
| 45 | ); |
||
| 46 | } |
||
| 47 | if (!isset($this->config['SMTPOptions'])) { |
||
| 48 | $this->config['SMTPOptions'] = [ |
||
| 49 | 'ssl' => [ |
||
| 50 | 'verify_peer' => false, |
||
| 51 | 'verify_peer_name' => false, |
||
| 52 | 'allow_self_signed' => true, |
||
| 53 | ], |
||
| 54 | ]; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!isset($this->config['port'])) { |
||
| 58 | $this->config['port'] = 25; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | public function checkMsgFormate($msg) |
||
| 63 | { |
||
| 64 | if (!isset($msg['subject'])) { |
||
| 65 | throw new \Exception('邮件主题未设置'); |
||
| 66 | } |
||
| 67 | |||
| 68 | if (!isset($msg['body'])) { |
||
| 69 | throw new \Exception('邮件正文未设置'); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (!isset($msg['to'])) { |
||
| 73 | throw new \Exception('收件人未设置'); |
||
| 74 | }else{ |
||
| 75 | $msg['to'] = $this->checkEmailAddress($msg['to']); |
||
| 76 | } |
||
| 77 | if (isset($msg['cc'])){ |
||
| 78 | $msg['cc'] = $this->checkEmailAddress($msg['cc']); |
||
| 79 | } |
||
| 80 | if (isset($msg['bcc'])){ |
||
| 81 | $msg['bcc'] = $this->checkEmailAddress($msg['bcc']); |
||
| 82 | } |
||
| 83 | if (isset($msg['attachments'])) { |
||
| 84 | $msg['attachments'] = $this->checkAttachments($msg['attachments']); |
||
| 85 | } |
||
| 86 | return $msg; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function checkEmailAddress($emailAddresses) |
||
| 90 | { |
||
| 91 | if (is_string($emailAddresses)){ |
||
| 92 | if (filter_var($emailAddresses,FILTER_VALIDATE_EMAIL)){ |
||
| 93 | return [ |
||
| 94 | ['email'=>$emailAddresses,'name'=>$emailAddresses] |
||
| 95 | ]; |
||
| 96 | }else{ |
||
| 97 | throw new \Exception("邮箱格式不正确:".$emailAddresses); |
||
| 98 | } |
||
| 99 | }elseif (is_array($emailAddresses)){ |
||
| 100 | foreach ($emailAddresses as &$emailAddress) { |
||
| 101 | if (is_string($emailAddress)){ |
||
| 102 | $emailAddress = ['email'=>$emailAddress,'name'=>$emailAddress]; |
||
| 103 | |||
| 104 | }else{ |
||
| 105 | if (!isset($emailAddress['email'])){ |
||
| 106 | throw new Exception("缺失邮箱地址[email]"); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | if (!filter_var($emailAddress['email'],FILTER_VALIDATE_EMAIL)){ |
||
| 110 | throw new \Exception("邮箱格式不正确:".$emailAddress); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | return $emailAddresses; |
||
| 114 | } |
||
| 115 | |||
| 116 | |||
| 117 | } |
||
| 118 | public function checkAttachments($attachments) |
||
| 147 | |||
| 148 | } |
||
| 149 | |||
| 150 | public function send($msg) |
||
| 151 | { |
||
| 152 | $msg = $this->checkMsgFormate($msg); |
||
| 153 | return $this->sendMail($msg); |
||
| 154 | } |
||
| 155 | |||
| 156 | private function sendMail($msg) |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.