gwCoreException::getInfo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Gwa\Exception;
4
5
/**
6
 * The core exception class, to be extended by all other Exceptions.
7
 */
8
class gwCoreException extends \Exception
9
{
10
    /**
11
     * A logic error.
12
     *
13
     * @var string
14
     */
15
    const ERR_LOGIC_ERROR = 'gwCoreException::logic_error';
16
17
    /**
18
     * Method does not exist on object
19
     * info = name of method called.
20
     *
21
     * @var string
22
     */
23
    const ERR_BAD_METHOD_CALL = 'gwCoreException::bad_method_call';
24
25
    /**
26
     * Argument passed to a method is invalid, and is not one of the following:
27
     * - invalid type
28
     * - out of bounds
29
     * - out of range.
30
     *
31
     * @var string
32
     */
33
    const ERR_INVALID_ARGUMENT = 'gwCoreException::invalid_argument';
34
35
    /**
36
     * Required argument is missing. Possibly within an array passed as an
37
     * argument.
38
     *
39
     * @var string
40
     */
41
    const ERR_MISSING_ARGUMENT = 'gwCoreException::missing_argument';
42
43
    /**
44
     * Argument passed to a method has the wrong type.
45
     *
46
     * @var string
47
     */
48
    const ERR_INVALID_TYPE = 'gwCoreException::invalid_type';
49
50
    /**
51
     * Argument passed is out of the allowed numeric range.
52
     *
53
     * @var string
54
     */
55
    const ERR_OUT_OF_RANGE = 'gwCoreException::out_of_range';
56
57
    /**
58
     * Argument passed is out of the allowed set of possible options
59
     * Could be used
60
     * - when an array key does not exist
61
     * - when an property does not exist on an object
62
     * - when a value is not within a specific range.
63
     *
64
     * @var string
65
     */
66
    const ERR_OUT_OF_BOUNDS = 'gwCoreException::out_of_bounds';
67
68
    /**
69
     * Thrown when trying to create an entry with a key that should be unique.
70
     * E.g. insert into database, create element in array.
71
     *
72
     * @var string
73
     */
74
    const ERR_KEY_EXISTS = 'gwCoreException::key_exists';
75
76
    /**
77
     * Thrown when trying to retrieve an entry with a key and no entry exists.
78
     * E.g. read from database, read from array.
79
     *
80
     * @var string
81
     */
82
    const ERR_KEY_NOT_EXIST = 'gwCoreException::key_not_exist';
83
84
    /**
85
     * Thrown by a method when caller does not have sufficient privileges.
86
     * E.g. database entry exists, but is blocked, inactive, unconfirmed, etc.
87
     *
88
     * @var string
89
     */
90
    const ERR_ACCESS_DENIED = 'gwCoreException::access_denied';
91
92
    /**
93
     * Value returned by method or function was unexpected.
94
     *
95
     * @var string
96
     */
97
    const ERR_UNEXPECTED_VALUE = 'gwCoreException::unexpected_value';
98
99
    /**
100
     * Container is full and someone is trying to add something to it.
101
     *
102
     * @var string
103
     */
104
    const ERR_OVERFLOW = 'gwCoreException::overflow';
105
106
    /**
107
     * Container is empty and someone is trying to remove something from it.
108
     *
109
     * @var string
110
     */
111
    const ERR_UNDERFLOW = 'gwCoreException::underflow';
112
113
    /**
114
     * @var gwCoreExceptionInfo
115
     */
116
    protected $info;
117
118
    /**
119
     * Constructor.
120
     *
121
     * @param string                          $message
122
     * @param gwCoreExceptionInfo|string|null $info
123
     * @param int                             $code
124
     * @param \Exception|null                 $previous
125
     */
126
    public function __construct($message, $info = null, $code = 0, \Exception $previous = null)
127
    {
128
        if (!is_a($info, 'gwCoreExceptionInfo')) {
129
            $info = new gwCoreExceptionInfo($info);
0 ignored issues
show
Bug introduced by
It seems like $info defined by new \Gwa\Exception\gwCoreExceptionInfo($info) on line 129 can also be of type object<Gwa\Exception\gwCoreExceptionInfo> or string; however, Gwa\Exception\gwCoreExceptionInfo::__construct() does only seem to accept object<Gwa\Exception\gwCoreException>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
130
        }
131
        $info->setException($this);
0 ignored issues
show
Bug introduced by
It seems like $info is not always an object, but can also be of type string|null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
132
        $this->info = $info;
0 ignored issues
show
Documentation Bug introduced by
It seems like $info can also be of type string. However, the property $info is declared as type object<Gwa\Exception\gwCoreExceptionInfo>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
133
        parent::__construct($message, $code, $previous);
134
    }
135
136
    /**
137
     * @return string representation of this Exception
138
     */
139
    public function __toString()
140
    {
141
        return __CLASS__ . " [$this->code]: $this->message | ".$this->info->fetch();
142
    }
143
144
    /**
145
     * @param string $key
146
     * @param string $value
147
     */
148
    public function __set($key, $value)
149
    {
150
        switch ($key) {
151
            case 'info':
152
                $this->info = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type string is incompatible with the declared type object<Gwa\Exception\gwCoreExceptionInfo> of property $info.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
153
                break;
154
        }
155
    }
156
157
    /**
158
     * @param string $key
159
     *
160
     * @return mixed
161
     */
162
    public function __get($key)
163
    {
164
        switch ($key) {
165
            case 'info':
166
                return $this->info;
167
        }
168
169
        return null;
170
    }
171
172
    /**
173
     * @return gwCoreExceptionInfo
174
     */
175
    public function getInfo()
176
    {
177
        return $this->info;
178
    }
179
}
180