Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
17 | class Message |
||
18 | { |
||
19 | |||
20 | protected $config = []; |
||
21 | protected $client = null; |
||
22 | |||
23 | /** |
||
24 | * Message constructor. |
||
25 | * @param array $config |
||
26 | */ |
||
27 | 44 | View Code Duplication | public function __construct(array $config, Client $client) |
28 | { |
||
29 | 44 | $this->client = $client; |
|
30 | |||
31 | 44 | if ($this->checkConfig($config) && !empty($client)) { |
|
32 | 40 | $this->config = $config; |
|
33 | 30 | } else { |
|
34 | 4 | throw new ConfigException(trans('messenger::errors.config_not_valid')); |
|
35 | } |
||
36 | |||
37 | 30 | } |
|
38 | |||
39 | 44 | protected function checkConfig(array $config) |
|
43 | |||
44 | /** |
||
45 | * @return array |
||
46 | */ |
||
47 | 4 | public function getConfig() |
|
51 | |||
52 | /** |
||
53 | * @return Client|null |
||
54 | */ |
||
55 | 12 | public function getClient() |
|
59 | |||
60 | |||
61 | 8 | public function sendTextMessage($recipientId, $messageText) |
|
70 | |||
71 | |||
72 | 4 | View Code Duplication | public function sendImageMessage($recipientId, $picture) |
90 | |||
91 | 4 | View Code Duplication | public function sendCard($recipientId, $card) |
106 | |||
107 | |||
108 | 20 | public function callSendAPI($messageData) |
|
123 | |||
124 | |||
125 | 4 | public function persistMenu($menu) |
|
136 | |||
137 | 8 | public function getCurrentUserProfile($uid, $fields = null) |
|
146 | } |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: