• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHP-Alchemist / coreFiles / 15423302022

03 Jun 2025 04:58PM UTC coverage: 92.411%. First build
15423302022

Pull #9

github

druid628
CS Fixes
Pull Request #9: [Release] v3.0.0

144 of 175 new or added lines in 16 files covered. (82.29%)

548 of 593 relevant lines covered (92.41%)

4.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.51
/src/Abstract/AbstractKeyValuePair.php
1
<?php
2

3
namespace PHPAlchemist\Abstract;
4

5
use PHPAlchemist\Contract\KeyValuePairInterface;
6
use PHPAlchemist\Exception\InvalidKeyTypeException;
7
use PHPAlchemist\Exception\UnmatchedClassException;
8
use PHPAlchemist\Exception\UnmatchedVersionException;
9

10
/**
11
 * Abstract class for KeyValue Pair.
12
 */
13
abstract class AbstractKeyValuePair implements KeyValuePairInterface
14
{
15
    public static $serializeVersion = 1;
16

17
    /** @var int position sentinel variable */
18
    protected $position;
19

20
    /** @var array */
21
    protected $keys = [];
22

23
    /** @var array */
24
    protected $values = [];
25

26
    public function __construct($data = [])
14✔
27
    {
28
        $this->setData($data);
14✔
29
        $this->position = 0;
14✔
30
    }
31

32
    /**
33
     * @param $key
34
     * @param $value
35
     *
36
     * @throws InvalidKeyTypeException
37
     *
38
     * @return $this
39
     */
40
    public function add($key, $value) : KeyValuePairInterface
14✔
41
    {
42
        $this->offsetSet($key, $value);
14✔
43

44
        return $this;
13✔
45
    }
46

47
    /**
48
     * @param $key
49
     * @param $value
50
     *
51
     * @throws InvalidKeyTypeException
52
     *
53
     * @return $this
54
     */
55
    public function set($key, $value) : KeyValuePairInterface
1✔
56
    {
57
        $this->offsetSet($key, $value);
1✔
58

59
        return $this;
1✔
60
    }
61

62
    public function get($key) : mixed
1✔
63
    {
64
        return $this->offsetGet($key);
1✔
65
    }
66

67
    public function getKeys() : array
1✔
68
    {
69
        return $this->keys;
1✔
70
    }
71

72
    public function getValues() : array
1✔
73
    {
74
        return $this->values;
1✔
75
    }
76

77
    public function count() : int
1✔
78
    {
79
        return count($this->keys);
1✔
80
    }
81

82
    // Iterator
83
    public function rewind() : void
1✔
84
    {
85
        $this->position = 0;
1✔
86
    }
87

88
    public function valid() : bool
3✔
89
    {
90
        return isset($this->keys[$this->position]);
3✔
91
    }
92

93
    public function key() : mixed
2✔
94
    {
95
        return array_keys($this->keys)[$this->position];
2✔
96
    }
97

98
    public function next() : void
4✔
99
    {
100
        $this->position++;
4✔
101
    }
102

103
    // not part of Iterator
104
    public function prev() : void
1✔
105
    {
106
        $this->position--;
1✔
107
    }
108

109
    public function current() : mixed
3✔
110
    {
111
        return ($this->valid()) ? array_values($this->values)[$this->position] : false;
3✔
112
    }
113

114
    // ArrayAccess
115

116
    /**
117
     * @param mixed $offset
118
     */
119
    public function offsetUnset($offset) : void
1✔
120
    {
121
        $offsetPosition = $this->getOffsetPosition($offset);
1✔
122

123
        unset($this->keys[$offsetPosition],
1✔
124
            $this->values[$offsetPosition]
1✔
125
        );
1✔
126
    }
127

128
    /**
129
     * Offset to set.
130
     *
131
     * @link https://php.net/manual/en/arrayaccess.offsetset.php
132
     *
133
     * @param mixed $offset The offset to assign the value to.
134
     * @param mixed $value  The value to set.
135
     *
136
     * @since 5.0.0
137
     *
138
     * @throws InvalidKeyTypeException
139
     */
140
    public function offsetSet($offset, $value) : void
14✔
141
    {
142
        if (!$this->validateKey($offset)) {
14✔
143
            throw new InvalidKeyTypeException(sprintf('Invalid Key type (%s) for Dictionary', gettype($offset)));
1✔
144
        }
145

146
        if (!$this->offsetExists($offset)) {
13✔
147
            $this->keys[] = $offset;
13✔
148
        }
149

150
        $position                = $this->getOffsetPosition($offset);
13✔
151
        $this->values[$position] = $value;
13✔
152
    }
153

154
    /**
155
     * Get raw data in PHP Array.
156
     *
157
     * @return array
158
     */
159
    public function getData() : array
6✔
160
    {
161
        return array_combine($this->keys, $this->values);
6✔
162
    }
163

164
    /**
165
     * @param array $data
166
     *
167
     * @throws InvalidKeyTypeException
168
     *
169
     * @return $this
170
     */
171
    public function setData(array $data) : KeyValuePairInterface
15✔
172
    {
173
        $this->validateKeys($data);
15✔
174
        foreach ($data as $key => $value) {
15✔
175
            $this->add($key, $value);
13✔
176
        }
177

178
        return $this;
15✔
179
    }
180

181
    /**
182
     * @param mixed $offset
183
     *
184
     * @return bool|mixed
185
     */
186
    public function offsetGet($offset) : mixed
4✔
187
    {
188
        if ($this->offsetExists($offset)) {
4✔
189
            $combined = $this->getData();
4✔
190

191
            return $combined[$offset];
4✔
192
        }
193

194
        return false;
1✔
195
    }
196

197
    /**
198
     * @param mixed $offset
199
     *
200
     * @return bool
201
     */
202
    public function offsetExists($offset) : bool
13✔
203
    {
204
        $flippedArray = array_flip($this->keys);
13✔
205

206
        return isset($flippedArray[$offset]);
13✔
207
    }
208

209
    /**
210
     * @param $offset
211
     *
212
     * @return mixed
213
     */
214
    protected function getOffsetPosition($offset)
13✔
215
    {
216
        $reverseKeys = array_flip($this->keys);
13✔
217

218
        return ($this->offsetExists($offset)) ? $reverseKeys[$offset] : false;
13✔
219
    }
220

221
    /**
222
     * @param array $dataSet
223
     *
224
     * @throws InvalidKeyTypeException
225
     *
226
     * @return bool
227
     */
228
    protected function validateKeys(array $dataSet)
15✔
229
    {
230
        foreach (array_keys($dataSet) as $key) {
15✔
231
            if (!$this->validateKey($key)) {
13✔
232
                throw new InvalidKeyTypeException('Key ('.$key.') is not a valid type.');
×
233
            }
234
        }
235

236
        return true;
15✔
237
    }
238

239
    /**
240
     * @param $key
241
     *
242
     * @return bool
243
     */
244
    protected function validateKey($key)
14✔
245
    {
246
        return is_string($key) || is_int($key);
14✔
247
    }
248

249
    public function __serialize() : array
1✔
250
    {
251
        return [
1✔
252
            'version' => static::$serializeVersion,
1✔
253
            'model'   => get_class($this),
1✔
254
            'data'    => $this->getData(),
1✔
255
        ];
1✔
256
    }
257

258
    public function __unserialize(array $data) : void
1✔
259
    {
260
        if ($data['model'] !== get_class($this)) {
1✔
261
            throw new UnmatchedClassException();
1✔
262
        }
263

264
        if ($data['version'] !== static::$serializeVersion) {
1✔
265
            throw new UnmatchedVersionException();
1✔
266
        }
267

268
        $this->setData($data['data']);
1✔
269
    }
270

271
    /**
272
     * Get the value of a specified key and remove from
273
     * array.
274
     *
275
     * @param mixed $key
276
     *
277
     * @return mixed
278
     */
279
    public function extract(mixed $key) : mixed
×
280
    {
281
        $returnValue = $this->offsetGet($key);
×
282
        $this->delete($key);
×
283

284
        return $returnValue;
×
285
    }
286

287
    public function delete(mixed $key) : void
×
288
    {
289
        if ($this->offsetExists($key)) {
×
290
            $this->offsetUnset($key);
×
291
        }
292
    }
293

294
    public function isEmpty() : bool
×
295
    {
296
        return empty($this->keys) && empty($this->values);
×
297
    }
298
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc