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 |
||
| 25 | class Manga extends API { |
||
| 26 | |||
| 27 | const READING = 'Reading'; |
||
| 28 | const PLAN_TO_READ = 'Plan to Read'; |
||
| 29 | const DROPPED = 'Dropped'; |
||
| 30 | const ON_HOLD = 'On Hold'; |
||
| 31 | const COMPLETED = 'Completed'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Map API constants to display constants |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $const_map = [ |
||
| 38 | MangaReadingStatus::READING => self::READING, |
||
| 39 | MangaReadingStatus::PLAN_TO_READ => self::PLAN_TO_READ, |
||
| 40 | MangaReadingStatus::ON_HOLD => self::ON_HOLD, |
||
| 41 | MangaReadingStatus::DROPPED => self::DROPPED, |
||
| 42 | MangaReadingStatus::COMPLETED => self::COMPLETED |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The base url for api requests |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $base_url = "https://hummingbird.me/"; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Make an authenticated manga API call |
||
| 53 | * |
||
| 54 | * @param string $type - the HTTP verb |
||
| 55 | * @param string $url |
||
| 56 | * @param string|null $json |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | protected function _manga_api_call($type, $url, $json = NULL) |
||
| 60 | { |
||
| 61 | $token = $this->container->get('auth') |
||
| 62 | ->get_auth_token(); |
||
| 63 | |||
| 64 | // Set the token cookie, with the authentication token |
||
| 65 | // from the auth class. |
||
| 66 | $cookieJar = $this->cookieJar; |
||
| 67 | $cookie_data = new SetCookie([ |
||
| 68 | 'Name' => 'token', |
||
| 69 | 'Value' => $token, |
||
| 70 | 'Domain' => 'hummingbird.me' |
||
| 71 | ]); |
||
| 72 | $cookieJar->setCookie($cookie_data); |
||
| 73 | |||
| 74 | $config = [ |
||
| 75 | 'cookies' => $cookieJar |
||
| 76 | ]; |
||
| 77 | |||
| 78 | if ( ! is_null($json)) |
||
| 79 | { |
||
| 80 | $config['json'] = $json; |
||
| 81 | } |
||
| 82 | |||
| 83 | $result = $this->client->request(strtoupper($type), $url, $config); |
||
| 84 | |||
| 85 | return [ |
||
| 86 | 'statusCode' => $result->getStatusCode(), |
||
| 87 | 'body' => $result->getBody() |
||
| 88 | ]; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Add a manga to the list |
||
| 93 | * |
||
| 94 | * @param array $data |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | public function add($data) |
||
| 98 | { |
||
| 99 | $object = [ |
||
| 100 | 'manga_library_entry' => [ |
||
| 101 | 'status' => $data['status'], |
||
| 102 | 'manga_id' => $data['id'] |
||
| 103 | ] |
||
| 104 | ]; |
||
| 105 | |||
| 106 | return $this->_manga_api_call('post', 'manga_library_entries', $object); |
||
|
|
|||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Update the selected manga |
||
| 111 | * |
||
| 112 | * @param array $data |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function update($data) |
||
| 116 | { |
||
| 117 | $id = $data['id']; |
||
| 118 | |||
| 119 | return $this->_manga_api_call( |
||
| 120 | 'put', |
||
| 121 | "manga_library_entries/{$id}", |
||
| 122 | ['manga_library_entry' => $data] |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Delete a manga entry |
||
| 128 | * |
||
| 129 | * @param array $data |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function delete($data) |
||
| 133 | { |
||
| 134 | $id = $data['id']; |
||
| 135 | |||
| 136 | return $this->_manga_api_call('delete', "manga_library_entries/{$id}"); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Search for manga by name |
||
| 141 | * |
||
| 142 | * @param string $name |
||
| 143 | * @return array |
||
| 144 | * @throws RuntimeException |
||
| 145 | */ |
||
| 146 | public function search($name) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get a category out of the full list |
||
| 173 | * |
||
| 174 | * @param string $status |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | public function get_list($status) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Retrieve the list from the hummingbird api |
||
| 185 | * |
||
| 186 | * @param string $status |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | public function _get_list_from_api($status = "All") |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Transform the response to be more consistent |
||
| 206 | * |
||
| 207 | * @param \GuzzleHttp\Message\Response $response |
||
| 208 | * @codeCoverageIgnore |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | private function transform($response) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get the details of a manga |
||
| 229 | * |
||
| 230 | * @param string $manga_id |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | public function get_manga($manga_id) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Map transformed anime data to be organized by reading status |
||
| 241 | * |
||
| 242 | * @param array $data |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | private function map_by_status($data) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Combine the two manga lists into one |
||
| 276 | * @param array $raw_data |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | private function zipper_lists($raw_data) |
||
| 283 | } |
||
| 284 | // End of MangaModel.php |
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: