Passed
Push — master ( 1b9949...a24caf )
by Chris
32s
created

MatchTester::isMatch()   C

Complexity

Conditions 19
Paths 17

Size

Total Lines 71
Code Lines 32

Duplication

Lines 10
Ratio 14.08 %

Code Coverage

Tests 24
CRAP Score 24.6406

Importance

Changes 0
Metric Value
dl 10
loc 71
ccs 24
cts 32
cp 0.75
rs 5.4997
c 0
b 0
f 0
cc 19
eloc 32
nc 17
nop 5
crap 24.6406

How to fix   Long Method    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 declare(strict_types = 1);
2
3
namespace DaveRandom\CallbackValidator;
4
5
final class MatchTester
6
{
7
    /**
8
     * Thou shalt not instantiate
9
     */
10
    private function __construct() { }
11
12
    /**
13
     * Lookup table of all built-in types
14
     */
15
    private static $builtInTypes = [
16
        BuiltInTypes::STRING   => true,
17
        BuiltInTypes::INT      => true,
18
        BuiltInTypes::FLOAT    => true,
19
        BuiltInTypes::BOOL     => true,
20
        BuiltInTypes::ARRAY    => true,
21
        BuiltInTypes::CALLABLE => true,
22
        BuiltInTypes::VOID     => true,
23
        BuiltInTypes::ITERABLE => true,
24
    ];
25
26
    /**
27
     * Lookup table of scalar types
28
     */
29
    private static $scalarTypes = [
30
        BuiltInTypes::STRING => true,
31
        BuiltInTypes::INT    => true,
32
        BuiltInTypes::FLOAT  => true,
33
        BuiltInTypes::BOOL   => true,
34
    ];
35
36
    /**
37
     * @param string|null $superTypeName
38
     * @param bool $superTypeNullable
39
     * @param string|null $subTypeName
40
     * @param bool $subTypeNullable
41
     * @param bool $weak
42
     * @return bool
43
     */
44 40
    public static function isMatch($superTypeName, $superTypeNullable, $subTypeName, $subTypeNullable, $weak)
45
    {
46
        // If the super type is unspecified, anything is a match
47 40
        if ($superTypeName === null) {
48
            return true;
49
        }
50
51
        // If the sub type is unspecified, nothing is a match
52 40
        if ($subTypeName === null) {
53
            return false;
54
        }
55
56 40
        $superTypeName = (string)$superTypeName;
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $superTypeName. This often makes code more readable.
Loading history...
57 40
        $subTypeName = (string)$subTypeName;
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $subTypeName. This often makes code more readable.
Loading history...
58
59
        // Sub type cannot be nullable unless the super type is as well
60 40
        if ($subTypeNullable && !$superTypeNullable) {
61 10
            return false;
62
        }
63
64
        // If the string is an exact match it's definitely acceptable
65 30
        if ($superTypeName === $subTypeName) {
66 6
            return true;
67
        }
68
69
        // Check iterable
70 30 View Code Duplication
        if ($superTypeName === BuiltInTypes::ITERABLE) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71 24
            return $subTypeName === BuiltInTypes::ARRAY
72 18
                || $subTypeName === \Traversable::class
73 24
                || \is_subclass_of($subTypeName, \Traversable::class);
74
        }
75
76
        // Check callable
77 12 View Code Duplication
        if ($superTypeName === BuiltInTypes::CALLABLE) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78 6
            return $subTypeName === \Closure::class
79 6
                || \method_exists($subTypeName, '__invoke')
80 6
                || \is_subclass_of($subTypeName, \Closure::class);
81
        }
82
83
        // If the super type is built-in, check whether casting rules can succeed
84 12
        if (isset(self::$builtInTypes[$superTypeName])) {
85
            // Fail immediately in strict mode
86 12
            if (!$weak) {
87 6
                return false;
88
            }
89
90
            // Nothing else satisfies array, callable, void or iterable
91 6
            if (!isset(self::$scalarTypes[$superTypeName])) {
92 3
                return false;
93
            }
94
95
            // Scalars can all cast to each other
96 3
            if (isset(self::$scalarTypes[$subTypeName])) {
97 3
                return true;
98
            }
99
100
            // Classes with __toString() satisfy string
101
            if ($superTypeName === BuiltInTypes::STRING && \method_exists($subTypeName, '__toString')) {
1 ignored issue
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $superTypeName ==...ypeName, '__toString');.
Loading history...
102
                return true;
103
            }
104
105
            return false;
106
        }
107
108
        // We now know the super type is not built-in and there's no string match, sub type must not be built-in
109
        if (isset(self::$builtInTypes[$subTypeName])) {
110
            return false;
111
        }
112
113
        return \is_subclass_of($subTypeName, $superTypeName);
114
    }
115
}
116