Skip to content

Commit

Permalink
Merge 33cb9bc into 65428ce
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Mar 25, 2021
2 parents 65428ce + 33cb9bc commit f219d10
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Language/AST/Node.php
Expand Up @@ -95,7 +95,7 @@ public function __toString() : string
}

/**
* @return mixed[]
* @return array<string, mixed>
*/
public function toArray(bool $recursive = false) : array
{
Expand Down
26 changes: 14 additions & 12 deletions src/Language/AST/NodeList.php
Expand Up @@ -23,15 +23,15 @@
class NodeList implements ArrayAccess, IteratorAggregate, Countable
{
/**
* @var Node[]
* @phpstan-var array<T>
* @var array<Node|array>
* @phpstan-var array<T|array<string, mixed>>
*/
private $nodes;

/**
* @param Node[] $nodes
* @param array<Node|array<string, mixed>> $nodes
*
* @phpstan-param array<T> $nodes
* @phpstan-param array<T|array<string, mixed>> $nodes
* @phpstan-return self<T>
*/
public static function create(array $nodes) : self
Expand All @@ -40,9 +40,9 @@ public static function create(array $nodes) : self
}

/**
* @param Node[] $nodes
* @param array<Node|array> $nodes
*
* @phpstan-param array<T> $nodes
* @phpstan-param array<T|array<string, mixed>> $nodes
*/
public function __construct(array $nodes)
{
Expand Down Expand Up @@ -73,20 +73,22 @@ public function offsetGet($offset)// : Node
{
$item = $this->nodes[$offset];

if (is_array($item) && isset($item['kind'])) {
if (is_array($item)) {
/** @phpstan-var T $node */
$node = AST::fromArray($item);
$this->nodes[$offset] = $node;

return $node;
}

return $this->nodes[$offset];
return $item;
}

/**
* @param int|string|null $offset
* @param Node|mixed[] $value
* @param int|string|null $offset
* @param Node|array<string, mixed> $value
*
* @phpstan-param T|mixed[] $value
* @phpstan-param T|array<string, mixed> $value
*/
public function offsetSet($offset, $value) : void
{
Expand Down Expand Up @@ -124,7 +126,7 @@ public function splice(int $offset, int $length, $replacement = null) : NodeList
}

/**
* @param NodeList|Node[] $list
* @param NodeList|array<Node|array<string, mixed>> $list
*
* @phpstan-param NodeList<T>|array<T> $list
* @phpstan-return NodeList<T>
Expand Down
1 change: 1 addition & 0 deletions tests/Language/NodeListTest.php
Expand Up @@ -27,6 +27,7 @@ public function testThrowsOnInvalidArrays() : void
$nodeList = new NodeList([]);

self::expectException(InvariantViolation::class);
// @phpstan-ignore-next-line Wrong on purpose
$nodeList[] = ['not a valid array representation of an AST node'];
}

Expand Down

0 comments on commit f219d10

Please sign in to comment.