Passed
Push — master ( c03be5...0b51c9 )
by Mr
01:52
created

MapTrait   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 98.7%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 147
ccs 76
cts 77
cp 0.987
rs 10
c 0
b 0
f 0
wmc 24

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 3 1
A get() 0 11 3
A count() 0 4 1
A isEmpty() 0 4 1
A equals() 0 4 1
A assertValidType() 0 8 3
A keys() 0 4 1
A getIterator() 0 3 1
A __get() 0 3 1
A last() 0 5 1
A has() 0 4 1
A with() 0 7 1
A first() 0 5 1
A assertValidMap() 0 6 1
A assertInitialized() 0 3 1
A init() 0 10 2
A without() 0 7 1
A assertValidKey() 0 3 1
A unwrap() 0 4 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/data-structure project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\DataStructure;
10
11
use Daikon\Interop\Assert;
12
use Daikon\Interop\Assertion;
13
use Ds\Map;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Daikon\DataStructure\Map. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
15
trait MapTrait
16
{
17
    protected Map $compositeMap;
18
19 24
    protected function init(iterable $values): void
20
    {
21 24
        Assertion::false(isset($this->compositeMap), 'Cannot reinitialize map.');
22
23 24
        foreach ($values as $key => $value) {
24 23
            $this->assertValidKey($key);
25 22
            $this->assertValidType($value);
26
        }
27
28 22
        $this->compositeMap = new Map($values);
29 22
    }
30
31 1
    public function keys(): array
32
    {
33 1
        $this->assertInitialized();
34 1
        return $this->compositeMap->keys()->toArray();
35
    }
36
37 7
    public function has(string $key): bool
38
    {
39 7
        $this->assertInitialized();
40 7
        return $this->compositeMap->hasKey($key);
41
    }
42
43 7
    public function get(string $key, $default = null)
44
    {
45 7
        $this->assertInitialized();
46 7
        if (func_num_args() === 1) {
47 4
            Assertion::satisfy($key, [$this, 'has'], "Key '$key' not found and no default provided.");
48 2
            return $this->compositeMap->get($key);
49
        } else {
50 3
            if (!is_null($default)) {
51 2
                $this->assertValidType($default);
52
            }
53 2
            return $this->compositeMap->get($key, $default);
54
        }
55
    }
56
57 2
    public function with(string $key, $value): self
58
    {
59 2
        $this->assertInitialized();
60 2
        $this->assertValidType($value);
61 1
        $copy = clone $this;
62 1
        $copy->compositeMap->put($key, $value);
63 1
        return $copy;
64
    }
65
66 2
    public function without(string $key): self
67
    {
68 2
        $this->assertInitialized();
69 2
        Assertion::satisfy($key, [$this, 'has'], "Key '$key' not found.");
70 1
        $copy = clone $this;
71 1
        $copy->compositeMap->remove($key);
72 1
        return $copy;
73
    }
74
75 1
    public function first()
76
    {
77 1
        $this->assertInitialized();
78
        /** @psalm-suppress MissingPropertyType */
79 1
        return $this->compositeMap->first()->value;
80
    }
81
82 1
    public function last()
83
    {
84 1
        $this->assertInitialized();
85
        /** @psalm-suppress MissingPropertyType */
86 1
        return $this->compositeMap->last()->value;
87
    }
88
89 1
    public function isEmpty(): bool
90
    {
91 1
        $this->assertInitialized();
92 1
        return $this->compositeMap->isEmpty();
93
    }
94
95
    /** @param static $map */
96 1
    public function equals($map): bool
97
    {
98 1
        $this->assertValidMap($map);
99 1
        return $this->unwrap() === $map->unwrap();
100
    }
101
102 2
    public function count(): int
103
    {
104 2
        $this->assertInitialized();
105 2
        return $this->compositeMap->count();
106
    }
107
108 6
    public function unwrap(): array
109
    {
110 6
        $this->assertInitialized();
111 6
        return $this->compositeMap->toArray();
112
    }
113
114
    /** @psalm-suppress ImplementedReturnTypeMismatch */
115 2
    public function getIterator(): Map
116
    {
117 2
        return $this->compositeMap;
118
    }
119
120 20
    protected function assertInitialized(): void
121
    {
122 20
        Assertion::true(isset($this->compositeMap), 'Map is not initialized.');
123 20
    }
124
125
    /** @param mixed $key */
126 23
    protected function assertValidKey($key): void
127
    {
128 23
        Assert::that($key, 'Key must be a valid string.')->string()->notEmpty();
129 22
    }
130
131
    /** @param mixed $value */
132 22
    protected function assertValidType($value): void
133
    {
134 22
        Assertion::true(
135 22
            is_array($value) || is_scalar($value),
136
            sprintf(
137 22
                "Invalid value type given to '%s', expected scalar or array but was given '%s'.",
138 22
                static::class,
139 22
                is_object($value) ? get_class($value) : @gettype($value)
140
            )
141
        );
142 21
    }
143
144
    /** @param mixed $map */
145 1
    protected function assertValidMap($map): void
146
    {
147 1
        Assertion::isInstanceOf(
148 1
            $map,
149 1
            static::class,
150 1
            sprintf("Map operation must be on same type as '%s'.", static::class)
151
        );
152 1
    }
153
154 2
    public function __get(string $key)
155
    {
156 2
        return $this->get($key);
157
    }
158
159 3
    public function __clone()
160
    {
161 3
        $this->compositeMap = clone $this->compositeMap;
162 3
    }
163
}
164