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

Completed
Pull Request — master (#234)
by Jérémiah
09:20
created

TypeResolver::createTypeLoadingException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Overblog\GraphQLBundle\Resolver;
4
5
use GraphQL\Type\Definition\Type;
6
use Psr\Cache\CacheItemPoolInterface;
7
use Symfony\Component\Cache\Adapter\ArrayAdapter;
8
9
class TypeResolver extends AbstractResolver
10
{
11
    /** @var CacheItemPoolInterface */
12
    private $cacheAdapter;
13
14 82
    public function __construct(CacheItemPoolInterface $cacheAdapter = null)
15
    {
16 82
        $this->cacheAdapter = null !== $cacheAdapter ? $cacheAdapter : new ArrayAdapter(0, false);
17 82
    }
18
19
    /**
20
     * @param string $alias
21
     *
22
     * @return Type
23
     */
24 75
    public function resolve($alias)
25
    {
26 75
        if (null === $alias) {
27 63
            return;
28
        }
29 75
        $item = $this->cacheAdapter->getItem(md5($alias));
30
31 75
        if (!$item->isHit()) {
32 75
            $type = $this->string2Type($alias);
33 72
            $item->set($type);
34 72
            $this->cacheAdapter->save($item);
35
        }
36
37 72
        return $item->get();
38
    }
39
40 75
    private function string2Type($alias)
41
    {
42 75
        if (false !== ($type = $this->wrapTypeIfNeeded($alias))) {
43 6
            return $type;
44
        }
45
46 74
        return $this->baseType($alias);
47
    }
48
49 74
    private function baseType($alias)
50
    {
51
        try {
52 74
            $type = $this->getSolution($alias);
53 1
        } catch (\Error $error) {
54 1
            throw self::createTypeLoadingException($alias, $error);
0 ignored issues
show
Documentation introduced by
$error is of type object<Error>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
        } catch (\Exception $exception) {
56
            throw self::createTypeLoadingException($alias, $exception);
0 ignored issues
show
Documentation introduced by
$exception is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
        }
58
59 73
        if (null !== $type) {
60 72
            return $type;
61
        }
62
63 1
        throw new UnresolvableException(
64 1
            sprintf('Unknown type with alias "%s" (verified service tag)', $alias)
65
        );
66
    }
67
68 75
    private function wrapTypeIfNeeded($alias)
69
    {
70
        // Non-Null
71 75
        if ('!' === $alias[strlen($alias) - 1]) {
72 4
            return Type::nonNull($this->string2Type(substr($alias, 0, -1)));
73
        }
74
        // List
75 75
        if ($this->hasNeedListOfWrapper($alias)) {
76 5
            return Type::listOf($this->string2Type(substr($alias, 1, -1)));
77
        }
78
79 74
        return false;
80
    }
81
82 75
    private function hasNeedListOfWrapper($alias)
83
    {
84 75
        if ('[' === $alias[0]) {
85 6
            $got = $alias[strlen($alias) - 1];
86 6
            if (']' !== $got) {
87 1
                throw new UnresolvableException(
88 1
                    sprintf('Malformed ListOf wrapper type "%s" expected "]" but got "%s".', $alias, json_encode($got))
89
                );
90
            }
91
92 5
            return true;
93
        }
94
95 74
        return false;
96
    }
97
98
    /**
99
     * @param string     $alias
100
     * @param \Throwable $errorOrException
101
     *
102
     * @return \RuntimeException
103
     */
104 1
    private static function createTypeLoadingException($alias, $errorOrException)
105
    {
106 1
        return new \RuntimeException(
107 1
            sprintf(
108 1
                'Type class for alias %s could not be load. If you are using your own classLoader verify the path and the namespace please.',
109 1
                json_encode($alias)
110
            ),
111 1
            0,
112 1
            $errorOrException
113
        );
114
    }
115
116 75
    protected function postLoadSolution($solution)
117
    {
118
        // also add solution with real type name if needed for typeLoader when using autoMapping
119 75
        if ($solution && !$this->hasSolution($solution->name)) {
120 2
            $this->addSolution($solution->name, function () use ($solution) {
121 2
                return $solution;
122 2
            });
123
        }
124 75
    }
125
126 76
    protected function supportedSolutionClass()
127
    {
128 76
        return Type::class;
129
    }
130
}
131