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 (#23)
by Jérémiah
12:42
created

QueryDepth::setMaxQueryDepth()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\Validator\Rule;
13
14
use GraphQL\Error;
15
use GraphQL\Language\AST\Field;
16
use GraphQL\Language\AST\FragmentSpread;
17
use GraphQL\Language\AST\InlineFragment;
18
use GraphQL\Language\AST\Node;
19
use GraphQL\Language\AST\OperationDefinition;
20
use GraphQL\Language\AST\SelectionSet;
21
use GraphQL\Validator\ValidationContext;
22
23
class QueryDepth extends AbstractQuerySecurity
24
{
25
    const DEFAULT_QUERY_MAX_DEPTH = self::DISABLED;
26
27
    /**
28
     * @var int
29
     */
30
    private static $maxQueryDepth;
31
32 73
    public function __construct($maxQueryDepth = self::DEFAULT_QUERY_MAX_DEPTH)
33
    {
34 73
        $this->setMaxQueryDepth($maxQueryDepth);
35 72
    }
36
37
    /**
38
     * Set max query depth. If equal to 0 no check is done. Must be greater or equal to 0.
39
     *
40
     * @param $maxQueryDepth
41
     */
42 73
    public static function setMaxQueryDepth($maxQueryDepth)
43
    {
44 73
        self::checkIfGreaterOrEqualToZero('maxQueryDepth', $maxQueryDepth);
45
46 72
        self::$maxQueryDepth = (int) $maxQueryDepth;
47 72
    }
48
49 72
    public static function getMaxQueryDepth()
50
    {
51 72
        return self::$maxQueryDepth;
52
    }
53
54 7
    public static function maxQueryDepthErrorMessage($max, $count)
55
    {
56 7
        return sprintf('Max query depth should be %d but got %d.', $max, $count);
57
    }
58
59 72
    public function __invoke(ValidationContext $context)
60
    {
61 72
        return $this->invokeIfNeeded(
62 72
            $context,
63
            [
64 72
                Node::OPERATION_DEFINITION => [
65 72
                    'leave' => function (OperationDefinition $operationDefinition) use ($context) {
66 33
                        $maxDepth = $this->fieldDepth($operationDefinition);
67
68 33
                        if ($maxDepth > $this->getMaxQueryDepth()) {
69 7
                            return new Error($this->maxQueryDepthErrorMessage($this->getMaxQueryDepth(), $maxDepth));
70
                        }
71 72
                    },
72 72
                ],
73
            ]
74 72
        );
75
    }
76
77 72
    protected function isEnabled()
78
    {
79 72
        return $this->getMaxQueryDepth() !== static::DISABLED;
80
    }
81
82 33 View Code Duplication
    private function fieldDepth($node, $depth = 0, $maxDepth = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 33
        if (isset($node->selectionSet) && $node->selectionSet instanceof SelectionSet) {
85 33
            foreach ($node->selectionSet->selections as $childNode) {
86 33
                $maxDepth = $this->nodeDepth($childNode, $depth, $maxDepth);
0 ignored issues
show
Documentation introduced by
$childNode is of type object<GraphQL\Language\AST\Selection>, but the function expects a object<GraphQL\Language\AST\Node>.

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...
87 33
            }
88 33
        }
89
90 33
        return $maxDepth;
91
    }
92
93 33
    private function nodeDepth(Node $node, $depth = 0, $maxDepth = 0)
94
    {
95 33
        switch ($node->kind) {
96 33
            case Node::FIELD:
97
                /* @var Field $node */
98
                // node has children?
99 33
                if (null !== $node->selectionSet) {
0 ignored issues
show
Bug introduced by
The property selectionSet does not seem to exist in GraphQL\Language\AST\Node.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
100
                    // update maxDepth if needed
101 33
                    if ($depth > $maxDepth) {
102 31
                        $maxDepth = $depth;
103 31
                    }
104 33
                    $maxDepth = $this->fieldDepth($node, $depth + 1, $maxDepth);
105 33
                }
106 33
                break;
107
108 21
            case Node::INLINE_FRAGMENT:
109
                /* @var InlineFragment $node */
110
                // node has children?
111 10
                if (null !== $node->selectionSet) {
112 10
                    $maxDepth = $this->fieldDepth($node, $depth, $maxDepth);
113 10
                }
114 10
                break;
115
116 11
            case Node::FRAGMENT_SPREAD:
117
                /* @var FragmentSpread $node */
118 11
                $fragment = $this->getFragment($node);
119
120 11
                if (null !== $fragment) {
121 11
                    $maxDepth = $this->fieldDepth($fragment, $depth, $maxDepth);
122 11
                }
123 11
                break;
124 33
        }
125
126 33
        return $maxDepth;
127
    }
128
}
129