TestSchema::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/*
3
 * This file is a part of GraphQL project.
4
 *
5
 * @author Alexandr Viniychuk <[email protected]>
6
 * created: 10:48 PM 5/14/16
7
 */
8
9
namespace Youshido\Tests\DataProvider;
10
11
12
use Youshido\GraphQL\Config\Schema\SchemaConfig;
13
use Youshido\GraphQL\Execution\ResolveInfo;
14
use Youshido\GraphQL\Schema\AbstractSchema;
15
use Youshido\GraphQL\Type\ListType\ListType;
16
use Youshido\GraphQL\Type\Scalar\IntType;
17
18
class TestSchema extends AbstractSchema
19
{
20
    private $testStatusValue = 0;
21
22
    public function build(SchemaConfig $config)
23
    {
24
        $config->getQuery()->addFields([
25
            'me'     => [
26
                'type'    => new TestObjectType(),
27
                'resolve' => function ($value, $args, ResolveInfo $info) {
28
                    return $info->getReturnType()->getData();
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 getData() does only exist in the following sub-classes of Youshido\GraphQL\Type\AbstractType: Youshido\Tests\DataProvider\TestObjectType. 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...
29
                }
30
            ],
31
            'status' => [
32
                'type'    => new TestEnumType(),
33
                'resolve' => function () {
34
                    return $this->testStatusValue;
35
                }
36
            ],
37
        ]);
38
        $config->getMutation()->addFields([
39
            'updateStatus' => [
40
                'type'    => new TestEnumType(),
41
                'resolve' => function () {
42
                    return $this->testStatusValue;
43
                },
44
                'args'    => [
45
                    'newStatus' => new TestEnumType(),
46
                    'list' => new ListType(new IntType())
47
                ]
48
            ]
49
        ]);
50
    }
51
52
53
}
54