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

MatchTester   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 111
Duplicated Lines 9.01 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 10
loc 111
ccs 24
cts 32
cp 0.75
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
C isMatch() 10 71 19

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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