Skip to content

Commit 769a8c2

Browse files
committed
Improve name + identifier ast nodes
1 parent 1f3f903 commit 769a8c2

32 files changed

Lines changed: 2012 additions & 219 deletions

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@
5353
"phpcs:check": ["phpcs", "cs", "psr", "per"]
5454
},
5555
"scripts": {
56-
"test": [
57-
"@test:unit"
58-
],
59-
"test:unit": "phpunit --testdox --testsuite=unit",
56+
"test:unit": "phpunit --testdox",
6057
"linter:check": "phpstan analyse --configuration phpstan.neon --memory-limit 256M",
6158
"linter:baseline": "@linter:check --generate-baseline",
6259
"phpcs:check": "@phpcs:fix --dry-run",

libs/parser/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
---
66

77
<p align="center">
8-
<a href="https://packagist.org/packages/type-lang/parser"><img src="https://poser.pugx.org/type-lang/parser/require/php?style=for-the-badge" alt="PHP 8.1+"></a>
8+
<a href="https://packagist.org/packages/type-lang/parser"><img src="https://poser.pugx.org/type-lang/parser/require/php?style=for-the-badge" alt="PHP 8.4+"></a>
99
<a href="https://packagist.org/packages/type-lang/parser"><img src="https://poser.pugx.org/type-lang/parser/version?style=for-the-badge" alt="Latest Stable Version"></a>
1010
<a href="https://packagist.org/packages/type-lang/parser"><img src="https://poser.pugx.org/type-lang/parser/v/unstable?style=for-the-badge" alt="Latest Unstable Version"></a>
1111
<a href="https://raw.githubusercontent.com/php-type-language/parser/blob/master/LICENSE"><img src="https://poser.pugx.org/type-lang/parser/license?style=for-the-badge" alt="License MIT"></a>

libs/parser/resources/grammar.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,16 @@
277277
],
278278
'reducers' => [
279279
0 => static function (\Phplrt\Parser\Context $ctx, $children) {
280-
return new Node\FullQualifiedName($children);
280+
return new Node\Name($children, true);
281281
},
282282
1 => static function (\Phplrt\Parser\Context $ctx, $children) {
283-
return new Node\Name($children);
283+
return new Node\Name($children, false);
284284
},
285285
3 => static function (\Phplrt\Parser\Context $ctx, $children) {
286-
return new Node\Identifier($children->getValue());
286+
return Node\Identifier::createFromString($children->getValue());
287287
},
288288
16 => static function (\Phplrt\Parser\Context $ctx, $children) {
289-
return new Node\Identifier($children->getValue());
289+
return Node\Identifier::createFromString($children->getValue());
290290
},
291291
17 => function (\Phplrt\Parser\Context $ctx, $children) {
292292
// The "$offset" variable is an auto-generated

libs/parser/resources/grammar/common.pp2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ Name
55
;
66

77
FullQualifiedName -> {
8-
return new Node\FullQualifiedName($children);
8+
return new Node\Name($children, true);
99
}
1010
: ::T_NS_DELIMITER:: Identifier() (::T_NS_DELIMITER:: Identifier())*
1111
;
1212

1313
RelativeName -> {
14-
return new Node\Name($children);
14+
return new Node\Name($children, false);
1515
}
1616
: Identifier() (::T_NS_DELIMITER:: Identifier())*
1717
;
1818

1919
Identifier -> {
20-
return new Node\Identifier($children->getValue());
20+
return Node\Identifier::createFromString($children->getValue());
2121
}
2222
: <T_NAME>
2323
| <T_NAME_WITH_SPACE>
@@ -27,7 +27,7 @@ Identifier -> {
2727
;
2828

2929
IdentifierWithExtraSpace -> {
30-
return new Node\Identifier($children->getValue());
30+
return Node\Identifier::createFromString($children->getValue());
3131
}
3232
: <T_NAME_WITH_SPACE>
3333
;

libs/parser/src/Node/FullQualifiedName.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

libs/parser/src/Node/Identifier.php

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class Identifier extends Node implements \Stringable
99
/**
1010
* @var list<non-empty-string>
1111
*/
12-
private const SPECIAL_CLASS_NAME = [
12+
private const array SPECIAL_CLASS_NAME = [
1313
'self',
1414
'parent',
1515
'static',
@@ -18,7 +18,7 @@ final class Identifier extends Node implements \Stringable
1818
/**
1919
* @var list<non-empty-string>
2020
*/
21-
private const BUILTIN_TYPE_NAME = [
21+
private const array BUILTIN_TYPE_NAME = [
2222
'mixed',
2323
'string',
2424
'int',
@@ -35,23 +35,6 @@ final class Identifier extends Node implements \Stringable
3535
'false',
3636
];
3737

38-
/**
39-
* @var non-empty-string
40-
*/
41-
public readonly string $value;
42-
43-
/**
44-
* @param non-empty-string $value
45-
*/
46-
public function __construct(string $value)
47-
{
48-
$value = \trim($value);
49-
50-
assert($value !== '', new \InvalidArgumentException('Identifier value cannot be empty'));
51-
52-
$this->value = $value;
53-
}
54-
5538
/**
5639
* Returns {@see true} if the identifier contains the name of
5740
* a "virtual" type, i.e. invalid in the PHP namespace.
@@ -61,41 +44,56 @@ public function __construct(string $value)
6144
* - `non-empty-array` - Virtual, cannot be defined in PHP.
6245
* - `empty-string` - Virtual, cannot be defined in PHP.
6346
*/
64-
public function isVirtual(): bool
65-
{
66-
return \str_contains($this->value, '-');
47+
public bool $isVirtual {
48+
get => \str_contains($this->value, '-');
6749
}
6850

6951
/**
7052
* Returns {@see true} in case of name contains special class reference.
7153
*/
72-
public function isSpecial(): bool
73-
{
74-
return self::looksLikeSpecial($this->value);
54+
public bool $isSpecial {
55+
get => self::isLooksLikeSpecial($this->value);
7556
}
7657

7758
/**
78-
* Returns {@see true} in case of passed "$name" argument looks like
79-
* a special type name or {@see false} instead.
59+
* Returns {@see true} in case of name contains builtin type name.
8060
*/
81-
public static function looksLikeSpecial(string $name): bool
61+
public bool $isBuiltin {
62+
get => self::isLooksLikeBuiltin($this->value);
63+
}
64+
65+
public function __construct(
66+
/**
67+
* @var non-empty-string
68+
*/
69+
public readonly string $value,
70+
) {}
71+
72+
public static function createFromString(string|\Stringable $value): self
8273
{
83-
return \in_array(\strtolower($name), self::SPECIAL_CLASS_NAME, true);
74+
$normalized = \trim((string) $value);
75+
76+
if ($normalized === '') {
77+
throw new \InvalidArgumentException('Name identifier cannot be empty');
78+
}
79+
80+
return new self($normalized);
8481
}
8582

8683
/**
87-
* Returns {@see true} in case of name contains builtin type name.
84+
* Returns {@see true} in case of passed "$name" argument looks like
85+
* a special type name or {@see false} instead.
8886
*/
89-
public function isBuiltin(): bool
87+
public static function isLooksLikeSpecial(string $name): bool
9088
{
91-
return self::looksLikeBuiltin($this->value);
89+
return \in_array(\strtolower($name), self::SPECIAL_CLASS_NAME, true);
9290
}
9391

9492
/**
9593
* Returns {@see true} in case of passed "$name" argument looks like
9694
* a builtin type name or {@see false} instead.
9795
*/
98-
public static function looksLikeBuiltin(string $value): bool
96+
public static function isLooksLikeBuiltin(string $value): bool
9997
{
10098
return \in_array(\strtolower($value), self::BUILTIN_TYPE_NAME, true);
10199
}

0 commit comments

Comments
 (0)