Passed
Pull Request — master (#74)
by Anatoly
02:08
created

InvalidArgumentException   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 22
c 1
b 0
f 0
dl 0
loc 136
ccs 36
cts 36
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getInvalidValue() 0 3 1
A throwIfEmptyArray() 0 5 3
A throwIfNotArray() 0 5 2
A throwIfNotString() 0 5 2
A throwIfEmptyString() 0 5 3
A throwIfEmpty() 0 5 3
A throwIfNotInteger() 0 5 2
A throwIfNotImplemented() 0 5 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router\Exception;
13
14
/**
15
 * Import functions
16
 */
17
use function is_array;
18
use function is_int;
19
use function is_string;
20
use function is_subclass_of;
21
22
/**
23
 * InvalidArgumentException
24
 *
25
 * @since 2.9.0
26
 */
27
class InvalidArgumentException extends Exception
28
{
29
30
    /**
31
     * Gets an invalid value
32
     *
33
     * @return mixed
34
     */
35 2
    public function getInvalidValue()
36
    {
37 2
        return $this->fromContext('invalidValue');
38
    }
39
40
    /**
41
     * Throws the exception if the given operand isn't an array
42
     *
43
     * @param mixed $operand
44
     * @param string $message
45
     *
46
     * @return void
47
     * @throws static
48
     */
49 52
    public static function throwIfNotArray($operand, string $message) : void
50
    {
51 52
        if (!is_array($operand)) {
52 26
            throw new static($message, [
53 26
                'invalidValue' => $operand,
54
            ]);
55
        }
56 26
    }
57
58
    /**
59
     * Throws the exception if the given operand isn't an integer
60
     *
61
     * @param mixed $operand
62
     * @param string $message
63
     *
64
     * @return void
65
     * @throws static
66
     */
67 13
    public static function throwIfNotInteger($operand, string $message) : void
68
    {
69 13
        if (!is_int($operand)) {
70 9
            throw new static($message, [
71 9
                'invalidValue' => $operand,
72
            ]);
73
        }
74 4
    }
75
76
    /**
77
     * Throws the exception if the given operand isn't a string
78
     *
79
     * @param mixed $operand
80
     * @param string $message
81
     *
82
     * @return void
83
     * @throws static
84
     */
85 20
    public static function throwIfNotString($operand, string $message) : void
86
    {
87 20
        if (!is_string($operand)) {
88 16
            throw new static($message, [
89 16
                'invalidValue' => $operand,
90
            ]);
91
        }
92 4
    }
93
94
    /**
95
     * Throws the exception if the given operand doesn't implement the given class
96
     *
97
     * @param mixed $operand
98
     * @param string $className
99
     * @param string $message
100
     *
101
     * @return void
102
     * @throws static
103
     */
104 32
    public static function throwIfNotImplemented($operand, string $className, string $message) : void
105
    {
106 32
        if (!is_subclass_of($operand, $className)) {
107 25
            throw new static($message, [
108 25
                'invalidValue' => $operand,
109
            ]);
110
        }
111 8
    }
112
113
    /**
114
     * Throws the exception if the given operand is an empty
115
     *
116
     * @param mixed $operand
117
     * @param string $message
118
     *
119
     * @return void
120
     * @throws static
121
     */
122 40
    public static function throwIfEmpty($operand, string $message) : void
123
    {
124 40
        if ('' === $operand || [] === $operand) {
125 4
            throw new static($message, [
126 4
                'invalidValue' => $operand,
127
            ]);
128
        }
129 39
    }
130
131
    /**
132
     * Throws the exception if the given operand isn't an array or empty
133
     *
134
     * @param mixed $operand
135
     * @param string $message
136
     *
137
     * @return void
138
     * @throws static
139
     */
140 102
    public static function throwIfEmptyArray($operand, string $message) : void
141
    {
142 102
        if ([] === $operand || !is_array($operand)) {
143 13
            throw new static($message, [
144 13
                'invalidValue' => $operand,
145
            ]);
146
        }
147 89
    }
148
149
    /**
150
     * Throws the exception if the given operand isn't a string or empty
151
     *
152
     * @param mixed $operand
153
     * @param string $message
154
     *
155
     * @return void
156
     * @throws static
157
     */
158 174
    public static function throwIfEmptyString($operand, string $message) : void
159
    {
160 174
        if ('' === $operand || !is_string($operand)) {
161 73
            throw new static($message, [
162 73
                'invalidValue' => $operand,
163
            ]);
164
        }
165 151
    }
166
}
167