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

ObjectType::getFields()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 6
nc 5
nop 0
crap 4
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\Utils;
17
18
class ObjectType extends BaseObjectType
19
{
20
    private $_isTypeOf;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
21
22
    /**
23
     * @param array $config
24
     *
25
     * @todo open PR on lib to ease inheritance
26
     */
27 38
    public function __construct(array $config)
28
    {
29 38
        Utils::invariant(!empty($config['name']), 'Every type is expected to have name');
30
31 38
        Config::validate($config, [
32 38
            'name' => Config::STRING | Config::REQUIRED,
33 38
            'fields' => Config::arrayOf(
34 38
                FieldDefinition::getDefinition(),
35 38
                Config::KEY_AS_NAME | Config::MAYBE_THUNK
36 38
            ),
37 38
            'description' => Config::STRING,
38 38
            'interfaces' => Config::arrayOf(
39 38
                Config::INTERFACE_TYPE,
40
                Config::MAYBE_THUNK
41 38
            ),
42 38
            'isTypeOf' => Config::CALLBACK, // ($value, ResolveInfo $info) => boolean
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
43 38
            'resolveField' => Config::CALLBACK,
44 38
        ]);
45
46 38
        $this->name = $config['name'];
47 38
        $this->description = isset($config['description']) ? $config['description'] : null;
48 38
        $this->resolveFieldFn = isset($config['resolveField']) ? $config['resolveField'] : null;
49 38
        $this->_isTypeOf = isset($config['isTypeOf']) ? $config['isTypeOf'] : null;
50 38
        $this->config = $config;
51
52 38
        if (isset($config['interfaces'])) {
53 11
            InterfaceType::addImplementationToInterfaces($this);
54 11
        }
55 38
    }
56
57
    /**
58
     * @var FieldDefinition[]
59
     */
60
    private $_fields;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
61
62
    /**
63
     * @return FieldDefinition[]
64
     */
65 80
    public function getFields()
66
    {
67 80
        if (null === $this->_fields) {
68 37
            $fields = isset($this->config['fields']) ? $this->config['fields'] : [];
69 37
            $fields = is_callable($fields) ? call_user_func($fields) : $fields;
70 37
            $this->_fields = FieldDefinition::createMap($fields);
71 37
        }
72
73 80
        return $this->_fields;
74
    }
75
76
    /**
77
     * @param string $name
78
     *
79
     * @return FieldDefinition
80
     *
81
     * @throws \Exception
82
     */
83
    public function getField($name)
84
    {
85
        if (null === $this->_fields) {
86
            $this->getFields();
87
        }
88
        Utils::invariant(isset($this->_fields[$name]), "Field '%s' is not defined for type '%s'", $name, $this->getName());
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Overblog\GraphQLBundle\Definition\ObjectType. Did you maybe mean getNamedType()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
89
90
        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...
91
    }
92
}
93