| Conditions | 20 |
| Paths | 18 |
| Total Lines | 72 |
| Code Lines | 32 |
| Lines | 10 |
| Ratio | 13.89 % |
| Tests | 27 |
| CRAP Score | 21.5244 |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types = 1); |
||
| 44 | 84 | public static function isMatch($superTypeName, $superTypeNullable, $subTypeName, $subTypeNullable, $weak) |
|
| 45 | { |
||
| 46 | // If the super type is unspecified, anything is a match |
||
| 47 | 84 | if ($superTypeName === null) { |
|
| 48 | return true; |
||
| 49 | } |
||
| 50 | |||
| 51 | // If the sub type is unspecified, nothing is a match |
||
| 52 | 84 | if ($subTypeName === null) { |
|
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | 84 | $superTypeName = (string)$superTypeName; |
|
|
1 ignored issue
–
show
|
|||
| 57 | 84 | $subTypeName = (string)$subTypeName; |
|
|
1 ignored issue
–
show
|
|||
| 58 | |||
| 59 | // Sub type cannot be nullable unless the super type is as well |
||
| 60 | 84 | if ($subTypeNullable && !$superTypeNullable) { |
|
| 61 | // nullable void doesn't really make sense but for completeness... |
||
| 62 | 24 | return $superTypeName === BuiltInTypes::VOID && $subTypeName === BuiltInTypes::VOID; |
|
| 63 | } |
||
| 64 | |||
| 65 | // If the string is an exact match it's definitely acceptable |
||
| 66 | 64 | if ($superTypeName === $subTypeName) { |
|
| 67 | 8 | return true; |
|
| 68 | } |
||
| 69 | |||
| 70 | // Check iterable |
||
| 71 | 62 | View Code Duplication | if ($superTypeName === BuiltInTypes::ITERABLE) { |
|
1 ignored issue
–
show
|
|||
| 72 | 24 | return $subTypeName === BuiltInTypes::ARRAY |
|
| 73 | 24 | || $subTypeName === \Traversable::class |
|
| 74 | 24 | || \is_subclass_of($subTypeName, \Traversable::class); |
|
| 75 | } |
||
| 76 | |||
| 77 | // Check callable |
||
| 78 | 44 | View Code Duplication | if ($superTypeName === BuiltInTypes::CALLABLE) { |
|
1 ignored issue
–
show
|
|||
| 79 | 24 | return $subTypeName === \Closure::class |
|
| 80 | 24 | || \method_exists($subTypeName, '__invoke') |
|
| 81 | 24 | || \is_subclass_of($subTypeName, \Closure::class); |
|
| 82 | } |
||
| 83 | |||
| 84 | // If the super type is built-in, check whether casting rules can succeed |
||
| 85 | 26 | if (isset(self::$builtInTypes[$superTypeName])) { |
|
| 86 | // Fail immediately in strict mode |
||
| 87 | 26 | if (!$weak) { |
|
| 88 | 13 | return false; |
|
| 89 | } |
||
| 90 | |||
| 91 | // Nothing else satisfies array, callable, void or iterable |
||
| 92 | 13 | if (!isset(self::$scalarTypes[$superTypeName])) { |
|
| 93 | 4 | return false; |
|
| 94 | } |
||
| 95 | |||
| 96 | // Scalars can all cast to each other |
||
| 97 | 9 | if (isset(self::$scalarTypes[$subTypeName])) { |
|
| 98 | 3 | return true; |
|
| 99 | } |
||
| 100 | |||
| 101 | // Classes with __toString() satisfy string |
||
| 102 | 6 | if ($superTypeName === BuiltInTypes::STRING && \method_exists($subTypeName, '__toString')) { |
|
|
1 ignored issue
–
show
|
|||
| 103 | 3 | return true; |
|
| 104 | } |
||
| 105 | |||
| 106 | 3 | return false; |
|
| 107 | } |
||
| 108 | |||
| 109 | // We now know the super type is not built-in and there's no string match, sub type must not be built-in |
||
| 110 | if (isset(self::$builtInTypes[$subTypeName])) { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | return \is_subclass_of($subTypeName, $superTypeName); |
||
| 115 | } |
||
| 116 | } |
||
| 117 |