Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 877b8f2

Browse files
mvorisekondrejmirtes
authored andcommitted
simplify/unify parseGeneric method
1 parent 8202c44 commit 877b8f2

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

‎src/Parser/TypeParser.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -398,42 +398,33 @@ public function isHtml(TokenIterator $tokens): bool
398398
public function parseGeneric(TokenIterator $tokens, Ast\Type\IdentifierTypeNode $baseType): Ast\Type\GenericTypeNode
399399
{
400400
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_ANGLE_BRACKET);
401-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
402401

402+
$startLine = $baseType->getAttribute(Ast\Attribute::START_LINE);
403+
$startIndex = $baseType->getAttribute(Ast\Attribute::START_INDEX);
403404
$genericTypes = [];
404405
$variances = [];
405406

406-
[$genericTypes[], $variances[]] = $this->parseGenericTypeArgument($tokens);
407-
408-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
409-
410-
while ($tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
407+
$isFirst = true;
408+
while ($isFirst || $tokens->tryConsumeTokenType(Lexer::TOKEN_COMMA)) {
411409
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
412-
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET)) {
413-
// trailing comma case
414-
$type = new Ast\Type\GenericTypeNode($baseType, $genericTypes, $variances);
415-
$startLine = $baseType->getAttribute(Ast\Attribute::START_LINE);
416-
$startIndex = $baseType->getAttribute(Ast\Attribute::START_INDEX);
417-
if ($startLine !== null && $startIndex !== null) {
418-
$type = $this->enrichWithAttributes($tokens, $type, $startLine, $startIndex);
419-
}
420410

421-
return $type;
411+
// trailing comma case
412+
if (!$isFirst && $tokens->isCurrentTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET)) {
413+
break;
422414
}
415+
$isFirst = false;
416+
423417
[$genericTypes[], $variances[]] = $this->parseGenericTypeArgument($tokens);
424-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
425418
}
426419

427-
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
428-
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
429-
430420
$type = new Ast\Type\GenericTypeNode($baseType, $genericTypes, $variances);
431-
$startLine = $baseType->getAttribute(Ast\Attribute::START_LINE);
432-
$startIndex = $baseType->getAttribute(Ast\Attribute::START_INDEX);
433421
if ($startLine !== null && $startIndex !== null) {
434422
$type = $this->enrichWithAttributes($tokens, $type, $startLine, $startIndex);
435423
}
436424

425+
$tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
426+
$tokens->consumeTokenType(Lexer::TOKEN_CLOSE_ANGLE_BRACKET);
427+
437428
return $type;
438429
}
439430

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /