MaxComplexityQueryVisitor::visit()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 8
nop 3
crap 4
1
<?php
2
/*
3
* Concrete implementation of query visitor.
4
*
5
* Enforces maximum complexity on a query, computed from "cost" functions on
6
* the fields touched by that query.
7
*
8
* @author Ben Roberts <[email protected]>
9
* created: 7/11/16 11:05 AM
10
*/
11
12
namespace Youshido\GraphQL\Execution\Visitor;
13
14
15
use Youshido\GraphQL\Config\Field\FieldConfig;
16
17
class MaxComplexityQueryVisitor extends AbstractQueryVisitor
18
{
19
20
    /**
21
     * @var int max score allowed before throwing an exception (causing processing to stop)
22
     */
23
    public $maxScore;
24
25
    /**
26
     * @var int default score for nodes without explicit cost functions
27
     */
28
    protected $defaultScore = 1;
29
30
    /**
31
     * MaxComplexityQueryVisitor constructor.
32
     *
33
     * @param int $max max allowed complexity score
34
     */
35 2
    public function __construct($max)
36
    {
37 2
        parent::__construct();
38
39 2
        $this->maxScore = $max;
40 2
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function visit(array $args, FieldConfig $fieldConfig, $childScore = 0)
46
    {
47 2
        $cost = $fieldConfig->get('cost', null);
48 2
        if (is_callable($cost)) {
49 1
            $cost = $cost($args, $fieldConfig, $childScore);
50 1
        }
51
52 2
        $cost = is_null($cost) ? $this->defaultScore : $cost;
53 2
        $this->memo += $cost;
54
55 2
        if ($this->memo > $this->maxScore) {
56 1
            throw new \Exception('query exceeded max allowed complexity of ' . $this->maxScore);
57
        }
58
59 2
        return $cost;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $cost; (object|integer|double|string|array|boolean) is incompatible with the return type declared by the abstract method Youshido\GraphQL\Executi...ractQueryVisitor::visit of type integer|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
    }
61
}