Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

BlogSchema::build()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 0
loc 47
rs 9.0303
cc 3
eloc 29
nc 1
nop 1
1
<?php
2
/**
3
 * BlogSchema.php
4
 */
5
6
namespace Examples\Blog\Schema;
7
8
use Youshido\GraphQL\Config\Schema\SchemaConfig;
9
use Youshido\GraphQL\Execution\ResolveInfo;
10
use Youshido\GraphQL\Schema\AbstractSchema;
11
use Youshido\GraphQL\Type\ListType\ListType;
12
use Youshido\GraphQL\Type\Scalar\StringType;
13
14
class BlogSchema extends AbstractSchema
15
{
16
    public function build(SchemaConfig $config)
17
    {
18
        $config->getQuery()->addFields([
19
            'latestPost'           => [
20
                'type'    => new PostType(),
21
                'resolve' => function ($value, array $args, ResolveInfo $info) {
22
                    return $info->getReturnType()->getOne(empty($args['id']) ? 1 : $args['id']);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Youshido\GraphQL\Type\AbstractType as the method getOne() does only exist in the following sub-classes of Youshido\GraphQL\Type\AbstractType: Examples\Blog\Schema\PostType, Examples\StarWars\FactionType, Examples\StarWars\ShipType. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
23
                }
24
            ],
25
            'randomBanner'         => [
26
                'type'    => new BannerType(),
27
                'resolve' => function () {
28
                    return DataProvider::getBanner(rand(1, 10));
29
                }
30
            ],
31
            'pageContentUnion'     => [
32
                'type'    => new ListType(new ContentBlockUnion()),
33
                'resolve' => function () {
34
                    return [DataProvider::getPost(1), DataProvider::getBanner(1)];
35
                }
36
            ],
37
            'pageContentInterface' => [
38
                'type'    => new ListType(new ContentBlockInterface()),
39
                'resolve' => function () {
40
                    return [DataProvider::getPost(2), DataProvider::getBanner(3)];
41
                }
42
            ]
43
        ]);
44
        $config->getMutation()->addFields([
45
            new LikePostField(),
46
            'createPost' => [
47
                'type'    => new PostType(),
48
                'args'    => [
49
                    'post'   => new PostInputType(),
50
                    'author' => new StringType()
51
                ],
52
                'resolve' => function ($value, array $args, ResolveInfo $info) {
0 ignored issues
show
Unused Code introduced by
The parameter $info is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
                    // code for creating a new post goes here
54
                    // we simple use our DataProvider for now
55
                    $post = DataProvider::getPost(10);
56
                    if (!empty($args['post']['title'])) $post['title'] = $args['post']['title'];
57
58
                    return $post;
59
                }
60
            ]
61
        ]);
62
    }
63
64
}
65