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

TestSchema   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 1
c 4
b 0
f 0
lcom 1
cbo 7
dl 0
loc 35
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B build() 0 28 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
16
class TestSchema extends AbstractSchema
17
{
18
    private $testStatusValue = 0;
19
20
    public function build(SchemaConfig $config)
21
    {
22
        $config->getQuery()->addFields([
23
            'me'     => [
24
                'type'    => new TestObjectType(),
25
                'resolve' => function ($value, $args, ResolveInfo $info) {
26
                    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...
27
                }
28
            ],
29
            'status' => [
30
                'type'    => new TestEnumType(),
31
                'resolve' => function () {
32
                    return $this->testStatusValue;
33
                }
34
            ],
35
        ]);
36
        $config->getMutation()->addFields([
37
            'updateStatus' => [
38
                'type'    => new TestEnumType(),
39
                'resolve' => function () {
40
                    return $this->testStatusValue;
41
                },
42
                'args'    => [
43
                    'newStatus' => new TestEnumType()
44
                ]
45
            ]
46
        ]);
47
    }
48
49
50
}
51