Completed
Pull Request — master (#29)
by
unknown
06:43
created

MutationBuilder::generateMutationArguments()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
namespace Softonic\GraphQL;
4
5
use Softonic\GraphQL\Config\MutationConfigObject;
6
use Softonic\GraphQL\Mutation\Collection as MutationCollection;
7
use Softonic\GraphQL\Mutation\Item as MutationItem;
8
use Softonic\GraphQL\Mutation\MutationObject;
9
use Softonic\GraphQL\Query\Collection as QueryCollection;
10
use Softonic\GraphQL\Query\Item as QueryItem;
11
use Softonic\GraphQL\Query\ReadObject;
12
13
class MutationBuilder
14
{
15
    const MUTATION_TYPE_MAP = [
16
        QueryItem::class       => MutationItem::class,
17
        QueryCollection::class => MutationCollection::class,
18
    ];
19
20
    const DEFAULT_MUTATION_TYPE = MutationItem::class;
21
22
    private $config;
23
24
    private $source;
25
26 48
    public function __construct(MutationConfigObject $config, QueryItem $source)
27
    {
28 48
        $this->config = $config;
29 48
        $this->source = $source;
30 48
    }
31
32 48
    public function build(): MutationItem
33
    {
34 48
        $path   = $this->config->linksTo;
35 48
        $config = $this->config->get($path);
36
37 48
        $mutationType = $this->getMutationType($config, $this->source);
38 48
        $arguments    = $this->generateMutationArguments($config, $this->source, $path);
39
40 48
        return new $mutationType($arguments, $config->children);
41
    }
42
43 48
    private function getMutationType(MutationConfigObject $config, ReadObject $source): string
44
    {
45 48
        return !empty($config->linksTo) ? self::MUTATION_TYPE_MAP[get_class($source)] : self::DEFAULT_MUTATION_TYPE;
46
    }
47
48 48
    private function generateMutationArguments(MutationConfigObject $config, ReadObject $source, string $path): array
49
    {
50 48
        $arguments = [];
51 48
        foreach ($source as $sourceKey => $sourceValue) {
0 ignored issues
show
Bug introduced by
The expression $source of type object<Softonic\GraphQL\Query\ReadObject> is not traversable.
Loading history...
52 48
            if ($sourceValue instanceof ReadObject) {
53 40
                $childPath = $this->createPathFromParent($path, $sourceKey);
54
55 40
                $arguments[$sourceKey] = $this->mutateChild($config->children[$sourceKey], $sourceValue, $childPath);
56
            } else {
57 48
                $arguments[$sourceKey] = $sourceValue;
58
            }
59
        }
60
61 48
        return $arguments;
62
    }
63
64 40
    private function createPathFromParent(string $parent, string $child): string
65
    {
66 40
        return ('.' === $parent) ? ".{$child}" : "{$parent}.{$child}";
67
    }
68
69 40
    private function mutateChild(MutationConfigObject $config, ReadObject $source, string $path): MutationObject
70
    {
71 40
        if (is_null($config->linksTo)) {
72 40
            $arguments = [];
73 40
            foreach ($config->children as $key => $childConfig) {
74 40
                $childArguments = [];
75 40
                foreach ($source as $sourceKey => $sourceValue) {
0 ignored issues
show
Bug introduced by
The expression $source of type object<Softonic\GraphQL\Query\ReadObject> is not traversable.
Loading history...
76 38
                    $childChildrenType      = $this->getMutationType($childConfig, $sourceValue);
77 38
                    $childChildrenArguments = $this->generateMutationArguments($childConfig, $sourceValue, $path);
78
79 38
                    $childArguments[$sourceKey] = new $childChildrenType(
80 38
                        $childChildrenArguments,
81 38
                        $childConfig->children
82
                    );
83
                }
84
85 40
                $childType = $this->getMutationType($childConfig, $source);
86
87 40
                $arguments[$key] = new $childType($childArguments, $childConfig->children);
88
            }
89
        }
90
91 40
        $type = $this->getMutationType($config, $source);
92
93 40
        return new $type($arguments, $config->children);
0 ignored issues
show
Bug introduced by
The variable $arguments does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
94
    }
95
}
96