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

FilteredCollection::filterItems()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 1
nop 2
crap 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 56
    public function __construct(array $arguments = [], array $config = [], bool $hasChanged = false)
27
    {
28 56
        $this->arguments  = $arguments;
29 56
        $this->config     = $config;
30 56
        $this->hasChanged = $hasChanged;
31 56
    }
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 22
    public function set(array $data): void
44
    {
45 22
        foreach ($this->arguments as $argument) {
46 22
            $argument->set($data);
47
        }
48 22
    }
49
50 20
    public function filter(array $filters): FilteredCollection
51
    {
52 20
        $filteredData = [];
53 20
        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 14
            $filteredData = $this->filterItems($this->arguments, $filters);
61
        }
62
63 20
        return new FilteredCollection($filteredData, $this->config);
64
    }
65
66 20
    private function areAllArgumentsCollections(): bool
67
    {
68 20
        return (!empty($this->arguments[0]) && $this->arguments[0] instanceof Collection);
69
    }
70
71
    private function filterItems(array $arguments, array $filters): array
72
    {
73 20
        $filteredItems = array_filter($arguments, function ($item) use ($filters) {
74 20
            foreach ($filters as $filterKey => $filterValue) {
75 20
                if (!($item->{$filterKey} == $filterValue)) {
76 20
                    return false;
77
                }
78
            }
79
80 20
            return true;
81 20
        });
82
83 20
        return array_values($filteredItems);
84
    }
85
86 34
    public function jsonSerialize(): array
87
    {
88 34
        $items = [];
89 34
        foreach ($this->arguments as $item) {
90 34
            if ($item->hasChanged()) {
91 34
                $items[] = $item->jsonSerialize();
92
            }
93
        }
94
95 34
        return $items;
96
    }
97
}
98