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 (#638)
by Vincent
11:30
created

GraphQLCollector::getCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\DataCollector;
6
7
use GraphQL\Error\SyntaxError;
8
use GraphQL\Language\AST\DocumentNode;
9
use GraphQL\Language\AST\OperationDefinitionNode;
10
use GraphQL\Language\Parser;
11
use Overblog\GraphQLBundle\Event\ExecutorResultEvent;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
15
16
class GraphQLCollector extends DataCollector
17
{
18
    /**
19
     * GraphQL Batchs executed.
20
     */
21
    protected $batches = [];
22
23
    public function collect(Request $request, Response $response, \Throwable $exception = null): void
24
    {
25
        $error = false;
26
        $count = 0;
27
        foreach ($this->batches as $batch) {
28
            if (isset($batch['error'])) {
29
                $error = true;
30
            }
31
            $count += $batch['count'];
32
        }
33
34
        $this->data = [
35
            'schema' => $request->attributes->get('_route_params')['schemaName'] ?? 'default',
36
            'batches' => $this->batches,
37
            'count' => $count,
38
            'error' => $error,
39
        ];
40
    }
41
42
    /**
43
     * Check if we have an error.
44
     *
45
     * @return bool
46
     */
47
    public function getError()
48
    {
49
        return $this->data['error'] ?? false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['error'] ?? false also could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the documented return type boolean.
Loading history...
50
    }
51
52
    /**
53
     * Count the number of executed queries.
54
     *
55
     * @return int
56
     */
57
    public function getCount()
58
    {
59
        return $this->data['count'] ?? 0;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['count'] ?? 0 also could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the documented return type integer.
Loading history...
60
    }
61
62
    /**
63
     * Return the targeted schema.
64
     *
65
     * @return string
66
     */
67
    public function getSchema()
68
    {
69
        return $this->data['schema'] ?? 'default';
70
    }
71
72
    /**
73
     * Return the list of executed batch.
74
     *
75
     * @return array
76
     */
77
    public function getBatches()
78
    {
79
        return $this->data['batches'] ?? [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['batches'] ?? array() also could return the type Symfony\Component\VarDumper\Cloner\Data which is incompatible with the documented return type array.
Loading history...
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function reset(): void
86
    {
87
        $this->data = [];
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getName()
94
    {
95
        return 'graphql';
96
    }
97
98
    /**
99
     * Hook into the GraphQL events to populate the collector.
100
     *
101
     * @param ExecutorResultEvent $event
102
     */
103
    public function onPostExecutor(ExecutorResultEvent $event): void
104
    {
105
        $executorArgument = $event->getExecutorArguments();
106
        $queryString = $executorArgument->getRequestString();
107
        $variables = $executorArgument->getVariableValue();
108
109
        $result = $event->getResult()->toArray();
110
111
        $batch = [
112
            'queryString' => $queryString,
113
            'variables' => $this->cloneVar($variables),
114
            'result' => $this->cloneVar($result),
115
            'count' => 0,
116
        ];
117
118
        try {
119
            $parsed = Parser::parse($queryString);
120
            $batch['graphql'] = $this->extractGraphql($parsed);
121
            if (isset($batch['graphql']['fields'])) {
122
                $batch['count'] += \count($batch['graphql']['fields']);
123
            }
124
            $error = $result['errors'][0] ?? false;
125
            if ($error) {
126
                $batch['error'] = [
127
                    'message' => $error['message'],
128
                    'location' => $error['locations'][0] ?? false,
129
                ];
130
            }
131
        } catch (SyntaxError $error) {
132
            $location = $error->getLocations()[0] ?? false;
133
            $batch['error'] = ['message' => $error->getMessage(), 'location' => $location];
134
        }
135
136
        $this->batches[] = $batch;
137
    }
138
139
    /**
140
     * Extract GraphQL Information from the documentNode².
141
     *
142
     * @param DocumentNode $document
143
     * @param array        $result
144
     * @param array        $variables
145
     *
146
     * @return array
147
     */
148
    protected function extractGraphql(DocumentNode $document)
149
    {
150
        $operation = null;
151
        $fields = [];
152
153
        foreach ($document->definitions as $definition) {
154
            if ($definition instanceof OperationDefinitionNode) {
155
                $operation = $definition->operation;
156
                foreach ($definition->selectionSet->selections as $selection) {
157
                    $name = $selection->name->value;
0 ignored issues
show
Bug introduced by
Accessing name on the interface GraphQL\Language\AST\SelectionNode suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
158
                    $alias = $selection->alias ? $selection->alias->value : null;
0 ignored issues
show
Bug introduced by
Accessing alias on the interface GraphQL\Language\AST\SelectionNode suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
159
160
                    $fields[] = [
161
                        'name' => $name,
162
                        'alias' => $alias,
163
                    ];
164
                }
165
            }
166
        }
167
168
        return [
169
            'operation' => $operation,
170
            'fields' => $fields,
171
        ];
172
    }
173
}
174