Skip to content

Commit d557796

Browse files
author
Kirill Nesmeyanov
committed
Add type resolver
1 parent 7e88ede commit d557796

20 files changed

+267
-25
lines changed

src/Node/Identifier.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,33 @@ public function __construct(
5353
*/
5454
public function isSpecial(): bool
5555
{
56-
return \in_array($this->value, self::SPECIAL_CLASS_NAME, true);
56+
return self::looksLikeSpecial($this->value);
57+
}
58+
59+
/**
60+
* Returns {@see true} in case of passed "$name" argument looks like
61+
* a special type name or {@see false} instead.
62+
*/
63+
public static function looksLikeSpecial(string $name): bool
64+
{
65+
return \in_array(\strtolower($name), self::SPECIAL_CLASS_NAME, true);
5766
}
5867

5968
/**
6069
* Returns {@see true} in case of name contains builtin type name.
6170
*/
6271
public function isBuiltin(): bool
6372
{
64-
return \in_array($this->value, self::BUILTIN_TYPE_NAME, true);
73+
return self::looksLikeBuiltin($this->value);
74+
}
75+
76+
/**
77+
* Returns {@see true} in case of passed "$name" argument looks like
78+
* a builtin type name or {@see false} instead.
79+
*/
80+
public static function looksLikeBuiltin(string $value): bool
81+
{
82+
return \in_array(\strtolower($value), self::BUILTIN_TYPE_NAME, true);
6583
}
6684

6785
/**

src/Node/Name.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ final public function __construct(iterable|string|Identifier $name)
4040
*/
4141
private function parseName(iterable|string|Identifier $name): array
4242
{
43+
if (\is_string($name)) {
44+
$name = \array_filter(\explode('\\', $name));
45+
}
46+
4347
if (\is_iterable($name)) {
4448
$result = [];
4549

@@ -110,6 +114,70 @@ public function slice(int $offset = 0, int $length = null): self
110114
return new static(\array_slice($this->parts, $offset, $length));
111115
}
112116

117+
/**
118+
* Appends the passed {@see Name} to the existing one at the end.
119+
*
120+
* ```php
121+
* $name = new Name('Some\Any');
122+
*
123+
* echo $name->withAdded(new Name('Test\Class'));
124+
* > "Some\Any\Test\Class"
125+
*
126+
* echo $name->withAdded(new Name('Any\Class'));
127+
* > "Some\Any\Any\Class"
128+
* ```
129+
*/
130+
public function withAdded(self $name): self
131+
{
132+
return new static([
133+
...$this->parts,
134+
...$name->parts,
135+
]);
136+
}
137+
138+
/**
139+
* Combines two names into one (in case the last one is an alias).
140+
*
141+
* ```php
142+
* $name = new Name('Some\Any');
143+
*
144+
* echo $name->mergeWith(new Name('Test\Class'));
145+
* > "Some\Any\Class"
146+
*
147+
* echo $name->mergeWith(new Name('Any\Class'));
148+
* > "Some\Any\Class"
149+
* ```
150+
*
151+
* Real world use case:
152+
* ```php
153+
* // use TypeLang\Parser\Node;
154+
* // echo Node::class;
155+
*
156+
* $name = new Name('TypeLang\Parser\Node');
157+
* echo $name->mergeWith(new Name('Node'));
158+
*
159+
* // > TypeLang\Parser\Node
160+
* ```
161+
*
162+
* Or aliased:
163+
* ```php
164+
* // use TypeLang\Parser\Exception as Error;
165+
* // echo Error\SemanticException::class;
166+
*
167+
* $name = new Name('TypeLang\Parser\Exception');
168+
* echo $name->mergeWith(new Name('Error\SemanticException'));
169+
*
170+
* // > TypeLang\Parser\Exception\SemanticException
171+
* ```
172+
*/
173+
public function mergeWith(self $name): self
174+
{
175+
return new static([
176+
...$this->parts,
177+
...\array_slice($name->parts, 1),
178+
]);
179+
}
180+
113181
/**
114182
* Convert name to full qualified name instance.
115183
*/

src/Node/NodeList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ abstract class NodeList extends Node implements \IteratorAggregate, \Countable
1515
* @param list<TNode> $list
1616
*/
1717
public function __construct(
18-
public readonly array $list = [],
18+
public array $list = [],
1919
) {}
2020

2121
public function getIterator(): \Traversable

src/Node/Stmt/CallableTypeNode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
class CallableTypeNode extends TypeStatement
1111
{
1212
public function __construct(
13-
public readonly Name $name,
14-
public readonly ParametersListNode $parameters = new ParametersListNode(),
15-
public readonly ?TypeStatement $type = null,
13+
public Name $name,
14+
public ParametersListNode $parameters = new ParametersListNode(),
15+
public ?TypeStatement $type = null,
1616
) {}
1717
}

src/Node/Stmt/ClassConstMaskNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class ClassConstMaskNode extends TypeStatement
1111
{
1212
public function __construct(
13-
public readonly Name $class,
14-
public readonly ?Identifier $constant = null,
13+
public Name $class,
14+
public ?Identifier $constant = null,
1515
) {}
1616
}

src/Node/Stmt/Condition/Condition.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
abstract class Condition extends Statement
1111
{
1212
public function __construct(
13-
public readonly TypeStatement $subject,
14-
public readonly TypeStatement $target,
13+
public TypeStatement $subject,
14+
public TypeStatement $target,
1515
) {}
1616
}

src/Node/Stmt/ConstMaskNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class ConstMaskNode extends TypeStatement implements \Stringable
1010
{
1111
public function __construct(
12-
public readonly Name $name,
12+
public Name $name,
1313
) {}
1414

1515
public function __toString(): string

src/Node/Stmt/GenericTypeStmt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ abstract class GenericTypeStmt extends TypeStatement
1313
* @param T $type
1414
*/
1515
public function __construct(
16-
public readonly TypeStatement $type,
16+
public TypeStatement $type,
1717
) {}
1818
}

src/Node/Stmt/LogicalTypeNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class LogicalTypeNode extends TypeStatement implements \IteratorAggrega
1313
/**
1414
* @var non-empty-list<T>
1515
*/
16-
public readonly array $statements;
16+
public array $statements;
1717

1818
public function __construct(
1919
TypeStatement $a,

src/Node/Stmt/NamedTypeNode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
class NamedTypeNode extends TypeStatement
1212
{
1313
public function __construct(
14-
public readonly Name $name,
15-
public readonly ?ArgumentsListNode $arguments = null,
16-
public readonly ?FieldsListNode $fields = null,
14+
public Name $name,
15+
public ?ArgumentsListNode $arguments = null,
16+
public ?FieldsListNode $fields = null,
1717
) {}
1818
}

0 commit comments

Comments
 (0)