Skip to content

Commit

Permalink
Extractor: fixed extracting of special arrays [Closes #143]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 29, 2023
1 parent d915b7f commit 0106e52
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,17 @@ private function toValue(Node\Expr $node): mixed
$res = [];
foreach ($node->items as $item) {
if ($item->unpack) {
$res[] = new Literal($this->getReformattedContents([$item], 0));
return new Literal($this->getReformattedContents([$node], 0));

} elseif ($item->key) {
$key = $item->key instanceof Node\Identifier
? $item->key->name
: $this->toValue($item->key);

if ($key instanceof Literal) {
return new Literal($this->getReformattedContents([$node], 0));
}

$res[$key] = $this->toValue($item->value);

} else {
Expand Down
14 changes: 12 additions & 2 deletions tests/PhpGenerator/Extractor.extractAll.vars.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ $file = (new Extractor(<<<'XX'
public $null = null;
public $scalar = [true, false, 1, 1.0, 'hello'];
public $const = [PHP_VERSION, self::Foo];
public $array = [1, 2, ['x' => [3]], ...self::Foo];
public $array = [1, 2, ['x' => [3]]];
public $arraySpec1 = [...self::Foo];
public $arraySpec2 = [self::class => 1];
public $concat = 'x' . 'y';
public $math = 10 * 3;
Expand Down Expand Up @@ -49,9 +51,17 @@ Assert::equal(
$class->getProperty('const')->getValue(),
);
Assert::equal(
[1, 2, ['x' => [3]], new Literal('...self::Foo')],
[1, 2, ['x' => [3]]],
$class->getProperty('array')->getValue(),
);
Assert::equal(
new Literal('[...self::Foo]'),
$class->getProperty('arraySpec1')->getValue(),
);
Assert::equal(
new Literal('[self::class => 1]'),
$class->getProperty('arraySpec2')->getValue(),
);
Assert::equal(
new Literal("'x' . 'y'"),
$class->getProperty('concat')->getValue(),
Expand Down

0 comments on commit 0106e52

Please sign in to comment.