FormulariumUtils   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 54.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 106
ccs 25
cts 46
cp 0.5435
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getFieldFromDirectives() 0 66 8
A directiveToExtradata() 0 27 4
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use ErrorException;
6
use Formularium\Exception\ClassNotFoundException;
7
use Formularium\Extradata;
8
use Formularium\ExtradataParameter;
9
use Formularium\Field;
10
use Formularium\Factory\ValidatorFactory;
11
use Formularium\Metadata;
12
use GraphQL\Language\AST\ArgumentNode;
13
use GraphQL\Language\AST\DirectiveNode;
14
use GraphQL\Language\AST\ListValueNode;
15
use GraphQL\Language\AST\NodeList;
16
use Modelarium\Exception\Exception;
17
18
use function Safe\json_encode;
19
20
class FormulariumUtils
21
{
22
    /**
23
     * Undocumented function
24
     *
25
     * @param string $fieldName
26
     * @param string $datatypeName
27
     * @param NodeList<DirectiveNode> $directives
28
     * @return Field
29
     */
30 10
    public static function getFieldFromDirectives(
31
        string $fieldName,
32
        string $datatypeName,
33
        NodeList $directives
34
    ): Field {
35 10
        $validators = [];
36 10
        $renderable = [];
37 10
        $extradata = [];
38 10
        foreach ($directives as $directive) {
39 8
            $name = $directive->name->value;
40
41 8
            if ($name === 'renderable') {
42
                foreach ($directive->arguments as $arg) {
43
                    /**
44
                     * @var \GraphQL\Language\AST\ArgumentNode $arg
45
                     */
46
47
                    $argName = $arg->name->value;
48
                    $argValue = $arg->value->value; /** @phpstan-ignore-line */
0 ignored issues
show
Bug introduced by
The property value does not seem to exist on GraphQL\Language\AST\ObjectValueNode.
Loading history...
Bug introduced by
The property value does not seem to exist on GraphQL\Language\AST\NullValueNode.
Loading history...
Bug introduced by
The property value does not exist on GraphQL\Language\AST\ListValueNode. Did you mean values?
Loading history...
Documentation Bug introduced by
It seems like $arg->value->value can also be of type string. However, the property $value is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Bug introduced by
The property value does not seem to exist on GraphQL\Language\AST\VariableNode.
Loading history...
49
                    $renderable[$argName] = $argValue;
50
                }
51
                continue;
52
            }
53
54 8
            $extradata[] = FormulariumUtils::directiveToExtradata($directive);
55
56 8
            $validator = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $validator is dead and can be removed.
Loading history...
57
            try {
58 8
                $validator = ValidatorFactory::class($name);
59 8
            } catch (ClassNotFoundException $e) {
60 8
                continue;
61
            }
62
63
            /**
64
             * @var Metadata $metadata
65
             */
66
            $metadata = $validator::getMetadata();
67
            $arguments = [];
68
69
            foreach ($directive->arguments as $arg) {
70
                /**
71
                 * @var \GraphQL\Language\AST\ArgumentNode $arg
72
                 */
73
74
                $argName = $arg->name->value;
75
                $argValue = $arg->value->value; /** @phpstan-ignore-line */
76
77
                $argValidator = $metadata->parameter($argName);
78
                if (!$argValidator) {
79
                    throw new Exception("Directive $validator does not have argument $argName");
80
                }
81
                if ($argValidator->type === 'Int') {
82
                    $argValue = (int)$argValue;
83
                }
84
                $arguments[$argName] = $argValue;
85
            }
86
87
            $validators[$name] = $arguments;
88
        }
89
90 10
        return new Field(
91 10
            $fieldName,
92
            $datatypeName,
93
            $renderable,
94
            $validators,
95
            $extradata
96
        );
97
    }
98
99 9
    public static function directiveToExtradata(DirectiveNode $directive): Extradata
100
    {
101 9
        $metadataArgs = [];
102 9
        foreach ($directive->arguments as $arg) {
103
            /**
104
             * @var ArgumentNode $arg
105
             */
106 4
            $v = $arg->value;
107 4
            $extradataV = null;
108 4
            if ($v instanceof ListValueNode) {
109
                $fields = [];
110
                foreach ($v->values as $i) {
111
                    $fields[] = $i->value; /** @phpstan-ignore-line */
112
                }
113
                $extradataV = json_encode($fields); // TODO: ExtradataParameter does not support array
114
            } else {
115 4
                $extradataV = $arg->value->value; /** @phpstan-ignore-line */
0 ignored issues
show
Bug introduced by
The property value does not seem to exist on GraphQL\Language\AST\NullValueNode.
Loading history...
Documentation Bug introduced by
It seems like $arg->value->value can also be of type string. However, the property $value is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Bug introduced by
The property value does not exist on GraphQL\Language\AST\ListValueNode. Did you mean values?
Loading history...
Bug introduced by
The property value does not seem to exist on GraphQL\Language\AST\VariableNode.
Loading history...
Bug introduced by
The property value does not seem to exist on GraphQL\Language\AST\ObjectValueNode.
Loading history...
116
            }
117
118 4
            $metadataArgs[] = new ExtradataParameter(
119 4
                $arg->name->value,
120
                $extradataV
121
            );
122
        }
123 9
        return new Extradata(
124 9
            $directive->name->value,
125
            $metadataArgs
126
        );
127
    }
128
}
129