Issue109Schema::build()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 8.9599
c 0
b 0
f 0
cc 2
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Youshido\Tests\Issues\Issue109;
3
4
use Youshido\GraphQL\Config\Schema\SchemaConfig;
5
use Youshido\GraphQL\Execution\ResolveInfo;
6
use Youshido\GraphQL\Schema\AbstractSchema;
7
use Youshido\GraphQL\Type\ListType\ListType;
8
use Youshido\GraphQL\Type\Object\ObjectType;
9
use Youshido\GraphQL\Type\Scalar\IntType;
10
11
class Issue109Schema extends AbstractSchema
12
{
13
14
    public function build(SchemaConfig $config)
15
    {
16
        $config->setQuery(
17
            new ObjectType([
18
                'name'   => 'RootQueryType',
19
                'fields' => [
20
                    'latestPost' => [
21
                        'type'    => new ObjectType([
22
                            'name'   => 'Post',
23
                            'fields' => [
24
                                'id'       => [
25
                                    'type' => new IntType(),
26
                                    'args' => [
27
                                        'comment_id' => new IntType()
28
                                    ]
29
                                ],
30
                                'comments' => [
31
                                    'type' => new ListType(new ObjectType([
32
                                        'name'   => 'Comment',
33
                                        'fields' => [
34
                                            'comment_id' => new IntType()
35
                                        ]
36
                                    ])),
37
                                    'args' => [
38
                                        'comment_id' => new IntType()
39
                                    ]
40
                                ]
41
                            ]
42
                        ]),
43
                        'resolve' => function ($source, array $args, ResolveInfo $info) {
44
                            $internalArgs = [
45
                                'comment_id' => 200
46
                            ];
47
                            if ($field = $info->getFieldAST('comments')->hasArguments()) {
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
                                $internalArgs['comment_id'] = $info->getFieldAST('comments')->getArgumentValue('comment_id');
49
                            }
50
51
                            return [
52
                                "id"       => 1,
53
                                "title"    => "New approach in API has been revealed",
54
                                "summary"  => "In two words - GraphQL Rocks!",
55
                                "comments" => [
56
                                    [
57
                                        "comment_id" => $internalArgs['comment_id']
58
                                    ]
59
                                ]
60
                            ];
61
                        },
62
                        'args'    => [
63
                            'id' => new IntType()
64
                        ]
65
                    ]
66
                ]
67
            ])
68
        );
69
    }
70
71
}