BaseCollection::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Arrayable;
6
7
use ArrayIterator;
8
use Traversable;
9
use function array_key_exists;
10
use function array_map;
11
use function count;
12
use function json_encode;
13
use const JSON_THROW_ON_ERROR;
14
15
/**
16
 * @template TItemKey of array-key
17
 * @template TItemValue
18
 * @template TKey of array-key
19
 * @template TValue of \Inspirum\Arrayable\Arrayable<TItemKey,TItemValue>
20
 * @implements \Inspirum\Arrayable\Collection<TItemKey,TItemValue,TKey,TValue>
21
 */
22
abstract class BaseCollection implements Collection
23
{
24
    /**
25
     * @param array<TKey,TValue> $items
26
     */
27 3
    public function __construct(
28
        protected array $items = [],
29
    ) {
30 3
    }
31
32
    /**
33
     * @param TKey $offset
0 ignored issues
show
Bug introduced by
The type Inspirum\Arrayable\TKey was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
     */
35 1
    public function offsetExists(mixed $offset): bool
36
    {
37 1
        return array_key_exists($offset, $this->items);
38
    }
39
40
    /**
41
     * @param TKey $offset
42
     *
43
     * @return TValue
0 ignored issues
show
Bug introduced by
The type Inspirum\Arrayable\TValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
     */
45 1
    public function offsetGet(mixed $offset): mixed
46
    {
47 1
        return $this->items[$offset];
48
    }
49
50
    /**
51
     * @param TKey $offset
52
     * @param TValue $value
53
     */
54 1
    public function offsetSet(mixed $offset, mixed $value): void
55
    {
56 1
        $this->items[$offset] = $value;
57
    }
58
59
    /**
60
     * @param TValue $value
61
     */
62 1
    public function offsetAdd(mixed $value): void
63
    {
64 1
        $this->items[] = $value;
65
    }
66
67
    /**
68
     * @param TKey $offset
69
     */
70 1
    public function offsetUnset(mixed $offset): void
71
    {
72 1
        unset($this->items[$offset]);
73
    }
74
75 1
    public function count(): int
76
    {
77 1
        return count($this->items);
78
    }
79
80
    /**
81
     * @return \Traversable<TKey,TValue>
82
     */
83 1
    public function getIterator(): Traversable
84
    {
85 1
        return new ArrayIterator($this->items);
86
    }
87
88
    /**
89
     * @return array<TKey,TValue>
90
     */
91 1
    public function getItems(): array
92
    {
93 1
        return $this->items;
94
    }
95
96
    /**
97
     * @return array<TKey,array<TItemKey,TItemValue>>
98
     */
99 2
    public function __toArray(): array
100
    {
101 2
        return array_map(static fn (Arrayable $item): array => $item->__toArray(), $this->items);
102
    }
103
104
    /**
105
     * @return array<TKey,array<TItemKey,TItemValue>>
106
     */
107 2
    public function toArray(): array
108
    {
109 2
        return $this->__toArray();
110
    }
111
112
    /**
113
     * @return array<TKey,array<TItemKey,TItemValue>>
114
     */
115 2
    public function jsonSerialize(): array
116
    {
117 2
        return $this->__toArray();
118
    }
119
120
    /**
121
     * @throws \JsonException
122
     */
123 2
    public function __toString(): string
124
    {
125 2
        return json_encode($this->jsonSerialize(), JSON_THROW_ON_ERROR);
126
    }
127
}
128