Failed Conditions
Pull Request — master (#1)
by Max
03:22
created

ScalarValidationTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 121
Duplicated Lines 30.58 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 37
loc 121
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 7 62 2
A testNullableScalarValidationOnNullValueSuccess() 30 30 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Tests\Type\ValidatedFieldDefinition;
6
7
use GraphQL\GraphQL;
8
use GraphQL\Tests\Utils;
9
use GraphQL\Type\Definition\ObjectType;
10
use GraphQL\Type\Definition\Type;
11
use GraphQL\Type\Definition\ValidatedFieldDefinition;
12
use GraphQL\Type\Schema;
13
use PHPUnit\Framework\TestCase;
14
15
final class ScalarValidationTest extends TestCase
16
{
17
    /** @var Type */
18
    protected $bookType;
19
20
    /** @var Type */
21
    protected $personType;
22
23
    /** @var mixed[] */
24
    protected $data = [
25
        'people' => [
26
            1 => ['firstName' => 'Wilson'],
27
        ],
28
        'books' => [
29
            1 => [
30
                'title' => 'Where the Red Fern Grows',
31
                'author' => 1,
32
            ],
33
        ],
34
    ];
35
36
    /** @var ObjectType */
37
    protected $query;
38
39
    /** @var Schema */
40
    protected $schema;
41
42
    protected function setUp()
43
    {
44
        $this->personType = new ObjectType([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \GraphQL\Type\Defini...tion\Type::string())))) of type object<GraphQL\Type\Definition\ObjectType> is incompatible with the declared type object<GraphQL\Type\Definition\Type> of property $personType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
            'name' => 'Person',
46
            'fields' => [
47
                'firstName' => [
48
                    'type' => Type::string(),
49
                ],
50
            ],
51
        ]);
52
53
        $this->bookType = new ObjectType([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \GraphQL\Type\Defini... $book['author']; })))) of type object<GraphQL\Type\Definition\ObjectType> is incompatible with the declared type object<GraphQL\Type\Definition\Type> of property $bookType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
            'name' => 'Book',
55
            'fields' => [
56
                'title' => [
57
                    'type' => Type::string(),
58
                    'resolve' => static function ($book) {
59
                        return $book['title'];
60
                    },
61
                ],
62
                'author' => [
63
                    'type' => $this->personType,
64
                    'resolve' => static function ($book) {
65
                        return $book['author'];
66
                    },
67
                ],
68
            ],
69
        ]);
70
71
        $this->query = new ObjectType(['name' => 'Query']);
72
73
        $this->schema = new Schema([
74
            'query' => $this->query,
75
            'mutation' => new ObjectType([
76
                'name' => 'Mutation',
77
                'fields' => function () {
78
                    return [
79
                        'updateBook' => new ValidatedFieldDefinition([
80
                            'name' => 'updateBook',
81
                            'type' => $this->bookType,
82
                            'args' => [
83
                                'bookId' => [
84
                                    'type' => Type::id(),
85
                                    'errorCodes' => ['bookNotFound'],
86 View Code Duplication
                                    'validate' => function ($bookId) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
                                        if (isset($this->data['books'][$bookId])) {
88
                                            return 0;
89
                                        }
90
91
                                        return ['bookNotFound', 'Unknown book!'];
92
                                    },
93
                                ],
94
                            ],
95
                            'resolve' => static function ($value) : bool {
96
                                return !!$value;
97
                            },
98
                        ]),
99
                    ];
100
                },
101
            ]),
102
        ]);
103
    }
104
105 View Code Duplication
    public function testNullableScalarValidationOnNullValueSuccess()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $res = GraphQL::executeQuery(
108
            $this->schema,
109
            Utils::nowdoc('
110
                mutation UpdateBook(
111
                    $bookId:ID
112
                ) {
113
                    updateBook (bookId: $bookId) {
114
                        valid
115
                        suberrors {
116
                            bookId {
117
                                code
118
                                msg
119
                            }
120
                        }
121
                        result {
122
                            title
123
                        }
124
                    }
125
                }
126
            '),
127
            [],
128
            null,
129
            ['bookId' => null]
130
        );
131
132
        static::assertEmpty($res->errors);
133
        static::assertTrue($res->data['updateBook']['valid']);
134
    }
135
}
136