We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 21 |
| Paths | 144 |
| Total Lines | 72 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 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 |
||
| 41 | public function guessType(ReflectionClass $reflectionClass, Reflector $reflector, array $filterGraphQLTypes = []): ?string |
||
| 42 | { |
||
| 43 | $contextFactory = new ContextFactory(); |
||
| 44 | $context = $contextFactory->createFromReflector($reflectionClass); |
||
| 45 | $docBlockComment = $reflector->getDocComment(); |
||
|
|
|||
| 46 | if (!$docBlockComment) { |
||
| 47 | throw new TypeGuessingException(sprintf('Doc Block not found')); |
||
| 48 | } |
||
| 49 | |||
| 50 | try { |
||
| 51 | $docBlock = $this->getParser()->create($docBlockComment, $context); |
||
| 52 | } catch (Exception $e) { |
||
| 53 | throw new TypeGuessingException(sprintf('Doc Block parsing failed with error: %s', $e->getMessage())); |
||
| 54 | } |
||
| 55 | $tagName = $reflector instanceof ReflectionProperty ? 'var' : 'return'; |
||
| 56 | $tags = $docBlock->getTagsByName($tagName); |
||
| 57 | $tag = $tags[0] ?? null; |
||
| 58 | if (!$tag || !$tag instanceof TagWithType) { |
||
| 59 | throw new TypeGuessingException(sprintf('No @%s tag found in doc block or tag has no type', $tagName)); |
||
| 60 | } |
||
| 61 | $type = $tag->getType(); |
||
| 62 | $isNullable = false; |
||
| 63 | $isList = false; |
||
| 64 | $isListNullable = false; |
||
| 65 | $exceptionPrefix = sprintf('Tag @%s found', $tagName); |
||
| 66 | |||
| 67 | if ($type instanceof Compound) { |
||
| 68 | $type = $this->resolveCompound($type); |
||
| 69 | if (!$type) { |
||
| 70 | throw new TypeGuessingException(sprintf('%s, but composite types are only allowed with null. Ex: string|null.', $exceptionPrefix)); |
||
| 71 | } |
||
| 72 | $isNullable = true; |
||
| 73 | } elseif ($type instanceof Nullable) { |
||
| 74 | $isNullable = true; |
||
| 75 | $type = $type->getActualType(); |
||
| 76 | } |
||
| 77 | |||
| 78 | if ($type instanceof AbstractList) { |
||
| 79 | $isList = true; |
||
| 80 | $isListNullable = $isNullable; |
||
| 81 | $isNullable = false; |
||
| 82 | $type = $type->getValueType(); |
||
| 83 | if ($type instanceof Compound) { |
||
| 84 | $type = $this->resolveCompound($type); |
||
| 85 | if (!$type) { |
||
| 86 | throw new TypeGuessingException(sprintf('%s, but composite types in array or iterable are only allowed with null. Ex: string|null.', $exceptionPrefix)); |
||
| 87 | } |
||
| 88 | $isNullable = true; |
||
| 89 | } elseif ($type instanceof Mixed_) { |
||
| 90 | throw new TypeGuessingException(sprintf('%s, but the array values cannot be mixed type', $exceptionPrefix)); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($type instanceof Object_) { |
||
| 95 | $className = $type->getFqsen(); |
||
| 96 | if (!$className) { |
||
| 97 | throw new TypeGuessingException(sprintf('%s, but type "object" is too generic.', $exceptionPrefix)); |
||
| 98 | } |
||
| 99 | // Remove first '\' from returned class name |
||
| 100 | $className = substr((string) $className, 1); |
||
| 101 | $gqlType = $this->map->resolveType((string) $className, $filterGraphQLTypes); |
||
| 102 | if (!$gqlType) { |
||
| 103 | throw new TypeGuessingException(sprintf('%s, but target object "%s" is not a GraphQL Type class.', $exceptionPrefix, $className)); |
||
| 104 | } |
||
| 105 | } else { |
||
| 106 | $gqlType = $this->resolveTypeFromPhpType((string) $type); |
||
| 107 | if (!$gqlType) { |
||
| 108 | throw new TypeGuessingException(sprintf('%s, but unable to resolve type "%s" to a GraphQL scalar.', $exceptionPrefix, (string) $type)); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | return $isList ? sprintf('[%s%s]%s', $gqlType, $isNullable ? '' : '!', $isListNullable ? '' : '!') : sprintf('%s%s', $gqlType, $isNullable ? '' : '!'); |
||
| 113 | } |
||
| 139 |