Skip to content

Commit 0106e52

Browse files
committed
Extractor: fixed extracting of special arrays [Closes #143]
1 parent d915b7f commit 0106e52

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/PhpGenerator/Extractor.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,17 @@ private function toValue(Node\Expr $node): mixed
418418
$res = [];
419419
foreach ($node->items as $item) {
420420
if ($item->unpack) {
421-
$res[] = new Literal($this->getReformattedContents([$item], 0));
421+
return new Literal($this->getReformattedContents([$node], 0));
422422

423423
} elseif ($item->key) {
424424
$key = $item->key instanceof Node\Identifier
425425
? $item->key->name
426426
: $this->toValue($item->key);
427+
428+
if ($key instanceof Literal) {
429+
return new Literal($this->getReformattedContents([$node], 0));
430+
}
431+
427432
$res[$key] = $this->toValue($item->value);
428433

429434
} else {

tests/PhpGenerator/Extractor.extractAll.vars.phpt

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ $file = (new Extractor(<<<'XX'
2020
public $null = null;
2121
public $scalar = [true, false, 1, 1.0, 'hello'];
2222
public $const = [PHP_VERSION, self::Foo];
23-
public $array = [1, 2, ['x' => [3]], ...self::Foo];
23+
public $array = [1, 2, ['x' => [3]]];
24+
public $arraySpec1 = [...self::Foo];
25+
public $arraySpec2 = [self::class => 1];
2426
public $concat = 'x' . 'y';
2527
public $math = 10 * 3;
2628
@@ -49,9 +51,17 @@ Assert::equal(
4951
$class->getProperty('const')->getValue(),
5052
);
5153
Assert::equal(
52-
[1, 2, ['x' => [3]], new Literal('...self::Foo')],
54+
[1, 2, ['x' => [3]]],
5355
$class->getProperty('array')->getValue(),
5456
);
57+
Assert::equal(
58+
new Literal('[...self::Foo]'),
59+
$class->getProperty('arraySpec1')->getValue(),
60+
);
61+
Assert::equal(
62+
new Literal('[self::class => 1]'),
63+
$class->getProperty('arraySpec2')->getValue(),
64+
);
5565
Assert::equal(
5666
new Literal("'x' . 'y'"),
5767
$class->getProperty('concat')->getValue(),

0 commit comments

Comments
 (0)