Total Complexity | 43 |
Total Lines | 218 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 1 | Features | 0 |
Complex classes like Ongkir 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 Ongkir, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Ongkir extends Api |
||
14 | { |
||
15 | private static $arr; |
||
16 | private static $return; |
||
17 | private static $province; |
||
18 | private static $city; |
||
19 | private static $cacheType; |
||
20 | |||
21 | public static function find($arr) |
||
22 | { |
||
23 | if (is_array($arr)) { |
||
24 | self::$arr = $arr; |
||
25 | |||
26 | return new static(); |
||
27 | } else { |
||
28 | throw new Exception('Parameter must be an array.'); |
||
29 | |||
30 | return false; |
||
|
|||
31 | } |
||
32 | } |
||
33 | |||
34 | public static function get() |
||
46 | } |
||
47 | |||
48 | public static function cachingProvince() |
||
51 | } |
||
52 | |||
53 | public static function cachingSubDistrict() |
||
54 | { |
||
55 | if(strtolower(config('irfa.rajaongkir.account_type')) == "starter") |
||
56 | { |
||
57 | echo "Tidak dapat mengambil SubDistrict dikarenakan akun yg anda pakai tipe starter."; |
||
58 | return true; |
||
59 | } |
||
60 | $get = self::cityData(); |
||
61 | $count = count($get); |
||
62 | $i=0; |
||
63 | echo PHP_EOL."\033[42mThis may take longer, please wait.\033[0m"; |
||
64 | foreach($get as $city){ |
||
65 | $i++; |
||
66 | echo PHP_EOL."Remaining City\033[96m ".$i."/".$count."\033[0m"; |
||
67 | echo PHP_EOL."Get Subdistrict\033[96m ".$city->city_name."...\033[0m".PHP_EOL; |
||
68 | self::cacheSubDistrict(['city' => $city->city_id]); |
||
69 | echo PHP_EOL; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | public static function cachingCity() |
||
74 | { |
||
75 | self::cacheCity(); |
||
76 | } |
||
77 | |||
78 | public static function costDetails() |
||
83 | } |
||
84 | public static function testConnection() |
||
89 | } |
||
90 | |||
91 | public static function courier() |
||
96 | } |
||
97 | |||
98 | public static function province() |
||
99 | { |
||
100 | $ret = self::provinceData(); |
||
101 | self::$return = $ret; |
||
102 | |||
103 | return new static(); |
||
104 | } |
||
105 | |||
106 | public static function subDistrict() |
||
107 | { |
||
108 | $ret = self::subDistrictData(); |
||
109 | self::$return = $ret; |
||
110 | |||
111 | return new static(); |
||
112 | } |
||
113 | public static function internationalOrigin() |
||
114 | { |
||
115 | $ret = self::internationalOriginData(); |
||
116 | self::$return = $ret; |
||
117 | |||
118 | return new static(); |
||
119 | } |
||
120 | |||
121 | public static function city() |
||
122 | { |
||
123 | $ret = self::cityData(); |
||
124 | self::$return = $ret; |
||
125 | |||
126 | return new static(); |
||
127 | } |
||
128 | |||
129 | private static function setupConfig() |
||
134 | } |
||
135 | |||
136 | private static function provinceData() |
||
137 | { |
||
138 | if (function_exists('config') && function_exists('app')) { |
||
139 | self::setupConfig(); |
||
140 | $cache_type = self::$cacheType; |
||
141 | if ($cache_type == 'database') { |
||
142 | if (ROCache::checkProv()) { |
||
143 | if (count(ROCache::getProv(self::$arr)) > 0) { |
||
144 | $ret = ROCache::getProv(self::$arr); |
||
145 | } else { |
||
146 | $ret = self::get_province(self::$arr); |
||
147 | } |
||
148 | } |
||
149 | } elseif ($cache_type == 'file') { |
||
150 | $ret = ROCache::cacheFile(self::$province, self::$arr); |
||
151 | if ($ret == null) { |
||
152 | self::exceptionCache(); |
||
153 | } |
||
154 | } else { |
||
155 | $ret = self::get_province(self::$arr); |
||
156 | } |
||
157 | } else { |
||
158 | $ret = self::get_province(self::$arr); |
||
159 | } |
||
160 | |||
161 | return $ret; |
||
162 | } |
||
163 | private static function subDistrictData() |
||
189 | } |
||
190 | private static function internationalOriginData() |
||
191 | { |
||
192 | |||
193 | $ret = self::get_international_origin(self::$arr); |
||
194 | |||
195 | |||
196 | return $ret; |
||
197 | } |
||
198 | private static function cityData() |
||
199 | { |
||
200 | if (function_exists('config') && function_exists('app')) { |
||
201 | self::setupConfig(); |
||
202 | $cache_type = self::$cacheType; |
||
203 | if ($cache_type == 'database') { |
||
204 | if (ROCache::checkCity()) { |
||
205 | if (count(ROCache::getCity(self::$arr)) > 0) { |
||
206 | $ret = ROCache::getCity(self::$arr); |
||
207 | } else { |
||
208 | $ret = self::get_city(self::$arr); |
||
209 | } |
||
210 | } |
||
211 | } elseif ($cache_type == 'file') { |
||
212 | $ret = ROCache::cacheFile(self::$city, self::$arr); |
||
213 | if ($ret == null) { |
||
214 | self::exceptionCache(); |
||
215 | } |
||
216 | } else { |
||
217 | $ret = self::get_city(self::$arr); |
||
218 | } |
||
219 | } else { |
||
220 | $ret = self::get_city(self::$arr); |
||
221 | } |
||
222 | |||
223 | return $ret; |
||
224 | } |
||
225 | |||
226 | private static function exceptionCache() |
||
231 | } |
||
232 | } |
||
233 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.