ExtendSchemaWithLocaleFields   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 11
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extendSchemaWithLocaleFields() 0 12 3
A defineLocaleField() 0 10 1
A defineTranslationIdField() 0 20 1
1
<?php
2
3
namespace BBSLab\NovaTranslation\GraphQL\Directives\Traits;
4
5
use GraphQL\Language\AST\ArgumentNode;
6
use GraphQL\Language\AST\DirectiveNode;
7
use GraphQL\Language\AST\FieldDefinitionNode;
8
use GraphQL\Language\AST\NamedTypeNode;
9
use GraphQL\Language\AST\NameNode;
10
use GraphQL\Language\AST\NodeList;
11
use GraphQL\Language\AST\NonNullTypeNode;
12
use GraphQL\Language\AST\StringValueNode;
13
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
14
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
15
16
trait ExtendSchemaWithLocaleFields
17
{
18
    /**
19
     * Extend GraphQL schema for given type with "locales" fields.
20
     *
21
     * @param  \Nuwave\Lighthouse\Schema\AST\DocumentAST  $documentAST
22
     * @param  string  $typeToExtend
23
     * @return void
24
     */
25
    protected function extendSchemaWithLocaleFields(DocumentAST &$documentAST, string $typeToExtend)
26
    {
27
        foreach ($documentAST->types as &$type) {
28
            if ($type->name->value === $typeToExtend) {
29
                /* @var \GraphQL\Language\AST\ObjectTypeDefinitionNode $type */
30
                $type->fields = ASTHelper::mergeUniqueNodeList($type->fields, [
31
                    $this->defineLocaleField(),
32
                    $this->defineTranslationIdField(),
33
                ]);
34
            }
35
        }
36
    }
37
38
    /**
39
     * Setup "locale" field definition.
40
     *
41
     * @return \GraphQL\Language\AST\FieldDefinitionNode
42
     */
43
    protected function defineLocaleField()
44
    {
45
        return new FieldDefinitionNode([
0 ignored issues
show
Documentation introduced by
array('name' => new \Gra...O', 'block' => false))) is of type array<string,object<Grap...ST\\StringValueNode>"}>, but the function expects a array<integer,object<Gra...er|boolean|double|null>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
            'name' => new NameNode(['value' => 'locale']),
47
            'type' => new NonNullTypeNode(['type' => new NamedTypeNode(['name' => new NameNode(['value' => 'String'])])]),
48
            'arguments' => new NodeList([]),
49
            'directives' => new NodeList([]),
50
            'description' => new StringValueNode(['value' => 'Locale ISO', 'block' => false]),
51
        ]);
52
    }
53
54
    /**
55
     * Setup "translationId" field definition.
56
     *
57
     * @return \GraphQL\Language\AST\FieldDefinitionNode
58
     */
59
    protected function defineTranslationIdField()
60
    {
61
        return new FieldDefinitionNode([
0 ignored issues
show
Documentation introduced by
array('name' => new \Gra...D', 'block' => false))) is of type array<string,object<Grap...ST\\StringValueNode>"}>, but the function expects a array<integer,object<Gra...er|boolean|double|null>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
            'name' => new NameNode(['value' => 'translationId']),
63
            'type' => new NonNullTypeNode(['type' => new NamedTypeNode(['name' => new NameNode(['value' => 'Int'])])]),
64
            'arguments' => new NodeList([]),
65
            'directives' => new NodeList([
66
                new DirectiveNode([
67
                    'name' => new NameNode(['value' => 'rename']),
68
                    'arguments' => new NodeList([
69
                        new ArgumentNode([
0 ignored issues
show
Documentation introduced by
array('name' => new \Gra...d', 'block' => false))) is of type array<string,object<Grap...ST\\StringValueNode>"}>, but the function expects a array<integer,object<Gra...er|boolean|double|null>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
                            'name' => new NameNode(['value' => 'attribute']),
71
                            'value' => new StringValueNode(['value' => 'translation_id', 'block' => false]),
72
                        ]),
73
                    ]),
74
                ]),
75
            ]),
76
            'description' => new StringValueNode(['value' => 'Item translation ID', 'block' => false]),
77
        ]);
78
    }
79
}
80