Completed
Pull Request — master (#29)
by
unknown
01:32
created

Item   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 68
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __set() 0 10 3
A setValue() 0 6 1
A set() 0 6 2
A jsonSerialize() 0 13 4
A __get() 0 11 5
1
<?php
2
3
namespace Softonic\GraphQL\Mutation;
4
5
use Softonic\GraphQL\Mutation\Traits\MutationObjectHandler;
6
7
class Item implements MutationObject, \JsonSerializable
8
{
9
    use MutationObjectHandler;
10
11
    private $arguments;
12
13
    private $config;
14
15
    private $hasChanged = false;
16
17 68
    public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false)
18
    {
19 68
        $this->arguments  = $arguments;
20 68
        $this->config     = $config;
21 68
        $this->hasChanged = $hasChanged;
22 68
    }
23
24 48
    public function __get(string $key)
25
    {
26 48
        if ((!array_key_exists($key, $this->arguments) || ($this->arguments[$key] === null))
27 48
            && array_key_exists($key, $this->config)) {
28 8
            $mutationTypeClass = $this->config[$key]->type;
29
30 8
            $this->arguments[$key] = new $mutationTypeClass([], $this->config[$key]->children);
31
        }
32
33 48
        return array_key_exists($key, $this->arguments) ? $this->arguments[$key] : null;
34
    }
35
36 32
    public function __set(string $key, $value): void
37
    {
38 32
        if (array_key_exists($key, $this->arguments)) {
39 32
            if ($this->arguments[$key] !== $value) {
40 32
                $this->setValue($key, $value);
41
            }
42
        } else {
43 4
            $this->setValue($key, $value);
44
        }
45 32
    }
46
47 30
    private function setValue(string $key, $value): void
48
    {
49 30
        $this->arguments[$key] = $value;
50
51 30
        $this->hasChanged = true;
52 30
    }
53
54 24
    public function set(array $data): void
55
    {
56 24
        foreach ($data as $key => $value) {
57 24
            $this->{$key} = $value;
58
        }
59 24
    }
60
61 46
    public function jsonSerialize(): array
62
    {
63 46
        if (!$this->hasChanged()) {
64 6
            return [];
65
        }
66
67 42
        $item = [];
68 42
        foreach ($this->arguments as $key => $value) {
69 42
            $item[$key] = $value instanceof \JsonSerializable ? $value->jsonSerialize() : $value;
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
70
        }
71
72 42
        return $item;
73
    }
74
}
75