Skip to content

Commit f23cd90

Browse files
committed
support comments in array shape
use older syntax linting + stanning revert missing file remove typehint lint stan stan comments as attributes revert some stuff revert some stuff missing file styles fix make public, change const name restore tests undo multiline ignore single lines comments preceded by : add comments to arrayshapeitems, not arrayshape whoops fix various things support comment after comma add test fix order remove unused remove unused restore start on format-preserving printer comment support remove addComments support multiline comments in add flow remove comments to the right stuff support changing of comments support editing of comments delayed add fix tests remove comments handle multiple comments clean up lint lint add nowdoc stanning linting, stanning add visibility use nowdoc in a few more places set up test for adding comment to object lint remove debug thing gather comments in TokenIterator move comment handling to TokenIterator use nowdoc add consumeAll flag add more tests for array shape comments add test file for object Allow asserting the type of `$this` Update actions/checkout action to v4 Make the CI job pass after upgrading PHPStan to v1.10.34 simplify/unify parseGeneric method fix/unify callable template parsing with EOL Allow conditional type in callable return type fix template Revert "fix template" This reverts commit 655d968. restore baseline add tests for comments on callable introduce tryConsumeTokenTypeAll single-line comments only simplify getReformattedText lint linting lint
1 parent e540adc commit f23cd90

23 files changed

+1730
-551
lines changed

.github/workflows/apiref.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
steps:
2020
- name: "Checkout"
21-
uses: actions/checkout@v3
21+
uses: actions/checkout@v4
2222

2323
- name: "Install PHP"
2424
uses: "shivammathur/setup-php@v2"

.github/workflows/backward-compatibility.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
steps:
1919
- name: "Checkout"
20-
uses: actions/checkout@v3
20+
uses: actions/checkout@v4
2121
with:
2222
fetch-depth: 0
2323

.github/workflows/build.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525

2626
steps:
2727
- name: "Checkout"
28-
uses: actions/checkout@v3
28+
uses: actions/checkout@v4
2929

3030
- name: "Install PHP"
3131
uses: "shivammathur/setup-php@v2"
@@ -53,10 +53,10 @@ jobs:
5353

5454
steps:
5555
- name: "Checkout"
56-
uses: actions/checkout@v3
56+
uses: actions/checkout@v4
5757

5858
- name: "Checkout build-cs"
59-
uses: actions/checkout@v3
59+
uses: actions/checkout@v4
6060
with:
6161
repository: "phpstan/build-cs"
6262
path: "build-cs"
@@ -104,7 +104,7 @@ jobs:
104104

105105
steps:
106106
- name: "Checkout"
107-
uses: actions/checkout@v3
107+
uses: actions/checkout@v4
108108

109109
- name: "Install PHP"
110110
uses: "shivammathur/setup-php@v2"
@@ -144,7 +144,7 @@ jobs:
144144

145145
steps:
146146
- name: "Checkout"
147-
uses: actions/checkout@v3
147+
uses: actions/checkout@v4
148148

149149
- name: "Install PHP"
150150
uses: "shivammathur/setup-php@v2"

.github/workflows/create-tag.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
runs-on: "ubuntu-latest"
2222
steps:
2323
- name: "Checkout"
24-
uses: actions/checkout@v3
24+
uses: actions/checkout@v4
2525
with:
2626
fetch-depth: 0
2727
token: ${{ secrets.PHPSTAN_BOT_TOKEN }}

.github/workflows/merge-maintained-branch.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- name: "Checkout"
16-
uses: actions/checkout@v3
16+
uses: actions/checkout@v4
1717
- name: "Merge branch"
1818
uses: everlytic/branch-merge@1.1.5
1919
with:

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
steps:
1616
- name: "Checkout"
17-
uses: actions/checkout@v3
17+
uses: actions/checkout@v4
1818

1919
- name: Generate changelog
2020
id: changelog

.github/workflows/send-pr.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
php-version: "8.1"
1919

