Skip to content

Commit 9be38e8

Browse files
committed
Simplify key iteration
1 parent b24e50c commit 9be38e8

File tree

1 file changed

+9
-39
lines changed

1 file changed

+9
-39
lines changed

src/Data.php

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,8 @@ public function append(string $key, $value = null): void
4949
$currentValue =& $this->data;
5050
$keyPath = $this->keyToPathArray($key);
5151

52-
if (1 == count($keyPath)) {
53-
if (!isset($currentValue[$key])) {
54-
$currentValue[$key] = [];
55-
}
56-
if (!is_array($currentValue[$key])) {
57-
// Promote this key to an array.
58-
// TODO: Is this really what we want to do?
59-
$currentValue[$key] = [$currentValue[$key]];
60-
}
61-
$currentValue[$key][] = $value;
62-
63-
return;
64-
}
65-
6652
$endKey = array_pop($keyPath);
67-
for ($i = 0; $i < count($keyPath); $i++) {
68-
$currentKey =& $keyPath[$i];
53+
foreach ($keyPath as $currentKey) {
6954
if (! isset($currentValue[$currentKey])) {
7055
$currentValue[$currentKey] = [];
7156
}
@@ -75,11 +60,13 @@ public function append(string $key, $value = null): void
7560
if (!isset($currentValue[$endKey])) {
7661
$currentValue[$endKey] = [];
7762
}
63+
7864
if (!is_array($currentValue[$endKey])) {
65+
// Promote this key to an array.
66+
// TODO: Is this really what we want to do?
7967
$currentValue[$endKey] = [$currentValue[$endKey]];
8068
}
81-
// Promote this key to an array.
82-
// TODO: Is this really what we want to do?
69+
8370
$currentValue[$endKey][] = $value;
8471
}
8572

@@ -91,15 +78,8 @@ public function set(string $key, $value = null): void
9178
$currentValue =& $this->data;
9279
$keyPath = $this->keyToPathArray($key);
9380

94-
if (1 == count($keyPath)) {
95-
$currentValue[$key] = $value;
96-
97-
return;
98-
}
99-
10081
$endKey = array_pop($keyPath);
101-
for ($i = 0; $i < count($keyPath); $i++) {
102-
$currentKey =& $keyPath[$i];
82+
foreach ($keyPath as $currentKey) {
10383
if (!isset($currentValue[$currentKey])) {
10484
$currentValue[$currentKey] = [];
10585
}
@@ -119,15 +99,8 @@ public function remove(string $key): void
11999
$currentValue =& $this->data;
120100
$keyPath = $this->keyToPathArray($key);
121101

122-
if (1 == count($keyPath)) {
123-
unset($currentValue[$key]);
124-
125-
return;
126-
}
127-
128102
$endKey = array_pop($keyPath);
129-
for ($i = 0; $i < count($keyPath); $i++) {
130-
$currentKey =& $keyPath[$i];
103+
foreach ($keyPath as $currentKey) {
131104
if (!isset($currentValue[$currentKey])) {
132105
return;
133106
}
@@ -146,8 +119,7 @@ public function get(string $key, $default = null)
146119
$currentValue = $this->data;
147120
$keyPath = $this->keyToPathArray($key);
148121

149-
for ($i = 0; $i < count($keyPath); $i++) {
150-
$currentKey = $keyPath[$i];
122+
foreach ($keyPath as $currentKey) {
151123
if (!isset($currentValue[$currentKey])) {
152124
return $default;
153125
}
@@ -168,10 +140,8 @@ public function get(string $key, $default = null)
168140
public function has(string $key): bool
169141
{
170142
$currentValue = &$this->data;
171-
$keyPath = $this->keyToPathArray($key);
172143

173-
for ($i = 0; $i < count($keyPath); $i++) {
174-
$currentKey = $keyPath[$i];
144+
foreach ($this->keyToPathArray($key) as $currentKey) {
175145
if (
176146
!is_array($currentValue) ||
177147
!array_key_exists($currentKey, $currentValue)

0 commit comments

Comments
 (0)