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 |
||
| 12 | class Client |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * $apiEndpoint. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | public $apiEndpoint = 'http://api.every8d.com/API21/HTTP'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * $credit. |
||
| 23 | * |
||
| 24 | * @var float |
||
| 25 | */ |
||
| 26 | public $credit = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * $userId. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $userId; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * $password. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $password; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * $httpClient. |
||
| 44 | * |
||
| 45 | * @var \Http\Client\HttpClient |
||
| 46 | */ |
||
| 47 | protected $httpClient; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * $messageFactory. |
||
| 51 | * |
||
| 52 | * @var \Http\Message\MessageFactory |
||
| 53 | */ |
||
| 54 | protected $messageFactory; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * __construct. |
||
| 58 | * |
||
| 59 | * @param string $userId |
||
| 60 | * @param string $password |
||
| 61 | * @param \Http\Client\HttpClient $httpClient |
||
| 62 | * @param \Http\Message\MessageFactory $messageFactory |
||
| 63 | */ |
||
| 64 | 5 | public function __construct($userId, $password, HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * https. |
||
| 74 | * |
||
| 75 | * @return $this |
||
| 76 | */ |
||
| 77 | 2 | public function https() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * send. |
||
| 86 | * |
||
| 87 | * @param array $params |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 2 | public function send($params) |
|
| 91 | { |
||
| 92 | 2 | $response = $this->doRequest('sendSMS.ashx', array_filter(array_merge([ |
|
| 93 | 2 | 'UID' => $this->userId, |
|
| 94 | 2 | 'PWD' => $this->password, |
|
| 95 | 2 | 'SB' => null, |
|
| 96 | 2 | 'MSG' => null, |
|
| 97 | 2 | 'DEST' => null, |
|
| 98 | 2 | 'ST' => null, |
|
| 99 | 2 | 'RETRYTIME' => null, |
|
| 100 | 2 | ], $this->remapParams($params)))); |
|
| 101 | |||
| 102 | 2 | if ($this->isValidResponse($response) === false) { |
|
| 103 | 1 | throw new DomainException($response, 500); |
|
| 104 | } |
||
| 105 | |||
| 106 | 1 | return $this->parseResponse($response); |
|
|
|
|||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * sendMMS. |
||
| 111 | * |
||
| 112 | * @param array $params |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | 1 | public function sendMMS($params) |
|
| 116 | { |
||
| 117 | 1 | $response = $this->https()->doRequest('snedMMS.ashx', array_filter(array_merge([ |
|
| 118 | 1 | 'UID' => $this->userId, |
|
| 119 | 1 | 'PWD' => $this->password, |
|
| 120 | 1 | 'SB' => null, |
|
| 121 | 1 | 'MSG' => null, |
|
| 122 | 1 | 'DEST' => null, |
|
| 123 | 1 | 'ST' => null, |
|
| 124 | 1 | 'RETRYTIME' => null, |
|
| 125 | 1 | 'ATTACHMENT' => null, |
|
| 126 | 1 | 'TYPE' => null, |
|
| 127 | 1 | ], $this->remapParams($params)))); |
|
| 128 | |||
| 129 | 1 | if ($this->isValidResponse($response) === false) { |
|
| 130 | throw new DomainException($response, 500); |
||
| 131 | } |
||
| 132 | |||
| 133 | 1 | return $this->parseResponse($response); |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * credit. |
||
| 138 | * |
||
| 139 | * @return float |
||
| 140 | */ |
||
| 141 | 4 | public function credit() |
|
| 142 | { |
||
| 143 | 4 | if (is_null($this->credit) === false) { |
|
| 144 | 2 | return $this->credit; |
|
| 145 | } |
||
| 146 | |||
| 147 | 2 | $response = $this->doRequest('getCredit.ashx', [ |
|
| 148 | 2 | 'UID' => $this->userId, |
|
| 149 | 2 | 'PWD' => $this->password, |
|
| 150 | 2 | ]); |
|
| 151 | |||
| 152 | 2 | if ($this->isValidResponse($response) === false) { |
|
| 153 | 1 | throw new DomainException($response, 500); |
|
| 154 | } |
||
| 155 | |||
| 156 | 1 | return $this->setCredit($response)->credit; |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * setCredit. |
||
| 161 | * |
||
| 162 | * @param string $credit |
||
| 163 | */ |
||
| 164 | 3 | protected function setCredit($credit) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * isValidResponse. |
||
| 173 | * |
||
| 174 | * @param string $response |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | 5 | protected function isValidResponse($response) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * doRequest. |
||
| 185 | * |
||
| 186 | * @param string $uri |
||
| 187 | * @param array $params |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | 5 | protected function doRequest($uri, $params) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * remapParams. |
||
| 206 | * |
||
| 207 | * @param array $params |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | 3 | protected function remapParams($params) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * parseResponse. |
||
| 247 | * |
||
| 248 | * @param array $response |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | 2 | protected function parseResponse($response) |
|
| 263 | } |
||
| 264 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: