Completed
Pull Request — master (#42)
by Jose Manuel
01:40
created

Item   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __get() 0 13 6
A __set() 0 10 3
A setValue() 0 6 1
A __unset() 0 6 1
A has() 0 17 3
A exists() 0 4 1
A set() 0 6 2
B jsonSerialize() 0 23 7
1
<?php
2
3
namespace Softonic\GraphQL\Mutation;
4
5
use Softonic\GraphQL\Config\MutationTypeConfig;
6
use Softonic\GraphQL\Mutation\Traits\MutationObjectHandler;
7
8
class Item implements MutationObject, \JsonSerializable
9
{
10
    use MutationObjectHandler;
11
12
    /**
13
     * @var array
14
     */
15
    private $arguments;
16
17
    /**
18
     * @var array
19
     */
20
    private $config;
21
22
    /**
23
     * @var bool
24
     */
25
    private $hasChanged = false;
26
27 100
    public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false)
28
    {
29 100
        $this->arguments  = $arguments;
30 100
        $this->config     = $config;
31 100
        $this->hasChanged = $hasChanged;
32 100
    }
33
34 72
    public function __get(string $key)
35
    {
36 72
        if ((!array_key_exists($key, $this->arguments) || ($this->arguments[$key] === null))
37 72
            && array_key_exists($key, $this->config)
38 72
            && ($this->config[$key]->type !== MutationTypeConfig::SCALAR_DATA_TYPE)
39
        ) {
40 8
            $mutationTypeClass = $this->config[$key]->type;
41
42 8
            $this->arguments[$key] = new $mutationTypeClass([], $this->config[$key]->children);
43
        }
44
45 72
        return array_key_exists($key, $this->arguments) ? $this->arguments[$key] : null;
46
    }
47
48 32
    public function __set(string $key, $value): void
49
    {
50 32
        if (array_key_exists($key, $this->arguments)) {
51 32
            if ($this->arguments[$key] !== $value) {
52 32
                $this->setValue($key, $value);
53
            }
54
        } else {
55 4
            $this->setValue($key, $value);
56
        }
57 32
    }
58
59 30
    private function setValue(string $key, $value): void
60
    {
61 30
        $this->arguments[$key] = $value;
62
63 30
        $this->hasChanged = true;
64 30
    }
65
66 6
    public function __unset(string $key): void
67
    {
68 6
        unset($this->arguments[$key]);
69
70 6
        $this->hasChanged = true;
71 6
    }
72
73 4
    public function has(string $key): bool
74
    {
75 4
        $keyPath  = explode('.', $key);
76 4
        $firstKey = array_shift($keyPath);
77
78 4
        if (!array_key_exists($firstKey, $this->arguments)) {
79 4
            return false;
80
        }
81
82 4
        if (empty($keyPath)) {
83 4
            return true;
84
        }
85
86 2
        $nextKey = implode('.', $keyPath);
87
88 2
        return $this->arguments[$firstKey]->has($nextKey);
89
    }
90
91 2
    public function exists(array $data): bool
92
    {
93 2
        return $data === $this->arguments;
94
    }
95
96 24
    public function set(array $data): void
97
    {
98 24
        foreach ($data as $key => $value) {
99 24
            $this->{$key} = $value;
100
        }
101 24
    }
102
103 56
    public function jsonSerialize(): array
104
    {
105 56
        if (!$this->hasChanged()) {
106 6
            return [];
107
        }
108
109 52
        $item = [];
110 52
        foreach ($this->arguments as $key => $value) {
111 52
            if ($value instanceof FilteredCollection && !$value->hasChildren()) {
112 2
                continue;
113
            }
114
115 52
            if ($value instanceof \JsonSerializable) {
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...
116 52
                if (!empty($valueSerialized = $value->jsonSerialize())) {
117 52
                    $item[$key] = $valueSerialized;
118
                }
119
            } else {
120 52
                $item[$key] = $value;
121
            }
122
        }
123
124 52
        return $item;
125
    }
126
}
127