Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#801)
by Vincent
20:50
created

TypeHintTypeGuesser::guessType()   C

Complexity

Conditions 13
Paths 70

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 24
c 1
b 0
f 0
nc 70
nop 3
dl 0
loc 40
rs 6.6166

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Config\Parser\MetadataParser\TypeGuesser;
6
7
use ReflectionClass;
8
use ReflectionMethod;
9
use ReflectionNamedType;
10
use ReflectionParameter;
11
use ReflectionProperty;
12
use Reflector;
13
14
class TypeHintTypeGuesser extends TypeGuesser
15
{
16
    public function getName(): string
17
    {
18
        return 'Type Hint';
19
    }
20
21
    public function guessType(ReflectionClass $reflectionClass, Reflector $reflector, array $filterGraphQLTypes = []): ?string
22
    {
23
        $type = null;
24
        $hasDefaultValue = false;
25
26
        switch (true) {
27
            case $reflector instanceof ReflectionParameter:
28
                /** @var ReflectionParameter $reflector */
29
                $hasDefaultValue = $reflector->isDefaultValueAvailable();
30
                // no break
31
            case $reflector instanceof ReflectionProperty:
32
                /** @var ReflectionProperty $reflector */
33
                $type = $reflector->hasType() ? $reflector->getType() : null;
0 ignored issues
show
Bug introduced by
The method getType() does not exist on ReflectionProperty. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
                $type = $reflector->hasType() ? $reflector->/** @scrutinizer ignore-call */ getType() : null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method hasType() does not exist on ReflectionProperty. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
                $type = $reflector->/** @scrutinizer ignore-call */ hasType() ? $reflector->getType() : null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
35
                break;
36
            case $reflector instanceof ReflectionMethod:
37
                /** @var ReflectionMethod $reflector */
38
                $type = $reflector->hasReturnType() ? $reflector->getReturnType() : null;
39
                break;
40
        }
41
        /** @var ReflectionNamedType|null $type */
42
        if (!$type) {
43
            throw new TypeGuessingException('No type-hint');
44
        }
45
46
        $sType = $type->getName();
47
        if ($type->isBuiltin()) {
48
            $gqlType = $this->resolveTypeFromPhpType($sType);
49
            if (null === $gqlType) {
50
                throw new TypeGuessingException(sprintf('No corresponding GraphQL type found for builtin type "%s"', $sType));
51
            }
52
        } else {
53
            $gqlType = $this->map->resolveType($sType, $filterGraphQLTypes);
54
            if (null === $gqlType) {
55
                throw new TypeGuessingException(sprintf('No corresponding GraphQL %s found for class "%s"', $filterGraphQLTypes ? implode(',', $filterGraphQLTypes) : 'object', $sType));
56
            }
57
        }
58
        $nullable = $hasDefaultValue || $type->allowsNull();
59
60
        return sprintf('%s%s', $gqlType, $nullable ? '' : '!');
61
    }
62
63
    /**
64
     * Convert a PHP Builtin type to a GraphQL type.
65
     */
66
    protected function resolveTypeFromPhpType(string $phpType): ?string
67
    {
68
        switch ($phpType) {
69
            case 'boolean':
70
            case 'bool':
71
                return 'Boolean';
72
            case 'integer':
73
            case 'int':
74
                return 'Int';
75
            case 'float':
76
            case 'double':
77
                return 'Float';
78
            case 'string':
79
                return 'String';
80
            default:
81
                return null;
82
        }
83
    }
84
}
85