| Total Complexity | 40 |
| Total Lines | 213 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Helper 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.
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 Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Helper |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Convert return result into easy-to-read result. |
||
| 12 | * |
||
| 13 | * @param string|array $response |
||
| 14 | * |
||
| 15 | * @return string|array |
||
| 16 | */ |
||
| 17 | public static function toResponse($response) |
||
| 18 | { |
||
| 19 | switch ($response) { |
||
| 20 | case 400: |
||
| 21 | return 'Search query needs at least 3 letters'; |
||
| 22 | case 403: |
||
| 23 | return 'Private user list'; |
||
| 24 | case 404: |
||
| 25 | return 'Page not found'; |
||
| 26 | default: |
||
| 27 | return $response; |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Convert return result into http response. |
||
| 33 | * |
||
| 34 | * @param string|array $response |
||
| 35 | * |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | public function response($response) |
||
| 39 | { |
||
| 40 | $result = []; |
||
| 41 | if (is_numeric($response)) { |
||
| 42 | header('HTTP/1.1 '.$response); |
||
| 43 | $result['status'] = $response; |
||
| 44 | $result['status_message'] = self::toResponse($response); |
||
| 45 | $result['data'] = []; |
||
| 46 | } else { |
||
| 47 | header('HTTP/1.1 '. 200); |
||
| 48 | $result['status'] = 200; |
||
| 49 | $result['status_message'] = 'Success'; |
||
| 50 | $result['data'] = self::superEncode($response); |
||
| 51 | } |
||
| 52 | |||
| 53 | $json_response = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); |
||
| 54 | $json_response = str_replace('\\\\', '', $json_response); |
||
| 55 | |||
| 56 | return $json_response; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Convert characters to UTF-8. |
||
| 61 | * |
||
| 62 | * @param array $array |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | private function superEncode($array) |
||
| 67 | { |
||
| 68 | if (is_array($array) && !empty($array)) { |
||
| 69 | foreach ($array as $key => $value) { |
||
| 70 | if (is_array($value)) { |
||
| 71 | $array[$key] = self::superEncode($value); |
||
| 72 | } else { |
||
| 73 | $array[$key] = mb_convert_encoding($value, 'UTF-8', 'UTF-8'); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return $array; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get top anime code. |
||
| 83 | * |
||
| 84 | * @param string|int $type |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public static function getTopAnimeType($type) |
||
| 89 | { |
||
| 90 | $converted_type = ''; |
||
| 91 | switch ($type) { |
||
| 92 | case '0': |
||
| 93 | $converted_type = ''; |
||
| 94 | break; |
||
| 95 | case '1': |
||
| 96 | $converted_type = 'airing'; |
||
| 97 | break; |
||
| 98 | case '2': |
||
| 99 | $converted_type = 'upcoming'; |
||
| 100 | break; |
||
| 101 | case '3': |
||
| 102 | $converted_type = 'tv'; |
||
| 103 | break; |
||
| 104 | case '4': |
||
| 105 | $converted_type = 'movie'; |
||
| 106 | break; |
||
| 107 | case '5': |
||
| 108 | $converted_type = 'ova'; |
||
| 109 | break; |
||
| 110 | case '6': |
||
| 111 | $converted_type = 'special'; |
||
| 112 | break; |
||
| 113 | case '7': |
||
| 114 | $converted_type = 'bypopularity'; |
||
| 115 | break; |
||
| 116 | case '8': |
||
| 117 | $converted_type = 'favorite'; |
||
| 118 | break; |
||
| 119 | default: |
||
| 120 | $converted_type = ''; |
||
| 121 | } |
||
| 122 | |||
| 123 | return $converted_type; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get top manga code. |
||
| 128 | * |
||
| 129 | * @param string|int $type |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public static function getTopMangaType($type) |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get current season. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public static function getCurrentSeason() |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Clean image URL. |
||
| 205 | * |
||
| 206 | * @param string $str |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public static function imageUrlCleaner($str) |
||
| 221 | } |
||
| 222 | } |