States::isCached()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace DigitalCanvas\Options;
2
3
use RuntimeException;
4
5
/**
6
 * States Options Class
7
 *
8
 * @package    Options
9
 */
10
class States
11
{
12
    /**
13
     * xml data file
14
     * @var string|null
15
     */
16
    private static $file;
17
18
    /**
19
     * States data
20
     * @var array|null
21
     */
22
    private static $states;
23
24
    /**
25
     * Sets the xml file to use to load states
26
     *
27
     * @param string $file
28
     *
29
     * @return void
30
     * @throws RuntimeException
31
     */
32 3
    public static function setXML($file = null)
33
    {
34 3
        if (is_null($file)) {
35 1
            $file = __DIR__ . '/states.xml';
36
        }
37 3
        if (!is_file($file)) {
38 1
            throw new RuntimeException('States XML file does not exist.');
39
        }
40 2
        self::$file = realpath($file);
41 2
        self::$states = null;
42 2
    }
43
44
    /**
45
     * Loads the states from the xml file.
46
     *
47
     * @return void
48
     */
49 10
    private static function loadStates()
50
    {
51 10
        if (is_null(self::$file)) {
52 1
            self::setXML();
53
        }
54
        // Load xml file
55 10
        $xml = simplexml_load_file(self::$file);
56 10
        $states = array();
57
        // loop through states
58 10
        foreach ($xml as $state) {
59 10
            $states[] = array(
60 10
                'abbreviation' => (string)$state->abbreviation,
61 10
                'name'         => (string)$state->name,
62 10
                'country'      => (string)$state->country,
63 10
                'countryname'  => (string)$state->countryname
64
            );
65
        }
66
        // Cache states
67 10
        self::$states = $states;
68
        // Clear xml instance
69 10
        unset($xml, $states, $state);
70 10
    }
71
72
    /**
73
     * Clears the states from the cache.
74
     *
75
     * @return void
76
     */
77 11
    public static function clearCache()
78
    {
79 11
        self::$states = null;
80 11
    }
81
82
    /**
83
     * Checks if data has been cached
84
     *
85
     * @return bool
86
     */
87 1
    public static function isCached()
88
    {
89 1
        return !is_null(self::$states);
90
    }
91
92
    /**
93
     * Returns array of states
94
     *
95
     * @param array $allowed_countries Array of allowed countries.<br> If null all are returned.
96
     *
97
     * @return array
98
     */
99 2
    public static function getArray($allowed_countries = array('US'))
100
    {
101
        // Load States if they are not yet loaded.
102 2
        if (is_null(self::$states)) {
103 2
            self::loadStates();
104
        }
105 2
        $states = self::$states;
106 2
        foreach ($states as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $states of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
107 2
            if (!empty($allowed_countries) && !in_array($value['country'], $allowed_countries)) {
108 2
                unset($states[$key]);
109
            }
110
        }
111 2
        return $states;
112
    }
113
114
    /**
115
     * Returns states as key=>value pair array
116
     *
117
     * @param array $allowed_countries Array of allowed countries.<br> If null all are returned.
118
     *
119
     * @return array
120
     */
121 4
    public static function getPairs($allowed_countries = array('US'))
122
    {
123
        // Load States if they are not yet loaded.
124 4
        if (is_null(self::$states)) {
125 4
            self::loadStates();
126
        }
127 4
        $states = array();
128 4
        foreach (self::$states as $state) {
0 ignored issues
show
Bug introduced by
The expression self::$states of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
129 4
            if (!$allowed_countries || in_array($state['country'], $allowed_countries)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $allowed_countries of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
130 4
                $states[$state['abbreviation']] = $state['name'];
131
            }
132
        }
133 4
        return $states;
134
    }
135
136
    /**
137
     * Returns states as key=>value pair array grouped by country
138
     *
139
     * @param array $allowed_countries Array of allowed countries.<br> If null all are returned.
140
     *
141
     * @return array
142
     */
143 1
    public static function getMultiPairs($allowed_countries = array('US', 'CA'))
144
    {
145
        // Load States if they are not yet loaded.
146 1
        if (is_null(self::$states)) {
147 1
            self::loadStates();
148
        }
149 1
        $states = array();
150 1
        foreach (self::$states as $state) {
0 ignored issues
show
Bug introduced by
The expression self::$states of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
151 1
            if (empty($allowed_countries) || in_array($state['country'], $allowed_countries)) {
152 1
                if (!array_key_exists($state['countryname'], $states)) {
153 1
                    $states[$state['countryname']] = array();
154
                }
155 1
                $states[$state['countryname']][$state['abbreviation']] = $state['name'];
156
            }
157
        }
158 1
        return $states;
159
    }
160
161
    /**
162
     * Returns a single state by abbreviation
163
     *
164
     * @param string $abbreviation
165
     * @param string $country
166
     * @param bool   $name_only
167
     *
168
     * @return string|array|null
169
     */
170 3
    public static function getState($abbreviation, $country = null, $name_only = true)
171
    {
172
        // Load States if they are not yet loaded.
173 3
        if (is_null(self::$states)) {
174 3
            self::loadStates();
175
        }
176 3
        foreach (self::$states as $state) {
0 ignored issues
show
Bug introduced by
The expression self::$states of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
177 3
            if ($abbreviation == $state['abbreviation'] && (!$country || $country == $state['country'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $country of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
178 3
                return ($name_only) ? $state['name'] : $state;
179
            }
180
        }
181 1
        return null;
182
    }
183
}
184