Complex classes like Sms 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Sms, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Sms |
||
11 | { |
||
12 | /** |
||
13 | * sms send task name |
||
14 | */ |
||
15 | const TASK = 'PhpSms'; |
||
16 | |||
17 | /** |
||
18 | * log agent`s name |
||
19 | */ |
||
20 | const LOG_AGENT = 'Log'; |
||
21 | |||
22 | /** |
||
23 | * agents instance |
||
24 | */ |
||
25 | protected static $agents; |
||
26 | |||
27 | /** |
||
28 | * agents`s name |
||
29 | * |
||
30 | * @var |
||
31 | */ |
||
32 | protected static $agentsName = []; |
||
33 | |||
34 | /** |
||
35 | * agents`s config |
||
36 | * |
||
37 | * @var |
||
38 | */ |
||
39 | protected static $agentsConfig = []; |
||
40 | |||
41 | /** |
||
42 | * whether to enable queue |
||
43 | * |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected static $enableQueue = false; |
||
47 | |||
48 | /** |
||
49 | * queue work |
||
50 | * |
||
51 | * @var null |
||
52 | */ |
||
53 | protected static $howToUseQueue = null; |
||
54 | |||
55 | /** |
||
56 | * sms already pushed to queue |
||
57 | * |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $pushedToQueue = false; |
||
61 | |||
62 | /** |
||
63 | * hook handlers |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected static $enableHooks = [ |
||
68 | 'beforeRun', |
||
69 | 'afterRun', |
||
70 | ]; |
||
71 | |||
72 | /** |
||
73 | * sms data |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $smsData = [ |
||
78 | 'to' => null, |
||
79 | 'templates' => [], |
||
80 | 'content' => '', |
||
81 | 'templateData' => [], |
||
82 | 'voiceCode' => null, |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * first agent for send sms/voice verify |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $firstAgent = null; |
||
91 | |||
92 | /** |
||
93 | * construct |
||
94 | */ |
||
95 | 3 | public function __construct() |
|
96 | { |
||
97 | 3 | self::init(); |
|
98 | 3 | } |
|
99 | |||
100 | /** |
||
101 | * create sms instance and set templates |
||
102 | * |
||
103 | * @param null $agentName |
||
104 | * @param null $tempId |
||
105 | * |
||
106 | * @return Sms |
||
107 | */ |
||
108 | public static function make($agentName = null, $tempId = null) |
||
123 | |||
124 | /** |
||
125 | * send voice verify |
||
126 | * |
||
127 | * @param $code |
||
128 | * |
||
129 | * @return Sms |
||
130 | */ |
||
131 | 3 | public static function voice($code) |
|
138 | |||
139 | /** |
||
140 | * set how to use queue. |
||
141 | * |
||
142 | * @param $enable |
||
143 | * @param $handler |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | 3 | public static function queue($enable = null, $handler = null) |
|
148 | { |
||
149 | 3 | if ($enable === null && $handler === null) { |
|
150 | 3 | return self::$enableQueue; |
|
151 | } |
||
152 | 3 | if (is_callable($enable)) { |
|
153 | 3 | $handler = $enable; |
|
154 | 3 | $enable = true; |
|
155 | 3 | } |
|
156 | 3 | self::$enableQueue = (bool) $enable; |
|
157 | 3 | if (is_callable($handler)) { |
|
158 | 3 | self::$howToUseQueue = $handler; |
|
|
|||
159 | 3 | } |
|
160 | |||
161 | 3 | return self::$enableQueue; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * set the mobile number |
||
166 | * |
||
167 | * @param $mobile |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | 3 | public function to($mobile) |
|
172 | { |
||
173 | 3 | $this->smsData['to'] = $mobile; |
|
174 | |||
175 | 3 | return $this; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * set content for content sms |
||
180 | * |
||
181 | * @param $content |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | 3 | public function content($content) |
|
186 | { |
||
187 | 3 | $this->smsData['content'] = trim((String) $content); |
|
188 | |||
189 | 3 | return $this; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * set template id for template sms |
||
194 | * |
||
195 | * @param $agentName |
||
196 | * @param $tempId |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | 3 | public function template($agentName, $tempId = null) |
|
201 | { |
||
202 | 3 | if (is_array($agentName)) { |
|
203 | 3 | foreach ($agentName as $k => $v) { |
|
204 | 3 | $this->template($k, $v); |
|
205 | 3 | } |
|
206 | 3 | } elseif ($agentName && $tempId) { |
|
207 | 3 | if (!isset($this->smsData['templates']) || !is_array($this->smsData['templates'])) { |
|
208 | $this->smsData['templates'] = []; |
||
209 | } |
||
210 | 3 | $this->smsData['templates']["$agentName"] = $tempId; |
|
211 | 3 | } |
|
212 | |||
213 | 3 | return $this; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * set data for template sms |
||
218 | * |
||
219 | * @param array $data |
||
220 | * |
||
221 | * @return $this |
||
222 | */ |
||
223 | 12 | public function data(array $data) |
|
224 | { |
||
225 | 12 | $this->smsData['templateData'] = $data; |
|
226 | |||
227 | 3 | return $this; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * set the first agent |
||
232 | * |
||
233 | * @param $name |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | 3 | public function agent($name) |
|
238 | { |
||
239 | 3 | $this->firstAgent = (String) $name; |
|
240 | |||
241 | 3 | return $this; |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * start send |
||
246 | * |
||
247 | * @param bool $immediately |
||
248 | * |
||
249 | * @return mixed |
||
250 | */ |
||
251 | 9 | public function send($immediately = false) |
|
252 | { |
||
253 | 9 | $this->validator(); |
|
254 | |||
255 | // if disable push to queue, |
||
256 | // send the sms immediately. |
||
257 | 9 | if (!self::$enableQueue) { |
|
258 | 6 | $immediately = true; |
|
259 | 6 | } |
|
260 | |||
261 | // whatever 'PhpSms' whether to enable or disable push to queue, |
||
262 | // if you are already pushed sms instance to queue, |
||
263 | // you can recall the method `send()` in queue job without `true` parameter. |
||
264 | // |
||
265 | // So this mechanism in order to make you convenient use the method `send()` in queue system. |
||
266 | 9 | if ($this->pushedToQueue) { |
|
267 | 3 | $immediately = true; |
|
268 | 3 | } |
|
269 | |||
270 | // whether to send sms immediately, |
||
271 | // or push it to queue. |
||
272 | 9 | if ($immediately) { |
|
273 | 9 | $result = Balancer::run(self::TASK, $this->getData(), $this->firstAgent); |
|
274 | 9 | } else { |
|
275 | 3 | $result = $this->push(); |
|
276 | } |
||
277 | |||
278 | 9 | return $result; |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * push sms send task to queue |
||
283 | * |
||
284 | * @throws \Exception | PhpSmsException |
||
285 | * |
||
286 | * @return mixed |
||
287 | */ |
||
288 | 3 | protected function push() |
|
303 | |||
304 | /** |
||
305 | * get sms data |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | 27 | public function getData() |
|
313 | |||
314 | /** |
||
315 | * init |
||
316 | * |
||
317 | * @return mixed |
||
318 | */ |
||
319 | 3 | protected static function init() |
|
325 | |||
326 | /** |
||
327 | * generator a sms send task |
||
328 | * |
||
329 | * @return null |
||
330 | */ |
||
331 | 6 | public static function generatorTask() |
|
342 | |||
343 | /** |
||
344 | * configuration |
||
345 | */ |
||
346 | 3 | protected static function configuration() |
|
347 | { |
||
348 | 3 | $config = []; |
|
349 | 3 | if (empty(self::$agentsName)) { |
|
350 | $config = include __DIR__ . '/../config/phpsms.php'; |
||
351 | self::generatorAgentsName($config); |
||
352 | } |
||
353 | 3 | if (empty(self::$agentsConfig)) { |
|
354 | $config = $config ?: include __DIR__ . '/../config/phpsms.php'; |
||
355 | self::generatorAgentsConfig($config); |
||
356 | } |
||
357 | 3 | self::configValidator(); |
|
358 | 3 | } |
|
359 | |||
360 | /** |
||
361 | * generate enabled agents name |
||
362 | * |
||
363 | * @param array $config |
||
364 | */ |
||
365 | protected static function generatorAgentsName($config) |
||
372 | |||
373 | /** |
||
374 | * generator agents config |
||
375 | * |
||
376 | * @param array $config |
||
377 | */ |
||
378 | protected static function generatorAgentsConfig($config) |
||
383 | |||
384 | /** |
||
385 | * config value validator |
||
386 | * |
||
387 | * @throws PhpSmsException |
||
388 | */ |
||
389 | 3 | protected static function configValidator() |
|
403 | |||
404 | /** |
||
405 | * create drivers for sms send task |
||
406 | * |
||
407 | * @param $task |
||
408 | */ |
||
409 | 9 | protected static function createAgents($task) |
|
436 | |||
437 | /** |
||
438 | * get agent config data by name |
||
439 | * |
||
440 | * @param $name |
||
441 | * |
||
442 | * @return array |
||
443 | */ |
||
444 | protected static function getAgentConfigData($name) |
||
449 | |||
450 | /** |
||
451 | * get a sms agent instance, |
||
452 | * if null, will create a new agent instance |
||
453 | * |
||
454 | * @param $name |
||
455 | * @param array $configData |
||
456 | * |
||
457 | * @throws PhpSmsException |
||
458 | * |
||
459 | * @return mixed |
||
460 | */ |
||
461 | 9 | protected static function getSmsAgent($name, array $configData) |
|
474 | |||
475 | /** |
||
476 | * validate |
||
477 | * |
||
478 | * @throws PhpSmsException |
||
479 | */ |
||
480 | 9 | protected function validator() |
|
488 | |||
489 | /** |
||
490 | * set enable agents |
||
491 | * |
||
492 | * @param $agentName |
||
493 | * @param null $options |
||
494 | */ |
||
495 | 3 | public static function enable($agentName, $options = null) |
|
496 | { |
||
509 | |||
510 | /** |
||
511 | * set config for available agents |
||
512 | * |
||
513 | * @param $agentName |
||
514 | * @param array $config |
||
515 | * |
||
516 | * @throws PhpSmsException |
||
517 | */ |
||
518 | 3 | public static function agents($agentName, array $config = []) |
|
531 | |||
532 | /** |
||
533 | * get enable agents |
||
534 | * |
||
535 | * @return array |
||
536 | */ |
||
537 | 3 | public static function getEnableAgents() |
|
541 | |||
542 | /** |
||
543 | * get agents config info |
||
544 | * |
||
545 | * @return array |
||
546 | */ |
||
547 | 3 | public static function getAgentsConfig() |
|
551 | |||
552 | /** |
||
553 | * overload static method |
||
554 | * |
||
555 | * @param $name |
||
556 | * @param $args |
||
557 | * |
||
558 | * @throws PhpSmsException |
||
559 | */ |
||
560 | public static function __callStatic($name, $args) |
||
577 | |||
578 | /** |
||
579 | * overload method |
||
580 | * |
||
581 | * @param $name |
||
582 | * @param $args |
||
583 | * |
||
584 | * @throws PhpSmsException |
||
585 | * @throws \Exception |
||
586 | */ |
||
587 | public function __call($name, $args) |
||
595 | } |
||
596 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..