Skip to content

Commit c969ee1

Browse files
committed
Pull all path splitting logic into a single method
1 parent 1064bbd commit c969ee1

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/Data.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ public function __construct(array $data = [])
4242
*/
4343
public function append(string $key, $value = null): void
4444
{
45-
if (0 == strlen($key)) {
46-
throw new InvalidPathException("Key cannot be an empty string");
47-
}
48-
4945
$currentValue =& $this->data;
50-
$keyPath = explode('.', $key);
46+
$keyPath = $this->keyToPathArray($key);
5147

5248
if (1 == count($keyPath)) {
5349
if (!isset($currentValue[$key])) {
@@ -88,12 +84,8 @@ public function append(string $key, $value = null): void
8884
*/
8985
public function set(string $key, $value = null): void
9086
{
91-
if (0 == strlen($key)) {
92-
throw new InvalidPathException("Key cannot be an empty string");
93-
}
94-
9587
$currentValue =& $this->data;
96-
$keyPath = explode('.', $key);
88+
$keyPath = $this->keyToPathArray($key);
9789

9890
if (1 == count($keyPath)) {
9991
$currentValue[$key] = $value;
@@ -120,12 +112,8 @@ public function set(string $key, $value = null): void
120112
*/
121113
public function remove(string $key): void
122114
{
123-
if (0 == strlen($key)) {
124-
throw new InvalidPathException("Key cannot be an empty string");
125-
}
126-
127115
$currentValue =& $this->data;
128-
$keyPath = explode('.', $key);
116+
$keyPath = $this->keyToPathArray($key);
129117

130118
if (1 == count($keyPath)) {
131119
unset($currentValue[$key]);
@@ -152,7 +140,7 @@ public function remove(string $key): void
152140
public function get(string $key, $default = null)
153141
{
154142
$currentValue = $this->data;
155-
$keyPath = explode('.', $key);
143+
$keyPath = $this->keyToPathArray($key);
156144

157145
for ($i = 0; $i < count($keyPath); $i++) {
158146
$currentKey = $keyPath[$i];
@@ -176,7 +164,7 @@ public function get(string $key, $default = null)
176164
public function has(string $key): bool
177165
{
178166
$currentValue = &$this->data;
179-
$keyPath = explode('.', $key);
167+
$keyPath = $this->keyToPathArray($key);
180168

181169
for ($i = 0; $i < count($keyPath); $i++) {
182170
$currentKey = $keyPath[$i];
@@ -266,4 +254,20 @@ public function offsetUnset($key)
266254
{
267255
$this->remove($key);
268256
}
257+
258+
/**
259+
* @return string[]
260+
*
261+
* @psalm-return non-empty-list<string>
262+
*
263+
* @psalm-pure
264+
*/
265+
protected function keyToPathArray(string $path): array
266+
{
267+
if (\strlen($path) === 0) {
268+
throw new InvalidPathException('Path cannot be an empty string');
269+
}
270+
271+
return \explode('.', $path);
272+
}
269273
}

tests/DataTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ protected function runSampleDataTests(DataInterface $data)
5656
$this->assertNull($data->get('f.g.h.i'));
5757
$this->assertEquals($data->get('foo', 'default-value-1'), 'default-value-1', 'Return default value');
5858
$this->assertEquals($data->get('f.g.h.i', 'default-value-2'), 'default-value-2');
59+
60+
$this->expectException(InvalidPathException::class);
61+
$data->get('', 'broken');
5962
}
6063

6164
public function testAppend()
@@ -165,6 +168,9 @@ public function testHas()
165168
) {
166169
$this->assertFalse($data->has($notExistentKey));
167170
}
171+
172+
$this->expectException(InvalidPathException::class);
173+
$data->has('', 'broken');
168174
}
169175

170176
public function testGetData()

0 commit comments

Comments
 (0)