TypeAssert::assertValue()   D
last analyzed

Complexity

Conditions 28
Paths 9

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 28.4014

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 28
eloc 25
c 2
b 0
f 0
nc 9
nop 4
dl 0
loc 34
ccs 23
cts 25
cp 0.92
crap 28.4014
rs 4.1666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Teto\Object;
4
5
/**
6
 * Argument type assertion methods
7
 *
8
 * @author    USAMI Kenta <[email protected]>
9
 * @copyright 2016 Baguette HQ
10
 * @license   http://www.apache.org/licenses/LICENSE-2.0
11
 */
12
trait TypeAssert
13
{
14
    /**
15
     * Assert a value is expected typed
16
     *
17
     * @param string $expected_type
18
     * @param string $name  Variable name (for error message)
19
     * @param mixed  $value Received value
20
     * @param bool   $is_nullable
21
     * @throws \LogicException
22
     * @throws \InvalidArgumentException
23
     * @suppress PhanUndeclaredStaticProperty
24
     */
25 49
    protected static function assertValue($expected_type, $name, $value, $is_nullable)
26
    {
27 49
        if ($is_nullable && $value === null) {
28 3
            return; // through
29 47
        } elseif ($expected_type === 'mixed') {
30
            return; // through
31 47
        } elseif ($expected_type === 'enum') {
32 2
            if (!isset(self::$enum_values) || !isset(self::$enum_values[$name])) {
33
                new \LogicException("Doesn't set self::\$enum_values[$name]");
34
            }
35
36 2
            if (in_array($value, self::$enum_values[$name], true)) {
37 1
                return;
38
            }
39
40 1
            $expects = '['. implode(', ', self::$enum_values[$name]) . ']';
41 1
            throw new \InvalidArgumentException(self::message($expects, $value, $name));
42
        } elseif (
43 45
               ($expected_type === 'int'      && is_int($value))
44 32
            || ($expected_type === 'string'   && is_string($value))
45 27
            || ($expected_type === 'float'    && is_float($value))
46 25
            || ($expected_type === 'array'    && is_array($value))
47 25
            || ($expected_type === 'bool'     && is_bool($value))
48 23
            || ($expected_type === 'object'   && is_object($value))
49 22
            || ($expected_type === 'scalar'   && is_scalar($value))
50 22
            || ($expected_type === 'callable' && is_callable($value))
51 45
            || ($expected_type === 'resource' && is_resource($value))
52
        ) {
53 27
            return;
54 19
        } elseif (is_object($value) && $value instanceof $expected_type) {
55 2
            return;
56
        }
57
58 17
        throw new \InvalidArgumentException(self::message($expected_type, $value, $name));
59
    }
60
61
    /**
62
     * Assert a value is integer
63
     *
64
     * @param mixed  $value Received value
65
     * @param string $name  Variable name (for error message)
66
     * @throws \InvalidArgumentException
67
     */
68 4
    protected static function assertInt($value, $name = null)
69
    {
70 4
        if (!is_int($value)) {
71 2
            throw new \InvalidArgumentException(self::message('int', $value, $name));
72
        }
73 2
    }
74
75
    /**
76
     * Assert a value is string
77
     *
78
     * @param mixed  $value Received value
79
     * @param string $name  Variable name (for error message)
80
     * @throws \InvalidArgumentException
81
     */
82
    protected static function assertString($value, $name = null)
83
    {
84
        if (!is_string($value)) {
85
            throw new \InvalidArgumentException(self::message('string', $value, $name));
86
        }
87
    }
88
89
    /**
90
     * Assert a value is array or array like object (that inplements ArrayAccess)
91
     *
92
     * @param mixed  $value Received value
93
     * @param string $name  Variable name (for error message)
94
     * @throws \InvalidArgumentException
95
     * @see    http://php.net/manual/class.arrayaccess.php
96
     */
97 13
    protected static function assertArrayOrObject($value, $name = null)
98
    {
99 13
        if (!is_array($value) && !$value instanceof \ArrayAccess) {
100 1
            throw new \InvalidArgumentException(self::message('array or ArrayAccess', $value, $name));
101
        }
102 12
    }
103
104
    /**
105
     * Assert a value is instance of $class
106
     *
107
     * @param mixed  $value Received value
108
     * @param string $class Class name
109
     * @param string $name  Variable name (for error message)
110
     * @throws \InvalidArgumentException
111
     */
112
    protected static function assertInstanceOf($value, $class, $name = null)
113
    {
114
        if (!$value instanceof $class) {
115
            throw new \InvalidArgumentException(self::message($class, $value, $name));
116
        }
117
    }
118
119
    /**
120
     * @param  string      $expected_type
121
     * @param  mixed       $value
122
     * @param  string|null $name
123
     * @return string
124
     */
125 21
    private static function message($expected_type, $value, $name)
126
    {
127 21
        $type = is_object($value) ? get_class($value) : gettype($value);
128 21
        $vars = ($name === null) ? $type : "$name as $type";
129
130 21
        return "got \$$vars (expects $expected_type)";
131
    }
132
}
133