Total Complexity | 48 |
Total Lines | 211 |
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() |
||
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 | |||
87 | return $msg; |
||
88 | } |
||
89 | |||
90 | public function checkEmailAddress($emailAddresses) |
||
91 | { |
||
92 | if (is_string($emailAddresses)) { |
||
93 | if (filter_var($emailAddresses, FILTER_VALIDATE_EMAIL)) { |
||
94 | return [ |
||
95 | ['email' => $emailAddresses, 'name' => $emailAddresses], |
||
96 | ]; |
||
97 | } else { |
||
98 | throw new \Exception('邮箱格式不正确:'.$emailAddresses); |
||
99 | } |
||
100 | } elseif (is_array($emailAddresses)) { |
||
101 | foreach ($emailAddresses as &$emailAddress) { |
||
102 | if (is_string($emailAddress)) { |
||
103 | $emailAddress = ['email' => $emailAddress, 'name' => $emailAddress]; |
||
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 | |||
114 | return $emailAddresses; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | public function checkAttachments($attachments) |
||
148 | } |
||
149 | |||
150 | public function send($msg) |
||
151 | { |
||
152 | $msg = $this->checkMsgFormate($msg); |
||
153 | |||
154 | return $this->sendMail($msg); |
||
155 | } |
||
156 | |||
157 | private function sendMail($msg) |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 |
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.