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

Collection::__get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Softonic\GraphQL\DataObjects\Query;
4
5
use Softonic\GraphQL\DataObjects\AbstractCollection;
6
use Softonic\GraphQL\DataObjects\Interfaces\DataObject;
7
use Softonic\GraphQL\Exceptions\InaccessibleArgumentException;
8
use Softonic\GraphQL\Traits\GqlIterator;
9
10
class Collection extends AbstractCollection implements QueryObject
11
{
12
    use GqlIterator;
13
14
    public function __get(string $key): Collection
15
    {
16
        if (empty($this->arguments)) {
17
            throw InaccessibleArgumentException::fromEmptyArguments($key);
18
        }
19
20
        $items = [];
21
        foreach ($this->arguments as $argument) {
22
            $items[] = $argument->{$key};
23
        }
24
25
        return new Collection($items);
26
    }
27
28
    public function has(string $key): bool
29
    {
30
        foreach ($this->arguments as $argument) {
31
            if ($argument->has($key)) {
32
                return true;
33
            }
34
        }
35
36
        return false;
37
    }
38
39 12
    protected function buildFilteredCollection($data)
40
    {
41 12
        return new Collection($data);
42
    }
43
44
    public function jsonSerialize(): array
45
    {
46
        if (!$this->hasChildren()) {
47
            return [];
48
        }
49
50
        $items = [];
51
        foreach ($this->arguments as $item) {
52
            $items[] = $item->jsonSerialize();
53
        }
54
55
        return $items;
56
    }
57
58
    public function hasChildren(): bool
59
    {
60
        foreach ($this->arguments as $argument) {
61
            if ($argument instanceof DataObject) {
62
                return true;
63
            }
64
        }
65
66
        return false;
67
    }
68
69
    public function toArray(): array
70
    {
71
        $item = [];
72
        foreach ($this->arguments as $key => $value) {
73
            $item[$key] = $value instanceof \JsonSerializable ? $value->toArray() : $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...
74
        }
75
76
        return $item;
77
    }
78
}
79