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
13:51 queued 05:06
created

ObjectType::isTypeOf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 2
crap 2
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\Definition;
13
14
use GraphQL\Type\Definition\Config;
15
use GraphQL\Type\Definition\ObjectType as BaseObjectType;
16
use GraphQL\Type\Definition\ResolveInfo;
17
use GraphQL\Utils;
18
19
class ObjectType extends BaseObjectType
20
{
21
    private $isTypeOf;
22
23
    /**
24
     * @var FieldDefinition[]
25
     */
26
    private $fields;
27
28
    /**
29
     * @param array $config
30
     *
31
     * @todo open PR on lib to ease inheritance
32
     */
33 38
    public function __construct(array $config)
34
    {
35 38
        Utils::invariant(!empty($config['name']), 'Every type is expected to have name');
36
37 38
        Config::validate($config, [
38 38
            'name' => Config::STRING | Config::REQUIRED,
39 38
            'fields' => Config::arrayOf(
40 38
                FieldDefinition::getDefinition(),
41 38
                Config::KEY_AS_NAME | Config::MAYBE_THUNK
42 38
            ),
43 38
            'description' => Config::STRING,
44 38
            'interfaces' => Config::arrayOf(
45 38
                Config::INTERFACE_TYPE,
46
                Config::MAYBE_THUNK
47 38
            ),
48 38
            'isTypeOf' => Config::CALLBACK, // ($value, ResolveInfo $info) => boolean
49 38
            'resolveField' => Config::CALLBACK,
50 38
        ]);
51
52 38
        $this->name = $config['name'];
53 38
        $this->description = isset($config['description']) ? $config['description'] : null;
54 38
        $this->resolveFieldFn = isset($config['resolveField']) ? $config['resolveField'] : null;
55 38
        $this->isTypeOf = isset($config['isTypeOf']) ? $config['isTypeOf'] : null;
56 38
        $this->config = $config;
57
58 38
        if (isset($config['interfaces'])) {
59 11
            InterfaceType::addImplementationToInterfaces($this);
60 11
        }
61 38
    }
62
63 80
    public function getFields()
64
    {
65 80
        if (null === $this->fields) {
66 37
            $fields = isset($this->config['fields']) ? $this->config['fields'] : [];
67 37
            $fields = is_callable($fields) ? call_user_func($fields) : $fields;
68 37
            $this->fields = FieldDefinition::createMap($fields);
69 37
        }
70
71 80
        return $this->fields;
72
    }
73
74
    public function getField($name)
75
    {
76
        if (null === $this->fields) {
77
            $this->getFields();
78
        }
79
        Utils::invariant(isset($this->fields[$name]), "Field '%s' is not defined for type '%s'", $name, $this->name);
80
81
        return $this->fields[$name];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->fields[$name]; (Overblog\GraphQLBundle\Definition\FieldDefinition) is incompatible with the return type of the parent method GraphQL\Type\Definition\ObjectType::getField of type GraphQL\Type\Definition\Field.

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...
82
    }
83
84 26
    public function isTypeOf($value, ResolveInfo $info)
85
    {
86 26
        return isset($this->isTypeOf) ? call_user_func($this->isTypeOf, $value, $info) : null;
87
    }
88
}
89