Issues (7)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/States.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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
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
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