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 |
||
| 11 | class ServerResponse extends Entity |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var bool |
||
| 15 | */ |
||
| 16 | protected $ok; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var null |
||
| 20 | */ |
||
| 21 | protected $result; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var null |
||
| 25 | */ |
||
| 26 | protected $error_code; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var null |
||
| 30 | */ |
||
| 31 | protected $description; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * ServerResponse constructor. |
||
| 35 | * |
||
| 36 | * @param array $data |
||
| 37 | * @param $bot_name |
||
| 38 | */ |
||
| 39 | 14 | public function __construct(array $data, $bot_name) |
|
| 40 | { |
||
| 41 | 14 | if (isset($data['ok']) && isset($data['result'])) { |
|
| 42 | 12 | if (is_array($data['result'])) { |
|
| 43 | 11 | if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) { |
|
| 44 | //Get Update |
||
| 45 | 2 | foreach ($data['result'] as $update) { |
|
| 46 | 2 | $this->result[] = new Update($update, $bot_name); |
|
| 47 | } |
||
| 48 | 9 | } elseif ($data['ok'] && !$this->isAssoc($data['result']) && isset($data['result'][0]['user'])) { |
|
| 49 | //Response from getChatAdministrators |
||
| 50 | $this->result = []; |
||
|
|
|||
| 51 | foreach ($data['result'] as $user) { |
||
| 52 | array_push($this->result, new ChatMember($user)); |
||
| 53 | } |
||
| 54 | 9 | } elseif ($data['ok'] && $this->isAssoc($data['result'])) { |
|
| 55 | 9 | if (isset($data['result']['total_count'])) { |
|
| 56 | //Response from getUserProfilePhotos |
||
| 57 | 1 | $this->result = new UserProfilePhotos($data['result']); |
|
| 58 | 8 | } elseif (isset($data['result']['file_id'])) { |
|
| 59 | //Response from getFile |
||
| 60 | 1 | $this->result = new File($data['result']); |
|
| 61 | 7 | } elseif (isset($data['result']['username'])) { |
|
| 62 | //Response from getMe |
||
| 63 | $this->result = new User($data['result']); |
||
| 64 | 7 | } elseif (isset($data['result']['id'])) { |
|
| 65 | //Response from getChat |
||
| 66 | $this->result = new Chat($data['result']); |
||
| 67 | 7 | } elseif (isset($data['result']['user'])) { |
|
| 68 | //Response from getChatMember |
||
| 69 | $this->result = new ChatMember($data['result']); |
||
| 70 | } else { |
||
| 71 | //Response from sendMessage |
||
| 72 | 7 | $this->result = new Message($data['result'], $bot_name); |
|
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | 11 | $this->ok = $data['ok']; |
|
| 77 | 11 | $this->error_code = null; |
|
| 78 | 11 | $this->description = null; |
|
| 79 | } else { |
||
| 80 | 2 | if ($data['ok'] && $data['result'] === true) { |
|
| 81 | //Response from setWebhook set |
||
| 82 | 2 | $this->ok = $data['ok']; |
|
| 83 | 2 | $this->result = true; |
|
| 84 | 2 | $this->error_code = null; |
|
| 85 | |||
| 86 | 2 | View Code Duplication | if (isset($data['description'])) { |
| 87 | 1 | $this->description = $data['description']; |
|
| 88 | } else { |
||
| 89 | 2 | $this->description = ''; |
|
| 90 | } |
||
| 91 | } elseif (is_numeric($data['result'])) { |
||
| 92 | //Response from getChatMembersCount |
||
| 93 | $this->result = $data['result']; |
||
| 94 | } else { |
||
| 95 | $this->ok = false; |
||
| 96 | $this->result = null; |
||
| 97 | $this->error_code = $data['error_code']; |
||
| 98 | 12 | $this->description = $data['description']; |
|
| 99 | } |
||
| 100 | } |
||
| 101 | } else { |
||
| 102 | //webHook not set |
||
| 103 | 2 | $this->ok = false; |
|
| 104 | |||
| 105 | 2 | if (isset($data['result'])) { |
|
| 106 | $this->result = $data['result']; |
||
| 107 | } else { |
||
| 108 | 2 | $this->result = null; |
|
| 109 | } |
||
| 110 | |||
| 111 | 2 | if (isset($data['error_code'])) { |
|
| 112 | 2 | $this->error_code = $data['error_code']; |
|
| 113 | } else { |
||
| 114 | $this->error_code = null; |
||
| 115 | } |
||
| 116 | |||
| 117 | 2 | View Code Duplication | if (isset($data['description'])) { |
| 118 | 2 | $this->description = $data['description']; |
|
| 119 | } else { |
||
| 120 | $this->description = null; |
||
| 121 | } |
||
| 122 | |||
| 123 | //throw new TelegramException('ok(variable) is not set!'); |
||
| 124 | } |
||
| 125 | 14 | } |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Check if array is associative |
||
| 129 | * |
||
| 130 | * @param array $array |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | 11 | protected function isAssoc(array $array) |
|
| 134 | { |
||
| 135 | 11 | return (bool) count(array_filter(array_keys($array), 'is_string')); |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * If response is ok |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | 5 | public function isOk() |
|
| 144 | { |
||
| 145 | 5 | return $this->ok; |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get result |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 14 | public function getResult() |
|
| 154 | { |
||
| 155 | 14 | return $this->result; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get error code |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | 5 | public function getErrorCode() |
|
| 164 | { |
||
| 165 | 5 | return $this->error_code; |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get description |
||
| 170 | * |
||
| 171 | * @return null |
||
| 172 | */ |
||
| 173 | 5 | public function getDescription() |
|
| 174 | { |
||
| 175 | 5 | return $this->description; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Print error |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function printError() |
||
| 187 | } |
||
| 188 |
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..