Passed
Push — master ( 6cac1c...80b4dd )
by Mr
07:01
created

MapTrait::empty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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