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

Item::__get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 5
cp 0.6
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3.576
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 46
    public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false)
18
    {
19 46
        $this->arguments  = $arguments;
20 46
        $this->config     = $config;
21 46
        $this->hasChanged = $hasChanged;
22 46
    }
23
24 32
    public function __get(string $key)
25
    {
26 32
        if (is_null($this->arguments[$key]) && array_key_exists($key, $this->config)) {
27
            $mutationTypeClass = $this->config[$key]->type;
28
29
            $this->arguments[$key] = new $mutationTypeClass([], $this->config[$key]->children);
30
        }
31
32 32
        return $this->arguments[$key];
33
    }
34
35 24
    public function __set(string $key, $value): void
36
    {
37 24
        if ($this->arguments[$key] !== $value) {
38 22
            $this->arguments[$key] = $value;
39
40 22
            $this->hasChanged = true;
41
        }
42 24
    }
43
44 18
    public function set(array $data): void
45
    {
46 18
        foreach ($data as $key => $value) {
47 18
            $this->{$key} = $value;
48
        }
49 18
    }
50
51 32
    public function jsonSerialize(): array
52
    {
53 32
        if (!$this->hasChanged()) {
54 6
            return [];
55
        }
56
57 28
        $item = [];
58 28
        foreach ($this->arguments as $key => $value) {
59 28
            $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...
60
        }
61
62 28
        return $item;
63
    }
64
}
65