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 (#65)
by Jérémiah
29:02 queued 04:17
created

Executor::addSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\Request;
13
14
use GraphQL\GraphQL;
15
use GraphQL\Schema;
16
use GraphQL\Validator\DocumentValidator;
17
use GraphQL\Validator\Rules\QueryComplexity;
18
use GraphQL\Validator\Rules\QueryDepth;
19
use Overblog\GraphQLBundle\Error\ErrorHandler;
20
use Overblog\GraphQLBundle\Event\Events;
21
use Overblog\GraphQLBundle\Event\ExecutorContextEvent;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
24
25
class Executor
26
{
27
    /**
28
     * @var Schema[]
29
     */
30
    private $schemas;
31
32
    /**
33
     * @var EventDispatcherInterface|null
34
     */
35
    private $dispatcher;
36
37
    /** @var bool */
38
    private $throwException;
39 8
40
    /** @var ErrorHandler|null */
41 8
    private $errorHandler;
42 8
43 8
    public function __construct(EventDispatcherInterface $dispatcher = null, $throwException = false, ErrorHandler $errorHandler = null)
44 8
    {
45 8
        $this->dispatcher = $dispatcher;
46
        $this->throwException = (bool) $throwException;
47 8
        $this->errorHandler = $errorHandler;
48
    }
49
50 8
    public function addSchema($name, Schema $schema)
51 8
    {
52 8
        $this->schemas[$name] = $schema;
53
54 8
        return $this;
55
    }
56
57 8
    public function setMaxQueryDepth($maxQueryDepth)
58 8
    {
59 8
        /** @var QueryDepth $queryDepth */
60
        $queryDepth = DocumentValidator::getRule('QueryDepth');
61
        $queryDepth->setMaxQueryDepth($maxQueryDepth);
62
    }
63
64
    public function setMaxQueryComplexity($maxQueryComplexity)
65
    {
66 23
        /** @var QueryComplexity $queryComplexity */
67
        $queryComplexity = DocumentValidator::getRule('QueryComplexity');
68 23
        $queryComplexity->setMaxQueryComplexity($maxQueryComplexity);
69
    }
70 23
71
    /**
72
     * @param bool $throwException
73 40
     *
74
     * @return $this
75 40
     */
76 40
    public function setThrowException($throwException)
77 40
    {
78 40
        $this->throwException = (bool) $throwException;
79 40
80
        return $this;
81 40
    }
82 40
83 40
    public function execute(array $data, array $context = [], $schemaName = null)
84 40
    {
85 40
        if (null !== $this->dispatcher) {
86 40
            $event = new ExecutorContextEvent($context);
87 40
            $this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, $event);
88 40
            $context = $event->getExecutorContext();
89
        }
90 40
91 40
        $schema = $this->getSchema($schemaName);
92 40
93
        $executionResult = GraphQL::executeAndReturnResult(
94 40
            $schema,
95
            isset($data[ParserInterface::PARAM_QUERY]) ? $data[ParserInterface::PARAM_QUERY] : null,
96
            $context,
97
            $context,
98
            $data[ParserInterface::PARAM_VARIABLES],
99
            isset($data[ParserInterface::PARAM_OPERATION_NAME]) ? $data[ParserInterface::PARAM_OPERATION_NAME] : null
100
        );
101
102
        if (null !== $this->errorHandler) {
103
            $this->errorHandler->handleErrors($executionResult, $this->throwException);
0 ignored issues
show
Bug introduced by
It seems like $executionResult defined by \GraphQL\GraphQL::execut...OPERATION_NAME] : null) on line 93 can also be of type array; however, Overblog\GraphQLBundle\E...Handler::handleErrors() does only seem to accept object<GraphQL\Executor\ExecutionResult>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
104
        }
105
106
        return $executionResult;
107
    }
108
109
    /**
110
     * @param string|null $name
111
     *
112
     * @return Schema
113
     */
114
    public function getSchema($name = null)
115
    {
116
        if (empty($this->schemas)) {
117
            throw new \RuntimeException('At least one schema should be declare.');
118
        }
119
120
        if (null === $name) {
121
            $schema = array_values($this->schemas)[0];
122
        } else {
123
            if (!isset($this->schemas[$name])) {
124
                throw new NotFoundHttpException(sprintf('Could not found "%s" schema.', $name));
125
            }
126
            $schema = $this->schemas[$name];
127
        }
128
129
        return $schema;
130
    }
131
}
132