2020
- name: "Checkout phpstan-src"
21-
uses: actions/checkout@v3
21+
uses: actions/checkout@v4
2222
with:
2323
repository: phpstan/phpstan-src
2424
path: phpstan-src

.github/workflows/test-slevomat-coding-standard.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ jobs:
2525

2626
steps:
2727
- name: "Checkout"
28-
uses: actions/checkout@v3
28+
uses: actions/checkout@v4
2929

3030
- name: "Checkout Slevomat Coding Standard"
31-
uses: actions/checkout@v3
31+
uses: actions/checkout@v4
3232
with:
3333
repository: slevomat/coding-standard
3434
path: slevomat-cs

phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ parameters:
1010
count: 1
1111
path: src/Ast/NodeTraverser.php
1212

13-
-
14-
message: "#^Strict comparison using \\=\\=\\= between 2 and 2 will always evaluate to true\\.$#"
15-
count: 2
16-
path: src/Ast/NodeTraverser.php
17-
1813
-
1914
message: "#^Variable property access on PHPStan\\\\PhpDocParser\\\\Ast\\\\Node\\.$#"
2015
count: 1

src/Ast/Attribute.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ final class Attribute
1313

1414
public const ORIGINAL_NODE = 'originalNode';
1515

16+
public const COMMENTS = 'comments';
17+
1618
}

src/Ast/Comment.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\PhpDocParser\Ast;
4+
5+
use function trim;
6+
7+
class Comment
8+
{
9+
10+
/** @var string */
11+
public $text;
12+
13+
/** @var int */
14+
public $startLine;
15+
16+
/** @var int */
17+
public $startIndex;
18+
19+
public function __construct(string $text, int $startLine = -1, int $startIndex = -1)
20+
{
21+
$this->text = $text;
22+
$this->startLine = $startLine;
23+
$this->startIndex = $startIndex;
24+
}
25+
26+
public function getReformattedText(): ?string
27+
{
28+
return trim($this->text);
29+
}
30+
31+
}

src/Lexer/Lexer.php

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class Lexer
5050
public const TOKEN_NEGATED = 35;
5151
public const TOKEN_ARROW = 36;
5252

