Skip to content

Commit 0a07684

Browse files
committed
Improve some other statements and remove old deprecated classes
1 parent e819450 commit 0a07684

37 files changed

Lines changed: 249 additions & 278 deletions

libs/parser/resources/grammar.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@
427427
throw FeatureNotAllowedException::fromFeature('callable types', $offset);
428428
}
429429

430-
$parameters = isset($children[0]) && $children[0] instanceof Node\Stmt\Callable\ParametersListNode
430+
$parameters = isset($children[0]) && $children[0] instanceof Node\Stmt\Callable\CallableParametersListNode
431431
? \array_shift($children)
432432
: new Node\Stmt\Callable\CallableParametersListNode();
433433

@@ -558,7 +558,7 @@
558558

559559
foreach ($children as $field) {
560560
if ($field instanceof Node\Stmt\Shape\ExplicitFieldNode) {
561-
$key = $field->getKey();
561+
$key = $field->index;
562562

563563
if (\in_array($key, $explicit, true)) {
564564
throw SemanticException::fromShapeFieldDuplication($key, $field->offset);
@@ -590,7 +590,7 @@
590590

591591
$parameters = null;
592592

593-
if (\end($children) instanceof Node\Stmt\Template\ArgumentsListNode) {
593+
if (\end($children) instanceof Node\Stmt\Template\TemplateArgumentsListNode) {
594594
$parameters = \array_pop($children);
595595
}
596596

@@ -651,7 +651,7 @@
651651
}
652652

653653
// Template parameters
654-
if (\end($children) instanceof Node\Stmt\Template\ArgumentsListNode) {
654+
if (\end($children) instanceof Node\Stmt\Template\TemplateArgumentsListNode) {
655655
$parameters = \array_pop($children);
656656
}
657657

libs/parser/resources/grammar/callable.pp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CallableType -> {
66
throw FeatureNotAllowedException::fromFeature('callable types', $offset);
77
}
88

9-
$parameters = isset($children[0]) && $children[0] instanceof Node\Stmt\Callable\ParametersListNode
9+
$parameters = isset($children[0]) && $children[0] instanceof Node\Stmt\Callable\CallableParametersListNode
1010
? \array_shift($children)
1111
: new Node\Stmt\Callable\CallableParametersListNode();
1212

libs/parser/resources/grammar/named-type.pp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ NamedType -> {
88
}
99

1010
// Template parameters
11-
if (\end($children) instanceof Node\Stmt\Template\ArgumentsListNode) {
11+
if (\end($children) instanceof Node\Stmt\Template\TemplateArgumentsListNode) {
1212
$parameters = \array_pop($children);
1313
}
1414

libs/parser/resources/grammar/shape-fields.pp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ShapeFields -> {
1010

1111
$parameters = null;
1212

13-
if (\end($children) instanceof Node\Stmt\Template\ArgumentsListNode) {
13+
if (\end($children) instanceof Node\Stmt\Template\TemplateArgumentsListNode) {
1414
$parameters = \array_pop($children);
1515
}
1616

@@ -43,7 +43,7 @@ ShapeFieldsList -> {
4343

4444
foreach ($children as $field) {
4545
if ($field instanceof Node\Stmt\Shape\ExplicitFieldNode) {
46-
$key = $field->getKey();
46+
$key = $field->index;
4747

4848
if (\in_array($key, $explicit, true)) {
4949
throw SemanticException::fromShapeFieldDuplication($key, $field->offset);

libs/parser/src/Node/NodeList.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ abstract class NodeList extends Node implements
1919
*/
2020
public array $items = [];
2121

22+
/**
23+
* @var TNode|null
24+
*/
25+
public ?Node $first {
26+
get => $this->items[0] ?? null;
27+
}
28+
29+
/**
30+
* @var TNode|null
31+
*/
32+
public ?Node $last {
33+
get => ($lastKey = \array_key_last($this->items)) !== null
34+
? $this->items[$lastKey]
35+
: null;
36+
}
37+
2238
/**
2339
* @param iterable<mixed, TNode> $items
2440
*/
@@ -49,26 +65,6 @@ public function findIndex(Node $node): ?int
4965
return null;
5066
}
5167

52-
/**
53-
* @return TNode|null
54-
*/
55-
public function first(): ?Node
56-
{
57-
$first = \reset($this->items);
58-
59-
return $first instanceof Node ? $first : null;
60-
}
61-
62-
/**
63-
* @return TNode|null
64-
*/
65-
public function last(): ?Node
66-
{
67-
$last = \end($this->items);
68-
69-
return $last instanceof Node ? $last : null;
70-
}
71-
7268
public function offsetExists(mixed $offset): bool
7369
{
7470
// @phpstan-ignore-next-line

libs/parser/src/Node/Stmt/Attribute/AttributeGroupNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
/**
1010
* @template-extends NodeList<AttributeNode>
1111
*/
12-
final class AttributeGroupNode extends NodeList {}
12+
class AttributeGroupNode extends NodeList {}

libs/parser/src/Node/Stmt/Attribute/AttributeGroupsListNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
/**
1010
* @template-extends NodeList<AttributeGroupNode>
1111
*/
12-
final class AttributeGroupsListNode extends NodeList {}
12+
class AttributeGroupsListNode extends NodeList {}

libs/parser/src/Node/Stmt/Attribute/AttributeNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use TypeLang\Parser\Node\Name;
88
use TypeLang\Parser\Node\Node;
99

10-
final class AttributeNode extends Node
10+
class AttributeNode extends Node
1111
{
1212
public function __construct(
1313
public Name $name,

libs/parser/src/Node/Stmt/Callable/CallableParameterNode.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,50 @@
44

55
namespace TypeLang\Parser\Node\Stmt\Callable;
66

7-
class CallableParameterNode extends ParameterNode {}
7+
use TypeLang\Parser\Node\Literal\VariableLiteralNode;
8+
use TypeLang\Parser\Node\Node;
9+
use TypeLang\Parser\Node\Stmt\Attribute\AttributeGroupsListNode;
10+
use TypeLang\Parser\Node\Stmt\TypeStatement;
11+
12+
class CallableParameterNode extends Node implements \Stringable
13+
{
14+
public function __construct(
15+
public ?TypeStatement $type = null,
16+
public ?VariableLiteralNode $name = null,
17+
public bool $output = false,
18+
public bool $variadic = false,
19+
public bool $optional = false,
20+
public ?AttributeGroupsListNode $attributes = null,
21+
) {
22+
assert($type !== null || $name !== null, new \TypeError(
23+
'Required indication of the type or name of the parameter (one of)',
24+
));
25+
26+
assert($variadic === false || $optional === false, new \TypeError(
27+
'Parameter cannot be both variable and optional (variadic parameter is already optional)',
28+
));
29+
}
30+
31+
public function __toString(): string
32+
{
33+
$result = [];
34+
35+
if ($this->output) {
36+
$result[] = 'output';
37+
}
38+
39+
if ($this->variadic) {
40+
$result[] = 'variadic';
41+
}
42+
43+
if ($this->optional) {
44+
$result[] = 'optional';
45+
}
46+
47+
if ($result === []) {
48+
return 'simple';
49+
}
50+
51+
return \implode(', ', $result);
52+
}
53+
}

libs/parser/src/Node/Stmt/Callable/CallableParametersListNode.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace TypeLang\Parser\Node\Stmt\Callable;
66

7+
use TypeLang\Parser\Node\NodeList;
8+
79
/**
8-
* @template-extends ParametersListNode<CallableParameterNode>
10+
* @template-extends NodeList<CallableParameterNode>
911
*/
10-
class CallableParametersListNode extends ParametersListNode {}
12+
class CallableParametersListNode extends NodeList {}

0 commit comments

Comments
 (0)