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.

Type::check()   C
last analyzed

Complexity

Conditions 12
Paths 11

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 17
nc 11
nop 1
dl 0
loc 24
rs 6.9666
c 1
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 Make it implement an interface.
14
 * @todo Rely on an implementation (Scalar).
15
 */
16
class Type
17
{
18
    /** @var string The type identifier (class name, scalar type, interface…) */
19
    private $type;
20
21
    public function __construct(string $type)
22
    {
23
        $this->type = $type;
24
        if ($this->isArrayType()
25
        || ($this->isStringType())
26
        || ($this->isIntegerType())
27
        || ($this->isBoolType())
28
        || ($this->isClassOrInterfaceName())) {
29
        } else {
30
            throw new InvalidArgumentException();
31
        }
32
    }
33
34
    /**
35
     * @return bool Whether the type is the array scalar type.
36
     */
37
    public function isArrayType(): bool
38
    {
39
        return Scalar::_ARRAY === $this->type;
40
    }
41
42
    /**
43
     * @return bool Whether the type is the boolean scalar type.
44
     */
45
    public function isBoolType(): bool
46
    {
47
        return Scalar::_BOOL === $this->type;
48
    }
49
50
    /**
51
     * @return bool Whether the type is the FQCN of a class or of an interface.
52
     */
53
    public function isClassOrInterfaceName(): bool
54
    {
55
        return class_exists($this->type) || interface_exists($this->type);
56
    }
57
58
    /**
59
     * @return bool Whether the type is the scalar integer type.
60
     */
61
    public function isIntegerType(): bool
62
    {
63
        return Scalar::_INT === $this->type;
64
    }
65
66
    /**
67
     * @return bool Whether the type is the string type.
68
     */
69
    public function isStringType(): bool
70
    {
71
        return Scalar::_STR === $this->type;
72
    }
73
74
    /**
75
     * Throws an exception if the value is not of the expected type.
76
     * @param mixed $value The value to check.
77
     * @throws InvalidArgumentException if the value is not of the specified
78
     * type.
79
     * @todo Use is_a or instanceof instead?
80
     * @todo Long switch-like statement.
81
     */
82
    public function check($value): void
83
    {
84
        if ($this->isArrayType($this->type)) {
0 ignored issues
show
Unused Code introduced by
The call to LM\Common\Type\Type::isArrayType() has too many arguments starting with $this->type. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        if ($this->/** @scrutinizer ignore-call */ isArrayType($this->type)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
85
            if (!is_array($value)) {
86
                throw new InvalidArgumentException();
87
            }
88
        } elseif ($this->isStringType($this->type)) {
0 ignored issues
show
Unused Code introduced by
The call to LM\Common\Type\Type::isStringType() has too many arguments starting with $this->type. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        } elseif ($this->/** @scrutinizer ignore-call */ isStringType($this->type)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
89
            if (!is_string($value)) {
90
                throw new InvalidArgumentException();
91
            }
92
        } elseif ($this->isIntegerType($this->type)) {
0 ignored issues
show
Unused Code introduced by
The call to LM\Common\Type\Type::isIntegerType() has too many arguments starting with $this->type. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        } elseif ($this->/** @scrutinizer ignore-call */ isIntegerType($this->type)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
93
            if (!is_int($value)) {
94
                throw new InvalidArgumentException();
95
            }
96
        } elseif ($this->isBoolType($this->type)) {
0 ignored issues
show
Unused Code introduced by
The call to LM\Common\Type\Type::isBoolType() has too many arguments starting with $this->type. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        } elseif ($this->/** @scrutinizer ignore-call */ isBoolType($this->type)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
97
            if (!is_bool($value)) {
98
                throw new InvalidArgumentException();
99
            }
100
        } elseif ($this->isClassOrInterfaceName($this->type)) {
0 ignored issues
show
Unused Code introduced by
The call to LM\Common\Type\Type::isClassOrInterfaceName() has too many arguments starting with $this->type. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
        } elseif ($this->/** @scrutinizer ignore-call */ isClassOrInterfaceName($this->type)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
101
            if (!is_object($value) || !is_a($value, $this->type)) {
102
                throw new InvalidArgumentException();
103
            }
104
        } else {
105
            throw new InvalidArgumentException();
106
        }
107
    }
108
}
109