| Conditions | 14 |
| Paths | 11 |
| Total Lines | 56 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 19 | public function __invoke( |
||
| 20 | ?ReflectionType $type, |
||
| 21 | ?ReflectionType $comparedType |
||
| 22 | ) : bool { |
||
| 23 | if ($comparedType === null) { |
||
| 24 | return true; |
||
| 25 | } |
||
| 26 | |||
| 27 | if ($type === null) { |
||
| 28 | // nothing can be contravariant to `mixed` besides `mixed` itself (handled above) |
||
| 29 | return false; |
||
| 30 | } |
||
| 31 | |||
| 32 | if ($type->allowsNull() && ! $comparedType->allowsNull()) { |
||
| 33 | return false; |
||
| 34 | } |
||
| 35 | |||
| 36 | $typeAsString = $type->__toString(); |
||
| 37 | $comparedTypeAsString = $comparedType->__toString(); |
||
| 38 | |||
| 39 | if (strtolower($typeAsString) === strtolower($comparedTypeAsString)) { |
||
| 40 | return true; |
||
| 41 | } |
||
| 42 | |||
| 43 | if ($typeAsString === 'void') { |
||
| 44 | // everything is always contravariant to `void` |
||
| 45 | return true; |
||
| 46 | } |
||
| 47 | |||
| 48 | if ($comparedTypeAsString === 'object' && ! $type->isBuiltin()) { |
||
| 49 | // `object` is always contravariant to any object type |
||
| 50 | return true; |
||
| 51 | } |
||
| 52 | |||
| 53 | if ($comparedTypeAsString === 'iterable' && $typeAsString === 'array') { |
||
| 54 | return true; |
||
| 55 | } |
||
| 56 | |||
| 57 | if ($type->isBuiltin() !== $comparedType->isBuiltin()) { |
||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | if ($type->isBuiltin()) { |
||
| 62 | // All other type declarations have no variance/contravariance relationship |
||
| 63 | return false; |
||
| 64 | } |
||
| 65 | |||
| 66 | $typeReflectionClass = $type->targetReflectionClass(); |
||
| 67 | |||
| 68 | if ($comparedType->targetReflectionClass()->isInterface()) { |
||
| 69 | return $typeReflectionClass->implementsInterface($comparedTypeAsString); |
||
| 70 | } |
||
| 71 | |||
| 72 | return ArrayHelpers::stringArrayContainsString( |
||
| 73 | $comparedTypeAsString, |
||
| 74 | $typeReflectionClass->getParentClassNames() |
||
| 75 | ); |
||
| 78 |