GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TypeCheckerTrait::checkType()   C
last analyzed

Complexity

Conditions 12
Paths 11

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 12
eloc 17
nc 11
nop 2
dl 0
loc 24
rs 6.9666
c 3
b 0
f 0

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
declare(strict_types=1);
4
5
namespace LM\Common\Type;
6
7
use InvalidArgumentException;
8
use LM\Common\Enum\Scalar;
9
10
/**
11
 * Trait for checking types.
12
 *
13
 * @todo Turn it into a class instead?
14
 * @deprecated
15
 */
16
trait TypeCheckerTrait
17
{
18
    /**
19
     * @param string $type The type to check.
20
     * @return bool Whether the type is the array scalar type.
21
     */
22
    public function isArrayType(string $type): bool
23
    {
24
        return Scalar::_ARRAY === $type;
25
    }
26
27
    /**
28
     * @param string $type The type to check.
29
     * @return bool Whether the type is the boolean scalar type.
30
     */
31
    public function isBoolType(string $type): bool
32
    {
33
        return Scalar::_BOOL === $type;
34
    }
35
36
    /**
37
     * @param string $type The type to check.
38
     * @return bool Whether the type is the FQCN of a class or of an interface.
39
     */
40
    public function isClassOrInterfaceName(string $type): bool
41
    {
42
        return class_exists($type) || interface_exists($type);
43
    }
44
45
    /**
46
     * @param string $type The type to check.
47
     * @return bool Whether the type is the scalar integer type.
48
     */
49
    public function isIntegerType(string $type): bool
50
    {
51
        return Scalar::_INT === $type;
52
    }
53
54
    /**
55
     * @param string $type The type to check.
56
     * @return bool Whether the type is the string type.
57
     */
58
    public function isStringType(string $type): bool
59
    {
60
        return Scalar::_STR === $type;
61
    }
62
63
    /**
64
     * Throws an exception if the value is not of the expected type.
65
     * @param mixed $value The value to check.
66
     * @param string $type The type to check against.
67
     * @throws InvalidArgumentException if the value is not of the specified
68
     * type.
69
     * @todo Use is_a or instanceof instead?
70
     */
71
    public function checkType($value, string $type): void
72
    {
73
        if ($this->isArrayType($type)) {
74
            if (!is_array($value)) {
75
                throw new InvalidArgumentException();
76
            }
77
        } elseif ($this->isStringType($type)) {
78
            if (!is_string($value)) {
79
                throw new InvalidArgumentException();
80
            }
81
        } elseif ($this->isIntegerType($type)) {
82
            if (!is_int($value)) {
83
                throw new InvalidArgumentException();
84
            }
85
        } elseif ($this->isBoolType($type)) {
86
            if (!is_bool($value)) {
87
                throw new InvalidArgumentException();
88
            }
89
        } elseif ($this->isClassOrInterfaceName($type)) {
90
            if (!is_object($value) || !is_a($value, $type)) {
91
                throw new InvalidArgumentException();
92
            }
93
        } else {
94
            throw new InvalidArgumentException();
95
        }
96
    }
97
}
98