Passed
Push — master ( 864271...d05f5e )
by IRFA
01:56
created

Ongkir::provinceData()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 18
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 24
rs 8.4444
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 cachingCity()
54
    {
55
        self::cacheCity();
56
    }
57
58
    public static function costDetails()
59
    {
60
        self::$return = self::get_cost_details(self::$arr);
61
62
        return new static();
63
    }
64
65
    public static function courier()
66
    {
67
        self::$return = self::get_courier(self::$arr);
68
69
        return new static();
70
    }
71
72
    public static function province()
73
    {
74
        $ret = self::provinceData();
75
        self::$return = $ret;
76
77
        return new static();
78
    }
79
80
    public static function city()
81
    {
82
        $ret = self::cityData();
83
        self::$return = $ret;
84
85
        return new static();
86
    }
87
    private static function setupConfig(){
88
        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

88
        self::$cacheType = strtolower(/** @scrutinizer ignore-call */ config('irfa.rajaongkir.cache_type'));
Loading history...
89
        self::$province = config('irfa.rajaongkir.province_table');
90
        self::$city = config('irfa.rajaongkir.city_table');
91
    }
92
    private static function provinceData(){
93
         if (function_exists('config') and function_exists('app')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
94
            self::setupConfig();
95
            $cache_type = self::$cacheType;
96
            if ($cache_type == 'database') {
97
                if (ROCache::checkProv()) {
98
                    if (count(ROCache::getProv(self::$arr)) > 0) {
99
                        $ret = ROCache::getProv(self::$arr);
100
                    } else {
101
                        $ret = self::get_province(self::$arr);
102
                    }
103
                }
104
            } elseif ($cache_type == 'file') {
105
                $ret = ROCache::cacheFile(self::$province, self::$arr);
106
                if ($ret == null) {
107
                    self::exceptionCache();
108
                }
109
            } else {
110
                $ret = self::get_province(self::$arr);
111
            }
112
        } else {
113
            $ret = self::get_province(self::$arr);
114
        }
115
        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...
116
    }
117
    private static function cityData(){
118
        if (function_exists('config') and function_exists('app')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
119
            self::setupConfig();
120
            $cache_type = self::$cacheType;
121
            if ($cache_type == 'database') {
122
                if (ROCache::checkCity()) {
123
                    if (count(ROCache::getCity(self::$arr)) > 0) {
124
                        $ret = ROCache::getCity(self::$arr);
125
                    } else {
126
                        $ret = self::get_city(self::$arr);
127
                    }
128
                }
129
            } elseif ($cache_type == 'file') {
130
                $ret = ROCache::cacheFile(self::$city, self::$arr);
131
                if ($ret == null) {
132
                    self::exceptionCache();
133
                }
134
            } else {
135
                $ret = self::get_city(self::$arr);
136
            }
137
        } else {
138
            $ret = self::get_city(self::$arr);
139
        }
140
141
        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...
142
    }
143
    private static function exceptionCache(){
144
        throw new Exception('Cache file is empty. Try php artisan raja-ongkir:cache');
145
146
        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...
147
148
    }
149
}
150