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

FilteredCollection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 91
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __get() 0 9 2
A set() 0 6 2
A filter() 0 15 3
A areAllArgumentsCollections() 0 4 2
A filterItems() 0 14 3
A jsonSerialize() 0 11 3
1
<?php
2
3
namespace Softonic\GraphQL\Mutation;
4
5
use Softonic\GraphQL\Mutation\Traits\MutationObjectHandler;
6
7
class FilteredCollection implements MutationObject, \JsonSerializable
8
{
9
    use MutationObjectHandler;
10
11
    /**
12
     * @var array<Item>
13
     */
14
    protected $arguments = [];
15
16
    /**
17
     * @var array
18
     */
19
    protected $config;
20
21
    /**
22
     * @var bool
23
     */
24
    private $hasChanged = false;
25
26 58
    public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false)
27
    {
28 58
        $this->arguments  = $arguments;
29 58
        $this->config     = $config;
30 58
        $this->hasChanged = $hasChanged;
31 58
    }
32
33 20
    public function __get(string $key): Collection
34
    {
35 20
        $items = [];
36 20
        foreach ($this->arguments as $argument) {
37 20
            $items[] = $argument->{$key};
38
        }
39
40 20
        return new Collection($items, $this->config[$key]->children);
41
    }
42
43 24
    public function set(array $data): void
44
    {
45 24
        foreach ($this->arguments as $argument) {
46 24
            $argument->set($data);
47
        }
48 24
    }
49
50 22
    public function filter(array $filters): FilteredCollection
51
    {
52 22
        $filteredData = [];
53 22
        if ($this->areAllArgumentsCollections()) {
54 8
            foreach ($this->arguments as $argument) {
55 8
                $filteredItems = $this->filterItems($argument->arguments, $filters);
0 ignored issues
show
Documentation introduced by
The property $arguments is declared private in Softonic\GraphQL\Mutation\Item. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
57 8
                $filteredData[] = new FilteredCollection($filteredItems, $this->config);
58
            }
59
        } else {
60 16
            $filteredData = $this->filterItems($this->arguments, $filters);
61
        }
62
63 22
        return new FilteredCollection($filteredData, $this->config);
64
    }
65
66 22
    private function areAllArgumentsCollections(): bool
67
    {
68 22
        return (!empty($this->arguments[0]) && $this->arguments[0] instanceof Collection);
69
    }
70
71
    private function filterItems(array $arguments, array $filters): array
72
    {
73 22
        $filteredItems = array_filter($arguments, function ($item) use ($filters) {
74 22
            foreach ($filters as $filterKey => $filterValue) {
75 22
                if (!($item->{$filterKey} == $filterValue)) {
76 22
                    return false;
77
                }
78
            }
79
80 22
            return true;
81 22
        });
82
83 22
        return array_values($filteredItems);
84
    }
85
86 36
    public function jsonSerialize(): array
87
    {
88 36
        $items = [];
89 36
        foreach ($this->arguments as $item) {
90 36
            if ($item->hasChanged()) {
91 36
                $items[] = $item->jsonSerialize();
92
            }
93
        }
94
95 36
        return $items;
96
    }
97
}
98