53+
public const TOKEN_COMMENT = 37;
54+
5355
public const TOKEN_LABELS = [
5456
self::TOKEN_REFERENCE => '\'&\'',
5557
self::TOKEN_UNION => '\'|\'',
@@ -65,6 +67,7 @@ class Lexer
6567
self::TOKEN_OPEN_CURLY_BRACKET => '\'{\'',
6668
self::TOKEN_CLOSE_CURLY_BRACKET => '\'}\'',
6769
self::TOKEN_COMMA => '\',\'',
70+
self::TOKEN_COMMENT => '\'//\'',
6871
self::TOKEN_COLON => '\':\'',
6972
self::TOKEN_VARIADIC => '\'...\'',
7073
self::TOKEN_DOUBLE_COLON => '\'::\'',
@@ -160,6 +163,7 @@ private function generateRegexp(): string
160163
self::TOKEN_CLOSE_CURLY_BRACKET => '\\}',
161164

162165
self::TOKEN_COMMA => ',',
166+
self::TOKEN_COMMENT => '((?<![:/])\/\/[^\n]*)',
163167
self::TOKEN_VARIADIC => '\\.\\.\\.',
164168
self::TOKEN_DOUBLE_COLON => '::',
165169
self::TOKEN_DOUBLE_ARROW => '=>',

src/Parser/PhpDocParser.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -1127,15 +1127,13 @@ private function parseAssertParameter(TokenIterator $tokens): array
11271127
{
11281128
if ($tokens->isCurrentTokenType(Lexer::TOKEN_THIS_VARIABLE)) {
11291129
$parameter = '$this';
1130-
$requirePropertyOrMethod = true;
11311130
$tokens->next();
11321131
} else {
11331132
$parameter = $tokens->currentTokenValue();
1134-
$requirePropertyOrMethod = false;
11351133
$tokens->consumeTokenType(Lexer::TOKEN_VARIABLE);
11361134
}
11371135

1138-
if ($requirePropertyOrMethod || $tokens->isCurrentTokenType(Lexer::TOKEN_ARROW)) {
1136+
if ($tokens->isCurrentTokenType(Lexer::TOKEN_ARROW)) {
11391137
$tokens->consumeTokenType(Lexer::TOKEN_ARROW);
11401138

11411139
$propertyOrMethod = $tokens->currentTokenValue();

src/Parser/TokenIterator.php

+35-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\PhpDocParser\Parser;
44

55
use LogicException;
6+
use PHPStan\PhpDocParser\Ast\Comment;
67
use PHPStan\PhpDocParser\Lexer\Lexer;
78
use function array_pop;
89
use function assert;
@@ -17,14 +18,19 @@ class TokenIterator
1718
/** @var list<array{string, int, int}> */
1819
private $tokens;
1920

21+
/** @var array<Comment> */
22+
private $comments = [];
23+
2024
/** @var int */
2125
private $index;
2226

2327
/** @var int[] */
2428
private $savePoints = [];
2529

2630
/** @var list<int> */
27-
private $skippedTokenTypes = [Lexer::TOKEN_HORIZONTAL_WS];
31+
private $skippedTokenTypes = [
32+
Lexer::TOKEN_HORIZONTAL_WS,
33+
Lexer::TOKEN_COMMENT];
2834

2935
/** @var string|null */
3036
private $newline = null;
@@ -154,8 +160,7 @@ public function consumeTokenType(int $tokenType): void
154160
}
155161
}
156162

157-
$this->index++;
158-
$this->skipIrrelevantTokens();
163+
$this->next();
159164
}
160165

161166

@@ -168,8 +173,7 @@ public function consumeTokenValue(int $tokenType, string $tokenValue): void
168173
$this->throwError($tokenType, $tokenValue);
169174
}
170175

171-
$this->index++;
172-
$this->skipIrrelevantTokens();
176+
$this->next();
173177
}
174178

175179

@@ -180,12 +184,30 @@ public function tryConsumeTokenValue(string $tokenValue): bool
180184
return false;
181185
}
182186

183-
$this->index++;
184-
$this->skipIrrelevantTokens();
187+
$this->next();
185188

186189
return true;
187190
}
188191

192+
/**
193+
* @return Comment[]
194+
*/
195+
public function flushComments(): array
196+
{
197+
$res = $this->comments;
198+
$this->comments = [];
199+
return $res;
200+
}
201+
202+
/** @phpstan-impure */
203+
public function tryConsumeTokenTypeAll(int $tokenType): bool
204+
{
205+
$found = false;
206+
while ($this->tryConsumeTokenType($tokenType)) {
207+
$found = true;
208+
}
209+
return $found;
210+
}
189211

190212
/** @phpstan-impure */
191213
public function tryConsumeTokenType(int $tokenType): bool
@@ -200,8 +222,7 @@ public function tryConsumeTokenType(int $tokenType): bool
200222
}
201223
}
202224

203-
$this->index++;
204-
$this->skipIrrelevantTokens();
225+
$this->next();
205226

206227
return true;
207228
}
@@ -256,6 +277,11 @@ private function skipIrrelevantTokens(): void
256277
if (!isset($this->tokens[$this->index + 1])) {
257278
break;
258279
}
280+
281+
if ($this->currentTokenType() === Lexer::TOKEN_COMMENT) {
282+
$this->comments[] = new Comment($this->currentTokenValue(), $this->currentTokenLine(), $this->currentTokenIndex());
283+
}
284+
259285
$this->index++;
260286
}
261287
}
@@ -299,7 +325,6 @@ public function rollback(): void
299325
$this->index = $index;
300326
}
301327

302-
303328
/**
304329
* @throws ParserException
305330
*/

0 commit comments

Comments
 (0)