Passed
Push — master ( 0b2a3d...a47e04 )
by Bruno
17:31 queued 04:58
created

FormulariumUtils   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 33
c 1
b 0
f 0
dl 0
loc 64
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getFieldFromDirectives() 0 62 8
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use Formularium\Exception\ClassNotFoundException;
6
use Formularium\Field;
7
use Formularium\ValidatorFactory;
8
use GraphQL\Language\AST\NodeList;
9
use Modelarium\Exception\Exception;
10
11
class FormulariumUtils
12
{
13
    public static function getFieldFromDirectives(
14
        string $fieldName,
15
        string $datatypeName,
16
        NodeList $directives
17
    ): Field {
18
        $validators = [];
19
        $renderable = [];
20
        foreach ($directives as $directive) {
21
            $name = $directive->name->value;
22
23
            if ($name === 'renderable') {
24
                foreach ($directive->arguments as $arg) {
25
                    /**
26
                     * @var \GraphQL\Language\AST\ArgumentNode $arg
27
                     */
28
29
                    $argName = $arg->name->value;
30
                    $argValue = $arg->value->value; /** @phpstan-ignore-line */
0 ignored issues
show
Bug introduced by
Accessing value on the interface GraphQL\Language\AST\ValueNode suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
31
                    $renderable[$argName] = $argValue;
32
                }
33
                continue;
34
            }
35
36
            $validator = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $validator is dead and can be removed.
Loading history...
37
            try {
38
                $validator = ValidatorFactory::class($name);
39
            } catch (ClassNotFoundException $e) {
40
                continue;
41
            }
42
43
            /**
44
             * @var ValidatorMetadata $metadata
45
             */
46
            $metadata = $validator::getMetadata();
47
            $arguments = [];
48
49
            foreach ($directive->arguments as $arg) {
50
                /**
51
                 * @var \GraphQL\Language\AST\ArgumentNode $arg
52
                 */
53
54
                $argName = $arg->name->value;
55
                $argValue = $arg->value->value; /** @phpstan-ignore-line */
56
57
                $argValidator = $metadata->argument($argName);
58
                if (!$argValidator) {
59
                    throw new Exception("Directive $validator does not have argument $argName");
60
                }
61
                if ($argValidator->type === 'Int') {
62
                    $argValue = (int)$argValue;
63
                }
64
                $arguments[$argName] = $argValue;
65
            }
66
67
            $validators[$name] = $arguments;
68
        }
69
70
        return new Field(
71
            $fieldName,
72
            $datatypeName,
73
            $renderable,
74
            $validators
75
        );
76
    }
77
}
78