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

AbstractItem   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 67
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A has() 0 17 3
A __get() 0 4 2
A __set() 0 4 1
A exists() 0 4 1
A jsonSerialize() 0 19 6
1
<?php
2
3
namespace Softonic\GraphQL\DataObjects;
4
5
use Softonic\GraphQL\DataObjects\Interfaces\DataObject;
6
use Softonic\GraphQL\DataObjects\Mutation\FilteredCollection;
7
use Softonic\GraphQL\DataObjects\Traits\ObjectHandler;
8
9
class AbstractItem implements DataObject, \JsonSerializable
10
{
11
    use ObjectHandler;
12
13
    /**
14
     * @var array
15
     */
16
    protected $arguments;
17
18 124
    public function __construct(array $arguments)
19
    {
20 124
        $this->arguments = $arguments;
21 124
    }
22
23 4
    public function has(string $key): bool
24
    {
25 4
        $keyPath  = explode('.', $key);
26 4
        $firstKey = array_shift($keyPath);
27
28 4
        if (!array_key_exists($firstKey, $this->arguments)) {
29 4
            return false;
30
        }
31
32 4
        if (empty($keyPath)) {
33 4
            return true;
34
        }
35
36 2
        $nextKey = implode('.', $keyPath);
37
38 2
        return $this->arguments[$firstKey]->has($nextKey);
39
    }
40
41 84
    public function __get(string $key)
42
    {
43 84
        return array_key_exists($key, $this->arguments) ? $this->arguments[$key] : null;
44
    }
45
46 34
    public function __set(string $key, $value): void
47
    {
48 34
        $this->arguments[$key] = $value;
49 34
    }
50
51 2
    public function exists(array $data): bool
52
    {
53 2
        return $data === $this->arguments;
54
    }
55
56 52
    public function jsonSerialize()
57
    {
58 52
        $item = [];
59 52
        foreach ($this->arguments as $key => $value) {
60 52
            if ($value instanceof FilteredCollection && !$value->hasChildren()) {
61 2
                continue;
62
            }
63
64 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...
65 52
                if (!empty($valueSerialized = $value->jsonSerialize())) {
66 52
                    $item[$key] = $valueSerialized;
67
                }
68
            } else {
69 52
                $item[$key] = $value;
70
            }
71
        }
72
73 52
        return $item;
74
    }
75
}
76