Completed
Push — master ( 4d2ac5...1a75da )
by Portey
03:12
created

FieldConfig::getArgument()   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 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 11/30/15 1:30 AM
7
*/
8
9
namespace Youshido\GraphQL\Type\Config\Field;
10
11
use Youshido\GraphQL\Type\Config\Config;
12
use Youshido\GraphQL\Type\Config\Traits\ArgumentsAwareTrait;
13
use Youshido\GraphQL\Type\Object\AbstractObjectType;
14
use Youshido\GraphQL\Type\TypeInterface;
15
use Youshido\GraphQL\Type\TypeMap;
16
17
class FieldConfig extends Config
18
{
19
20
    use ArgumentsAwareTrait {
21
        getArguments as traitGetArguments;
22
        getArgument as traitGetArgument;
23
    }
24
25 17
    public function getRules()
26
    {
27
        return [
28 17
            'name'              => ['type' => TypeMap::TYPE_STRING, 'required' => true],
29 17
            'type'              => ['type' => TypeMap::TYPE_ANY, 'required' => true],
30 17
            'args'              => ['type' => TypeMap::TYPE_ARRAY],
31 17
            'required'          => ['type' => TypeMap::TYPE_BOOLEAN],
32 17
            'description'       => ['type' => TypeMap::TYPE_STRING],
33 17
            'resolve'           => ['type' => TypeMap::TYPE_FUNCTION],
34 17
            'isDeprecated'      => ['type' => TypeMap::TYPE_BOOLEAN],
35 17
            'deprecationReason' => ['type' => TypeMap::TYPE_STRING],
36 17
        ];
37 1
    }
38
39 17
    protected function build()
40
    {
41 17
        $this->buildArguments();
42 17
    }
43
44 12
    public function resolve($value = null, $args = [])
45
    {
46 12
        $resolveFunction = $this->get('resolve', null);
47
48 12
        if ($resolveFunction && is_callable($resolveFunction)) {
49 5
            return $resolveFunction($value, $args);
50
        }
51
52 8
        return $this->getType()->resolve($value, $args);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Youshido\GraphQL\Type\TypeInterface as the method resolve() does only exist in the following implementations of said interface: Youshido\GraphQL\Definition\ArgumentListType, Youshido\GraphQL\Definition\ArgumentType, Youshido\GraphQL\Definition\EnumValueListType, Youshido\GraphQL\Definition\EnumValueType, Youshido\GraphQL\Definition\FieldListType, Youshido\GraphQL\Definition\FieldType, Youshido\GraphQL\Definition\InputValueListType, Youshido\GraphQL\Definition\InputValueType, Youshido\GraphQL\Definition\InterfaceListType, Youshido\GraphQL\Definition\InterfaceType, Youshido\GraphQL\Definition\MutationType, Youshido\GraphQL\Definition\PossibleOfListType, Youshido\GraphQL\Definition\PossibleOfType, Youshido\GraphQL\Definition\QueryListType, Youshido\GraphQL\Definition\QueryType, Youshido\GraphQL\Definition\SchemaType, Youshido\GraphQL\Definition\TypeDefinitionType, Youshido\GraphQL\Type\ListType\AbstractListType, Youshido\GraphQL\Type\ListType\ListType, Youshido\GraphQL\Type\Object\AbstractEnumType, Youshido\GraphQL\Type\Ob...AbstractInputObjectType, Youshido\GraphQL\Type\Object\AbstractObjectType, Youshido\GraphQL\Type\Object\EnumType, Youshido\GraphQL\Type\Object\InputObjectType, Youshido\GraphQL\Type\Object\ObjectType, Youshido\Tests\DataProvider\UserType, Youshido\Tests\Schema\DroidType, Youshido\Tests\Schema\EpisodeEnum, Youshido\Tests\Schema\HumanType, Youshido\Tests\Schema\QueryType.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
53
    }
54
55
    /**
56
     * @return TypeInterface|AbstractObjectType
57
     */
58 15
    public function getType()
59
    {
60 15
        return $this->data['type'];
61
    }
62
63 5
    public function getArgument($name)
64
    {
65 5
        return $this->traitGetArgument($name) ?: $this->getType()->getConfig()->getArgument($name);
66
    }
67
68
    /**
69
     * @return \Youshido\GraphQL\Type\Field\InputField[]
70
     */
71 13
    public function getArguments()
72
    {
73 13
        return $this->traitGetArguments() ?: $this->getType()->getConfig()->getArguments();
74
    }
75
76
}