Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Fixed empty array-like objects to be falsy.
* Fixed length() and reverse() to read objects with __toString() as strings.
* Changed array-like objects that are not Traversable to error when their elements are listed.
* Improved the performance of flatten on large arrays.

## 2.10.0 - 2026-11-17

Expand Down
33 changes: 22 additions & 11 deletions src/TreeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ private function visit_expref(array $node)
private function visit_flatten(array $node)
{
$this->dispatch($node['children'][0]);
$merged = $this->makeVar('merged');
$lists = $this->makeVar('lists');
$run = $this->makeVar('run');
$val = $this->makeVar('val');

$this
Expand All @@ -347,33 +348,43 @@ private function visit_flatten(array $node)
->write('} else {')
->indent()
->write('if (!is_array($value)) { $value = Utils::toArray($value); }')
->write('%s = [];', $merged)
->write('%s = [];', $lists)
->write('%s = [];', $run)
->write('foreach ($value as %s) {', $val)
->indent()
->write('if (is_array(%s)) {', $val)
->indent()
->write('if (array_key_exists(0, %s)) {', $val)
->write('if (!array_key_exists(0, %s)) {', $val)
->indent()
->write('%s = array_merge(%s, %s);', $merged, $merged, $val)
->outdent()
->write('} elseif (%s !== []) {', $val)
->indent()
->write('%s[] = %s;', $merged, $val)
->write('if (%s !== []) { %s[] = %s; }', $val, $run, $val)
->write('continue;')
->outdent()
->write('}')
->outdent()
->write('} elseif (%s instanceof \\ArrayAccess && Utils::isArray(%s)) {', $val, $val)
->indent()
->write('%s = array_merge(%s, Utils::toArray(%s));', $merged, $merged, $val)
->write('%s = Utils::toArray(%s);', $val, $val)
->outdent()
->write('} else {')
->indent()
->write('%s[] = %s;', $merged, $val)
->write('%s[] = %s;', $run, $val)
->write('continue;')
->outdent()
->write('}')
->write('if (%s !== []) { %s[] = %s; %s = []; }', $run, $lists, $run, $run)
->write('%s[] = %s;', $lists, $val)
->outdent()
->write('}')
->write('if (%s === []) {', $lists)
->indent()
->write('$value = %s;', $run)
->outdent()
->write('} else {')
->indent()
->write('%s[] = %s;', $lists, $run)
->write('$value = array_merge(...%s);', $lists)
->outdent()
->write('}')
->write('$value = %s;', $merged)
->outdent()
->write('}');

Expand Down
29 changes: 21 additions & 8 deletions src/TreeInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,36 @@ private function dispatch(array $node, $value)
$value = Utils::toArray($value);
}

$merged = [];
$lists = [];
$run = [];
foreach ($value as $values) {
// Only merge up arrays lists and not hashes
if (is_array($values)) {
if (array_key_exists(0, $values)) {
$merged = array_merge($merged, $values);
} elseif ($values !== $skipElement) {
$merged[] = $values;
if (!array_key_exists(0, $values)) {
if ($values !== $skipElement) {
$run[] = $values;
}
continue;
}
} elseif ($values instanceof \ArrayAccess && Utils::isArray($values)) {
$merged = array_merge($merged, Utils::toArray($values));
$values = Utils::toArray($values);
} else {
$merged[] = $values;
$run[] = $values;
continue;
}

if ($run !== []) {
$lists[] = $run;
$run = [];
}
$lists[] = $values;
}
if ($lists === []) {
return $run;
}
$lists[] = $run;

return $merged;
return array_merge(...$lists);

case 'literal':
return $node['value'];
Expand Down
2 changes: 2 additions & 0 deletions tests/CompilerRuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,14 @@ public function testRuntimesFlattenArrayLikeObjects(): void
$data = [
'lists' => new _TestArrayLike([new _TestArrayLike([1, 2]), [3], new _TestArrayLike(), 4]),
'hashes' => new _TestArrayLike([$hash]),
'mixed' => [1, $hash, [], [2, 3], 4, 5, new _TestArrayLike([6]), 7],
];

try {
foreach ([new AstRuntime(), new CompilerRuntime($dir)] as $runtime) {
$this->assertSame([1, 2, 3, 4], $runtime('lists[]', $data));
$this->assertSame([$hash], $runtime('hashes[]', $data));
$this->assertSame([1, $hash, 2, 3, 4, 5, 6, 7], $runtime('mixed[]', $data));
}
} finally {
$this->removeTempDir($dir);
Expand Down