Skip to content

Commit 4bc2253

Browse files
Restrict Token::type to string
1 parent e947e87 commit 4bc2253

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

src/Token/Token.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010
final class Token
1111
{
1212
// From Twig\Token
13-
public const EOF_TYPE = -1;
14-
public const TEXT_TYPE = 0;
15-
public const BLOCK_START_TYPE = 1;
16-
public const VAR_START_TYPE = 2;
17-
public const BLOCK_END_TYPE = 3;
18-
public const VAR_END_TYPE = 4;
19-
public const NAME_TYPE = 5;
20-
public const NUMBER_TYPE = 6;
21-
public const STRING_TYPE = 7;
22-
public const OPERATOR_TYPE = 8;
23-
public const PUNCTUATION_TYPE = 9;
24-
public const INTERPOLATION_START_TYPE = 10;
25-
public const INTERPOLATION_END_TYPE = 11;
26-
public const ARROW_TYPE = 12;
27-
public const SPREAD_TYPE = 13;
13+
public const EOF_TYPE = 'EOF_TYPE';
14+
public const TEXT_TYPE = 'TEXT_TYPE';
15+
public const BLOCK_START_TYPE = 'BLOCK_START_TYPE';
16+
public const VAR_START_TYPE = 'VAR_START_TYPE';
17+
public const BLOCK_END_TYPE = 'BLOCK_END_TYPE';
18+
public const VAR_END_TYPE = 'VAR_END_TYPE';
19+
public const NAME_TYPE = 'NAME_TYPE';
20+
public const NUMBER_TYPE = 'NUMBER_TYPE';
21+
public const STRING_TYPE = 'STRING_TYPE';
22+
public const OPERATOR_TYPE = 'OPERATOR_TYPE';
23+
public const PUNCTUATION_TYPE = 'PUNCTUATION_TYPE';
24+
public const INTERPOLATION_START_TYPE = 'INTERPOLATION_START_TYPE';
25+
public const INTERPOLATION_END_TYPE = 'INTERPOLATION_END_TYPE';
26+
public const ARROW_TYPE = 'ARROW_TYPE';
27+
public const SPREAD_TYPE = 'SPREAD_TYPE';
2828
// New constants
2929
public const DQ_STRING_START_TYPE = 'DQ_STRING_START_TYPE';
3030
public const DQ_STRING_END_TYPE = 'DQ_STRING_END_TYPE';
@@ -94,7 +94,7 @@ final class Token
9494
];
9595

9696
public function __construct(
97-
private int|string $type,
97+
private string $type,
9898
private int $line,
9999
private int $linePosition,
100100
private string $filename,
@@ -103,12 +103,12 @@ public function __construct(
103103
) {
104104
}
105105

106-
public function getType(): int|string
106+
public function getType(): string
107107
{
108108
return $this->type;
109109
}
110110

111-
public function setType(int|string $type): void
111+
public function setType(string $type): void
112112
{
113113
$this->type = $type;
114114
}
@@ -144,10 +144,10 @@ public function setRelatedToken(self $token): void
144144
}
145145

146146
/**
147-
* @param int|string|array<int|string> $type
148-
* @param string|string[] $value
147+
* @param string|string[] $type
148+
* @param string|string[] $value
149149
*/
150-
public function isMatching(int|string|array $type, string|array $value = []): bool
150+
public function isMatching(string|array $type, string|array $value = []): bool
151151
{
152152
if (!\is_array($type)) {
153153
$type = [$type];

src/Token/Tokenizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ private function moveCurrentExpressionStarter(): void
285285
++$this->currentExpressionStarter;
286286
}
287287

288-
private function pushToken(int|string $type, string $value = '', ?Token $relatedToken = null): Token
288+
private function pushToken(string $type, string $value = '', ?Token $relatedToken = null): Token
289289
{
290290
$token = new Token(
291291
$type,

src/Token/Tokens.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public function toArray(): array
9898
}
9999

100100
/**
101-
* @param int|string|array<int|string> $type
101+
* @param string|string[] $type
102102
*/
103-
public function findNext(int|string|array $type, int $start, ?int $end = null, bool $exclude = false): int|false
103+
public function findNext(string|array $type, int $start, ?int $end = null, bool $exclude = false): int|false
104104
{
105105
$end ??= $this->tokenCount;
106106
for ($i = $start; $i < $end; ++$i) {
@@ -113,9 +113,9 @@ public function findNext(int|string|array $type, int $start, ?int $end = null, b
113113
}
114114

115115
/**
116-
* @param int|string|array<int|string> $type
116+
* @param string|string[] $type
117117
*/
118-
public function findPrevious(int|string|array $type, int $start, int $end = 0, bool $exclude = false): int|false
118+
public function findPrevious(string|array $type, int $start, int $end = 0, bool $exclude = false): int|false
119119
{
120120
for ($i = $start; $i >= $end; --$i) {
121121
if ($exclude !== $this->get($i)->isMatching($type)) {

tests/Token/Tokenizer/TokenizerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function testTokenizeTypes(string $filePath, array $expectedTokenTypes):
151151
static::fail($diff);
152152
}
153153

154-
$tokenTypes = array_map(static fn (Token $token): int|string => $token->getType(), $tokens->toArray());
154+
$tokenTypes = array_map(static fn (Token $token): string => $token->getType(), $tokens->toArray());
155155
static::assertSame($expectedTokenTypes, $tokenTypes);
156156
static::assertTrue($tokens->isReadOnly());
157157
}

0 commit comments

Comments
 (0)