Passed
Push — master ( 2fa866...3020a3 )
by Bruno
07:44
created

FormulariumUtils::directiveToExtradata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 6
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use Formularium\Exception\ClassNotFoundException;
6
use Formularium\Extradata;
0 ignored issues
show
Bug introduced by
The type Formularium\Extradata was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Formularium\ExtradataParameter;
0 ignored issues
show
Bug introduced by
The type Formularium\ExtradataParameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Formularium\Field;
9
use Formularium\Factory\ValidatorFactory;
10
use Formularium\Metadata;
11
use GraphQL\Language\AST\DirectiveNode;
12
use GraphQL\Language\AST\NodeList;
13
use Modelarium\Exception\Exception;
14 10
15
class FormulariumUtils
16
{
17
    public static function getFieldFromDirectives(
18
        string $fieldName,
19 10
        string $datatypeName,
20 10
        NodeList $directives
21 10
    ): Field {
22 8
        $validators = [];
23
        $renderable = [];
24 8
        $extradata = [];
25
        foreach ($directives as $directive) {
26
            $name = $directive->name->value;
27
28
            if ($name === 'renderable') {
29
                foreach ($directive->arguments as $arg) {
30
                    /**
31
                     * @var \GraphQL\Language\AST\ArgumentNode $arg
32
                     */
33
34
                    $argName = $arg->name->value;
35
                    $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...
36
                    $renderable[$argName] = $argValue;
37 8
                }
38
                continue;
39 8
            }
40 8
41 8
            $extradata[] = FormulariumUtils::directiveToExtradata($directive);
42
43
            $validator = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $validator is dead and can be removed.
Loading history...
44
            try {
45
                $validator = ValidatorFactory::class($name);
46
            } catch (ClassNotFoundException $e) {
47
                continue;
48
            }
49
50
            /**
51
             * @var Metadata $metadata
52
             */
53
            $metadata = $validator::getMetadata();
54
            $arguments = [];
55
56
            foreach ($directive->arguments as $arg) {
57
                /**
58
                 * @var \GraphQL\Language\AST\ArgumentNode $arg
59
                 */
60
61
                $argName = $arg->name->value;
62
                $argValue = $arg->value->value; /** @phpstan-ignore-line */
63
64
                $argValidator = $metadata->parameter($argName);
65
                if (!$argValidator) {
66
                    throw new Exception("Directive $validator does not have argument $argName");
67
                }
68
                if ($argValidator->type === 'Int') {
69
                    $argValue = (int)$argValue;
70
                }
71 10
                $arguments[$argName] = $argValue;
72 10
            }
73 10
74 10
            $validators[$name] = $arguments;
75 10
        }
76
77
        return new Field(
78
            $fieldName,
79
            $datatypeName,
80
            $renderable,
81
            $validators,
82
            $extradata
0 ignored issues
show
Unused Code introduced by
The call to Formularium\Field::__construct() has too many arguments starting with $extradata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        return /** @scrutinizer ignore-call */ new Field(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
83
        );
84
    }
85
86
    public static function directiveToExtradata(DirectiveNode $directive): Extradata
87
    {
88
        $metadataArgs = [];
89
        foreach ($directive->arguments as $arg) {
90
            $metadataArgs[] = new ExtradataParameter(
91
                $arg->name->value,
92
                // @phpstan-ignore-next-line
93
                $arg->value->value
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...
94
            );
95
        }
96
        return new Extradata(
97
            $directive->name->value,
98
            $metadataArgs
99
        );
100
    }
101
}
102