Skip to content

Commit e768f4d

Browse files
committed
Reduce array encoding complexity further
1 parent 3225ca1 commit e768f4d

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/Encoder/ArrayEncoder.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,26 @@ private function getPairs($array, $space, $omit, callable $encode, & $omitted =
158158
$format = '%s' . $space . '=>' . $space . '%s';
159159

160160
foreach ($array as $key => $value) {
161-
if ($key === $nextIndex && $omit) {
161+
if ($omit && $this->canOmit($key, $nextIndex)) {
162162
$pairs[] = $encode($value, 1);
163163
} else {
164164
$pairs[] = sprintf($format, $encode($key, 1), $encode($value, 1));
165165
$omitted = false;
166166
}
167-
168-
if (is_int($key) && $key >= $nextIndex) {
169-
$nextIndex = $key + 1;
170-
}
171167
}
172168

173169
return $pairs;
174170
}
171+
172+
private function canOmit($key, & $nextIndex)
173+
{
174+
if (!is_int($key) || $key < $nextIndex) {
175+
return false;
176+
}
177+
178+
$result = $key === $nextIndex;
179+
$nextIndex = $key + 1;
180+
181+
return $result;
182+
}
175183
}

0 commit comments

Comments
 (0)