Passed
Push — master ( 66268d...ed54af )
by IRFA
01:51
created

Ongkir   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Importance

Changes 11
Bugs 1 Features 0
Metric Value
wmc 43
eloc 113
c 11
b 1
f 0
dl 0
loc 218
rs 8.96

18 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 12 2
A cachingProvince() 0 3 1
A find() 0 10 2
A costDetails() 0 5 1
A testConnection() 0 5 1
A courier() 0 5 1
A setupConfig() 0 5 1
B subDistrictData() 0 26 8
A subDistrict() 0 6 1
A exceptionCache() 0 5 1
B cityData() 0 26 8
A city() 0 6 1
A internationalOriginData() 0 7 1
A cachingSubDistrict() 0 17 3
B provinceData() 0 26 8
A cachingCity() 0 3 1
A internationalOrigin() 0 6 1
A province() 0 6 1

How to fix   Complexity   

Complex Class

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
2
3
/*
4
    Author: Irfa Ardiansyah <[email protected]>
5
*/
6
7
namespace Irfa\RajaOngkir\Ongkir;
8
9
use Exception;
10
use Irfa\RajaOngkir\Caching\ROCache;
11
use Irfa\RajaOngkir\Ongkir\Func\Api;
12
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;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
31
        }
32
    }
33
34
    public static function get()
35
    {
36
        self::$arr = null; //Clear parameter
37
        if (empty(self::$return)) {
38
            throw new Exception('Data is not defined.');
39
40
            return false;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
41
        }
42
        $ret = self::$return;
43
        self::$return = null;
44
45
        return $ret;
46
    }
47
48
    public static function cachingProvince()
49
    {
50
        self::cacheProvince();
51
    }
52
53
     public static function cachingSubDistrict()
54
    {
55
        if(strtolower(config('irfa.rajaongkir.account_type')) == "starter")
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        if(strtolower(/** @scrutinizer ignore-call */ config('irfa.rajaongkir.account_type')) == "starter")
Loading history...
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()
79
    {
80
        self::$return = self::get_cost_details(self::$arr);
81
82
        return new static();
83
    } 
84
    public static function testConnection()
85
    {
86
        self::$return = self::test_connection(self::$arr);
87
88
        return new static();
89
    }
90
91
    public static function courier()
92
    {
93
        self::$return = self::get_courier(self::$arr);
94
95
        return new static();
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()
130
    {
131
        self::$cacheType = strtolower(config('irfa.rajaongkir.cache_type'));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
        self::$cacheType = strtolower(/** @scrutinizer ignore-call */ config('irfa.rajaongkir.cache_type'));
Loading history...
132
        self::$province = config('irfa.rajaongkir.province_table');
133
        self::$city = config('irfa.rajaongkir.city_table');
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.
Loading history...
162
    }
163
    private static function subDistrictData()
164
    {
165
        if (function_exists('config') && function_exists('app')) {
166
            self::setupConfig();
167
            $cache_type = self::$cacheType;
168
            if ($cache_type == 'database') {
169
                if (ROCache::checkProv()) {
170
                    if (count(ROCache::getSubdistrict(self::$arr)) > 0) {
171
                        $ret = ROCache::getSubdistrict(self::$arr);
172
                    } else {
173
                        $ret = self::getSubdistrict(self::$arr);
174
                    }
175
                }
176
            } elseif ($cache_type == 'file') {
177
                $ret = ROCache::cacheFile(self::$province, self::$arr);
178
                if ($ret == null) {
179
                    self::exceptionCache();
180
                }
181
            } else {
182
                $ret = self::getSubdistrict(self::$arr);
183
            }
184
        } else {
185
            $ret = self::getSubdistrict(self::$arr);
186
        }
187
188
        return $ret;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.
Loading history...
189
    }
190
    private static function internationalOriginData()
191
    {
192
        
193
            $ret = self::get_international_origin(self::$arr);
0 ignored issues
show
Bug introduced by
The method get_international_origin() does not exist on Irfa\RajaOngkir\Ongkir\Ongkir. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
            /** @scrutinizer ignore-call */ 
194
            $ret = self::get_international_origin(self::$arr);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.
Loading history...
224
    }
225
226
    private static function exceptionCache()
227
    {
228
        throw new Exception('Cache file is empty. Try php artisan raja-ongkir:cache');
229
230
        return false;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
231
    }
232
}
233