Completed
Pull Request — master (#43)
by Jose Manuel
02:54
created

Mutation   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 117
ccs 53
cts 53
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 36 4
B generateMutationArguments() 0 30 6
A createPathFromParent() 0 4 2
A mutateChild() 0 21 4
1
<?php
2
3
namespace Softonic\GraphQL;
4
5
use Softonic\GraphQL\Config\MutationTypeConfig;
6
use Softonic\GraphQL\DataObjects\Mutation\Collection as MutationCollection;
7
use Softonic\GraphQL\DataObjects\Mutation\Item as MutationItem;
8
use Softonic\GraphQL\DataObjects\Mutation\MutationObject;
9
use Softonic\GraphQL\DataObjects\Query\Collection as QueryCollection;
10
use Softonic\GraphQL\DataObjects\Query\Item as QueryItem;
11
use Softonic\GraphQL\DataObjects\Query\QueryObject;
12
13
class Mutation
14
{
15
    const SOURCE_ROOT_PATH = '.';
16
17
    /**
18
     * @var array
19
     */
20
    private static $config;
21
22
    /**
23
     * @var bool
24
     */
25
    private static $hasChanged;
26
27
    /**
28
     * @var MutationTypeConfig
29
     */
30
    private static $mutationTypeConfig;
31
32
    /**
33
     * @param array<MutationTypeConfig> $config
34
     */
35 98
    public static function build(array $config, QueryObject $source, bool $fromMutation = false): MutationObject
36
    {
37 98
        static::$config     = $config;
0 ignored issues
show
Bug introduced by
Since $config is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $config to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
38 98
        static::$hasChanged = $fromMutation;
0 ignored issues
show
Bug introduced by
Since $hasChanged is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hasChanged to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
39
40 98
        $mutationVariables = [];
41 98
        foreach (static::$config as $variableName => $mutationTypeConfig) {
0 ignored issues
show
Bug introduced by
Since $config is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $config to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
42 96
            static::$mutationTypeConfig = $mutationTypeConfig;
0 ignored issues
show
Bug introduced by
Since $mutationTypeConfig is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mutationTypeConfig to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
43 96
            $path                       = self::SOURCE_ROOT_PATH;
44 96
            $config                     = $mutationTypeConfig->get($path);
45 96
            if ($config->type === MutationCollection::class) {
46 6
                $arguments = [];
47 6
                foreach ($source as $sourceItem) {
0 ignored issues
show
Bug introduced by
The expression $source of type object<Softonic\GraphQL\...ects\Query\QueryObject> is not traversable.
Loading history...
48 6
                    $mutationItemArguments = static::generateMutationArguments($sourceItem, $path);
0 ignored issues
show
Bug introduced by
Since generateMutationArguments() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of generateMutationArguments() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
49
50 6
                    $arguments[] = new MutationItem($mutationItemArguments, $config->children, static::$hasChanged);
0 ignored issues
show
Bug introduced by
Since $hasChanged is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hasChanged to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
51
                }
52
53 6
                $mutationVariables[$variableName] = new $config->type(
54 6
                    $arguments,
55 6
                    $config->children,
56 6
                    static::$hasChanged
0 ignored issues
show
Bug introduced by
Since $hasChanged is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hasChanged to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
57
                );
58
            } else {
59 90
                $arguments = static::generateMutationArguments($source, $path);
0 ignored issues
show
Bug introduced by
Since generateMutationArguments() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of generateMutationArguments() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
Compatibility introduced by
$source of type object<Softonic\GraphQL\...ects\Query\QueryObject> is not a sub-type of object<Softonic\GraphQL\DataObjects\Query\Item>. It seems like you assume a concrete implementation of the interface Softonic\GraphQL\DataObjects\Query\QueryObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60
61 90
                $mutationVariables[$variableName] = new $config->type(
62 90
                    $arguments,
63 90
                    $config->children,
64 90
                    static::$hasChanged
0 ignored issues
show
Bug introduced by
Since $hasChanged is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hasChanged to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
65
                );
66
            }
67
        }
68
69 98
        return new MutationItem($mutationVariables, static::$config, static::$hasChanged);
0 ignored issues
show
Bug introduced by
Since $config is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $config to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Bug introduced by
Since $hasChanged is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hasChanged to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
70
    }
71
72 96
    private static function generateMutationArguments(QueryItem $source, string $path): array
73
    {
74 96
        $arguments = [];
75 96
        foreach ($source as $sourceKey => $sourceValue) {
76 96
            $childPath   = static::createPathFromParent($path, $sourceKey);
0 ignored issues
show
Bug introduced by
Since createPathFromParent() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createPathFromParent() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
77 96
            $childConfig = static::$mutationTypeConfig->get($childPath);
0 ignored issues
show
Bug introduced by
Since $mutationTypeConfig is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mutationTypeConfig to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
78
79 96
            if (is_null($childConfig)) {
80 4
                break;
81
            }
82
83 96
            if ($sourceValue instanceof QueryObject) {
84 74
                if ($sourceValue instanceof QueryCollection) {
85 74
                    $arguments[$sourceKey] = static::mutateChild($childConfig, $sourceValue, $childPath);
0 ignored issues
show
Bug introduced by
Since mutateChild() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of mutateChild() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
86
                } else {
87 4
                    $mutationItemArguments = static::generateMutationArguments($sourceValue, $childPath);
0 ignored issues
show
Bug introduced by
Since generateMutationArguments() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of generateMutationArguments() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
Compatibility introduced by
$sourceValue of type object<Softonic\GraphQL\...ects\Query\QueryObject> is not a sub-type of object<Softonic\GraphQL\DataObjects\Query\Item>. It seems like you assume a concrete implementation of the interface Softonic\GraphQL\DataObjects\Query\QueryObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
88
89 4
                    $arguments[$sourceKey] = new MutationItem(
90 4
                        $mutationItemArguments,
91 4
                        $childConfig->children,
92 74
                        static::$hasChanged
0 ignored issues
show
Bug introduced by
Since $hasChanged is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hasChanged to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
93
                    );
94
                }
95 96
            } elseif ($childConfig->type === MutationTypeConfig::SCALAR_DATA_TYPE) {
96 96
                $arguments[$sourceKey] = $sourceValue;
97
            }
98
        }
99
100 96
        return $arguments;
101
    }
102
103 96
    private static function createPathFromParent(string $parent, string $child): string
104
    {
105 96
        return ('.' === $parent) ? ".{$child}" : "{$parent}.{$child}";
106
    }
107
108 74
    private static function mutateChild(
109
        MutationTypeConfig $config,
110
        QueryCollection $sourceCollection,
111
        string $path
112
    ): MutationObject {
113 74
        $arguments = [];
114 74
        if (is_null($config->linksTo)) {
115 66
            foreach ($config->children as $key => $childConfig) {
116 66
                $childPath       = static::createPathFromParent($path, $key);
0 ignored issues
show
Bug introduced by
Since createPathFromParent() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createPathFromParent() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
117 66
                $arguments[$key] = static::mutateChild($childConfig, $sourceCollection, $childPath);
0 ignored issues
show
Bug introduced by
Since mutateChild() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of mutateChild() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
118
            }
119
        } else {
120 74
            foreach ($sourceCollection as $sourceItem) {
121 72
                $itemArguments = static::generateMutationArguments($sourceItem, $path);
0 ignored issues
show
Bug introduced by
Since generateMutationArguments() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of generateMutationArguments() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
122
123 72
                $arguments[] = new MutationItem($itemArguments, $config->children, static::$hasChanged);
0 ignored issues
show
Bug introduced by
Since $hasChanged is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hasChanged to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
124
            }
125
        }
126
127 74
        return new $config->type($arguments, $config->children, static::$hasChanged);
0 ignored issues
show
Bug introduced by
Since $hasChanged is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $hasChanged to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
128
    }
129
}
130