Skip to content

Commit c5cdc3b

Browse files
committed
fix various things
1 parent 4e6d80e commit c5cdc3b

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/Parser/TypeParser.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ class TypeParser
2929
private $useIndexAttributes;
3030

3131
/**
32-
* @param array{lines?: bool, indexes?: bool, comments?: bool} $usedAttributes
32+
* @param array{
33+
* lines?: bool,
34+
* indexes?: bool,
35+
* comments?: bool
36+
* } $usedAttributes
3337
*/
3438
public function __construct(
3539
?ConstExprParser $constExprParser = null,
@@ -71,25 +75,25 @@ public function parse(TokenIterator $tokens): Ast\Type\TypeNode
7175
* @template T of Ast\Node
7276
* @param T $type
7377
* @param list<Ast\Comment> $comments
78+
*
7479
* @return T
7580
*/
7681
public function enrichWithAttributes(TokenIterator $tokens, Ast\Node $type, int $startLine, int $startIndex, array $comments = []): Ast\Node
7782
{
78-
if ($tokens->currentTokenType() === Lexer::TOKEN_COMMENT) {
79-
$comments[] = new Ast\Comment(
80-
$tokens->currentTokenValue(),
81-
$tokens->currentTokenLine(),
82-
$tokens->currentTokenIndex()
83-
);
84-
$tokens->next();
85-
}
86-
8783
if ($this->useLinesAttributes) {
8884
$type->setAttribute(Ast\Attribute::START_LINE, $startLine);
8985
$type->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine());
9086
}
9187

9288
if ($this->useCommentsAttributes) {
89+
if ($tokens->currentTokenType() === Lexer::TOKEN_COMMENT) {
90+
$comments[] = new Ast\Comment(
91+
$tokens->currentTokenValue(),
92+
$tokens->currentTokenLine(),
93+
$tokens->currentTokenIndex()
94+
);
95+
$tokens->next();
96+
}
9397
$type->setAttribute(Ast\Attribute::COMMENTS, $comments);
9498
}
9599

@@ -754,6 +758,11 @@ private function tryParseArrayOrOffsetAccess(TokenIterator $tokens, Ast\Type\Typ
754758
return $type;
755759
}
756760

761+
762+
/**
763+
* @phpstan-impure
764+
* @param Ast\Type\ArrayShapeNode::KIND_* $kind
765+
*/
757766
private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type, string $kind): Ast\Type\ArrayShapeNode
758767
{
759768
$tokens->consumeTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET);
@@ -785,12 +794,14 @@ private function parseArrayShape(TokenIterator $tokens, Ast\Type\TypeNode $type,
785794
return new Ast\Type\ArrayShapeNode($items, $sealed, $kind);
786795
}
787796

797+
788798
/** @phpstan-impure */
789799
private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShapeItemNode
790800
{
791801
$startLine = $tokens->currentTokenLine();
792802
$startIndex = $tokens->currentTokenIndex();
793803

804+
// parse any comments above the item
794805
$comments = [];
795806
while (1) {
796807
if ($tokens->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
@@ -809,6 +820,12 @@ private function parseArrayShapeItem(TokenIterator $tokens): Ast\Type\ArrayShape
809820
$optional = $tokens->tryConsumeTokenType(Lexer::TOKEN_NULLABLE);
810821
$tokens->consumeTokenType(Lexer::TOKEN_COLON);
811822
$value = $this->parse($tokens);
823+
824+
// parse comment to the right of the item
825+
if ($tokens->currentTokenType() === Lexer::TOKEN_COMMENT) {
826+
$comments[] = new Ast\Comment($tokens->currentTokenValue(), $tokens->currentTokenLine(), $tokens->currentTokenIndex());
827+
$tokens->next();
828+
}
812829
$tokens->dropSavePoint();
813830

814831
return $this->enrichWithAttributes(

tests/PHPStan/Parser/TypeParserTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ public function provideParseData(): array
207207
[
208208
'string // with comment after',
209209
new IdentifierTypeNode('string'),
210+
Lexer::TOKEN_COMMENT,
210211
],
211212
[
212213
'string',

0 commit comments

Comments
 (0)