| Conditions | 19 |
| Paths | 17 |
| Total Lines | 71 |
| Code Lines | 32 |
| Lines | 10 |
| Ratio | 14.08 % |
| Tests | 24 |
| CRAP Score | 24.6406 |
| 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 | 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
|
|||
| 57 | 40 | $subTypeName = (string)$subTypeName; |
|
|
1 ignored issue
–
show
|
|||
| 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
|
|||
| 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
|
|||
| 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
|
|||
| 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 |