Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9d57f3d

Browse files
committedSep 7, 2024··
Use one ParserConfig object across all parser-involved classes
1 parent 938b7fa commit 9d57f3d

14 files changed

+146
-111
lines changed
 

‎README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
3434
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
3535
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
3636
use PHPStan\PhpDocParser\Lexer\Lexer;
37+
use PHPStan\PhpDocParser\ParserConfig;
3738
use PHPStan\PhpDocParser\Parser\ConstExprParser;
3839
use PHPStan\PhpDocParser\Parser\PhpDocParser;
3940
use PHPStan\PhpDocParser\Parser\TokenIterator;
4041
use PHPStan\PhpDocParser\Parser\TypeParser;
4142

4243
// basic setup
4344

44-
$lexer = new Lexer();
45-
$constExprParser = new ConstExprParser();
46-
$typeParser = new TypeParser($constExprParser);
47-
$phpDocParser = new PhpDocParser($typeParser, $constExprParser);
45+
$config = new ParserConfig(usedAttributes: []);
46+
$lexer = new Lexer($config);
47+
$constExprParser = new ConstExprParser($config);
48+
$typeParser = new TypeParser($config, $constExprParser);
49+
$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);
4850

4951
// parsing and reading a PHPDoc string
5052

@@ -72,6 +74,7 @@ use PHPStan\PhpDocParser\Ast\NodeVisitor\CloningVisitor;
7274
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
7375
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
7476
use PHPStan\PhpDocParser\Lexer\Lexer;
77+
use PHPStan\PhpDocParser\ParserConfig;
7578
use PHPStan\PhpDocParser\Parser\ConstExprParser;
7679
use PHPStan\PhpDocParser\Parser\PhpDocParser;
7780
use PHPStan\PhpDocParser\Parser\TokenIterator;
@@ -80,12 +83,11 @@ use PHPStan\PhpDocParser\Printer\Printer;
8083

8184
// basic setup with enabled required lexer attributes
8285

83-
$usedAttributes = ['lines' => true, 'indexes' => true];
84-
85-
$lexer = new Lexer();
86-
$constExprParser = new ConstExprParser(true, true, $usedAttributes);
87-
$typeParser = new TypeParser($constExprParser, true, $usedAttributes);
88-
$phpDocParser = new PhpDocParser($typeParser, $constExprParser, true, true, $usedAttributes);
86+
$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true]);
87+
$lexer = new Lexer($config);
88+
$constExprParser = new ConstExprParser($config);
89+
$typeParser = new TypeParser($config, $constExprParser);
90+
$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);
8991

9092
$tokens = new TokenIterator($lexer->tokenize('/** @param Lorem $a */'));
9193
$phpDocNode = $phpDocParser->parse($tokens); // PhpDocNode

‎src/Lexer/Lexer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\PhpDocParser\Lexer;
44

5+
use PHPStan\PhpDocParser\ParserConfig;
56
use function implode;
67
use function preg_match_all;
78
use const PREG_SET_ORDER;
@@ -94,8 +95,16 @@ class Lexer
9495
public const TYPE_OFFSET = 1;
9596
public const LINE_OFFSET = 2;
9697

98+
private ParserConfig $config; // @phpstan-ignore property.onlyWritten
99+
97100
private ?string $regexp = null;
98101

102+
public function __construct(ParserConfig $config)

Comments cannot be loaded right now

Refresh the page or try again later

Code has comments. Press enter to view.
103+
{
104+
$this->config = $config;
105+
}
106+
107+
99108
/**
100109
* @return list<array{string, int, int}>
101110
*/

‎src/Parser/ConstExprParser.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,22 @@
44

55
use PHPStan\PhpDocParser\Ast;
66
use PHPStan\PhpDocParser\Lexer\Lexer;
7+
use PHPStan\PhpDocParser\ParserConfig;
78
use function str_replace;
89
use function strtolower;
910

1011
class ConstExprParser
1112
{
1213

13-
private bool $useLinesAttributes;
14-
15-
private bool $useIndexAttributes;
14+
private ParserConfig $config;
1615

1716
private bool $parseDoctrineStrings;
1817

19-
/**
20-
* @param array{lines?: bool, indexes?: bool} $usedAttributes
21-
*/
2218
public function __construct(
23-
array $usedAttributes = []
19+
ParserConfig $config
2420
)
2521
{
26-
$this->useLinesAttributes = $usedAttributes['lines'] ?? false;
27-
$this->useIndexAttributes = $usedAttributes['indexes'] ?? false;
22+
$this->config = $config;
2823
$this->parseDoctrineStrings = false;
2924
}
3025

@@ -33,12 +28,7 @@ public function __construct(
3328
*/
3429
public function toDoctrine(): self
3530
{
36-
$self = new self(
37-
[
38-
'lines' => $this->useLinesAttributes,
39-
'indexes' => $this->useIndexAttributes,
40-
],
41-
);
31+
$self = new self($this->config);
4232
$self->parseDoctrineStrings = true;
4333
return $self;
4434
}
@@ -286,12 +276,12 @@ private function parseArrayItem(TokenIterator $tokens): Ast\ConstExpr\ConstExprA
286276
*/
287277
private function enrichWithAttributes(TokenIterator $tokens, Ast\ConstExpr\ConstExprNode $node, int $startLine, int $startIndex): Ast\ConstExpr\ConstExprNode
288278
{
289-
if ($this->useLinesAttributes) {
279+
if ($this->config->useLinesAttributes) {
290280
$node->setAttribute(Ast\Attribute::START_LINE, $startLine);
291281
$node->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine());
292282
}
293283

294-
if ($this->useIndexAttributes) {
284+
if ($this->config->useIndexAttributes) {
295285
$node->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
296286
$node->setAttribute(Ast\Attribute::END_INDEX, $tokens->endIndexOfLastRelevantToken());
297287
}

‎src/Parser/PhpDocParser.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine;
1111
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
1212
use PHPStan\PhpDocParser\Lexer\Lexer;
13+
use PHPStan\PhpDocParser\ParserConfig;
1314
use PHPStan\ShouldNotHappenException;
1415
use function array_key_exists;
1516
use function array_values;
@@ -29,30 +30,24 @@ class PhpDocParser
2930
Lexer::TOKEN_INTERSECTION,
3031
];
3132

33+
private ParserConfig $config;
34+
3235
private TypeParser $typeParser;
3336

3437
private ConstExprParser $constantExprParser;
3538

3639
private ConstExprParser $doctrineConstantExprParser;
3740

38-
private bool $useLinesAttributes;
39-
40-
private bool $useIndexAttributes;
41-
42-
/**
43-
* @param array{lines?: bool, indexes?: bool} $usedAttributes
44-
*/
4541
public function __construct(
42+
ParserConfig $config,
4643
TypeParser $typeParser,
47-
ConstExprParser $constantExprParser,
48-
array $usedAttributes = []
44+
ConstExprParser $constantExprParser
4945
)
5046
{
47+
$this->config = $config;
5148
$this->typeParser = $typeParser;
5249
$this->constantExprParser = $constantExprParser;
5350
$this->doctrineConstantExprParser = $constantExprParser->toDoctrine();
54-
$this->useLinesAttributes = $usedAttributes['lines'] ?? false;
55-
$this->useIndexAttributes = $usedAttributes['indexes'] ?? false;
5651
}
5752

5853

@@ -172,12 +167,12 @@ private function parseChild(TokenIterator $tokens): Ast\PhpDoc\PhpDocChildNode
172167
*/
173168
private function enrichWithAttributes(TokenIterator $tokens, Ast\Node $tag, int $startLine, int $startIndex): Ast\Node
174169
{
175-
if ($this->useLinesAttributes) {
170+
if ($this->config->useLinesAttributes) {
176171
$tag->setAttribute(Ast\Attribute::START_LINE, $startLine);
177172
$tag->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine());
178173
}
179174

180-
if ($this->useIndexAttributes) {
175+
if ($this->config->useIndexAttributes) {
181176
$tag->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
182177
$tag->setAttribute(Ast\Attribute::END_INDEX, $tokens->endIndexOfLastRelevantToken());
183178
}

‎src/Parser/TypeParser.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\PhpDocParser\Ast;
77
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
88
use PHPStan\PhpDocParser\Lexer\Lexer;
9+
use PHPStan\PhpDocParser\ParserConfig;
910
use function in_array;
1011
use function str_replace;
1112
use function strlen;
@@ -15,23 +16,17 @@
1516
class TypeParser
1617
{
1718

18-
private ConstExprParser $constExprParser;
19-
20-
private bool $useLinesAttributes;
19+
private ParserConfig $config;
2120

22-
private bool $useIndexAttributes;
21+
private ConstExprParser $constExprParser;
2322

24-
/**
25-
* @param array{lines?: bool, indexes?: bool} $usedAttributes
26-
*/
2723
public function __construct(
28-
ConstExprParser $constExprParser,
29-
array $usedAttributes = []
24+
ParserConfig $config,
25+
ConstExprParser $constExprParser
3026
)
3127
{
28+
$this->config = $config;
3229
$this->constExprParser = $constExprParser;
33-
$this->useLinesAttributes = $usedAttributes['lines'] ?? false;
34-
$this->useIndexAttributes = $usedAttributes['indexes'] ?? false;
3530
}
3631

3732
/** @phpstan-impure */
@@ -64,12 +59,12 @@ public function parse(TokenIterator $tokens): Ast\Type\TypeNode
6459
*/
6560
public function enrichWithAttributes(TokenIterator $tokens, Ast\Node $type, int $startLine, int $startIndex): Ast\Node
6661
{
67-
if ($this->useLinesAttributes) {
62+
if ($this->config->useLinesAttributes) {
6863
$type->setAttribute(Ast\Attribute::START_LINE, $startLine);
6964
$type->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine());
7065
}
7166

72-
if ($this->useIndexAttributes) {
67+
if ($this->config->useIndexAttributes) {
7368
$type->setAttribute(Ast\Attribute::START_INDEX, $startIndex);
7469
$type->setAttribute(Ast\Attribute::END_INDEX, $tokens->endIndexOfLastRelevantToken());
7570
}

‎src/ParserConfig.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\PhpDocParser;
4+
5+
class ParserConfig
6+
{
7+
8+
public bool $useLinesAttributes;
9+
10+
public bool $useIndexAttributes;
11+
12+
/**
13+
* @param array{lines?: bool, indexes?: bool} $usedAttributes
14+
*/
15+
public function __construct(array $usedAttributes)
16+
{
17+
$this->useLinesAttributes = $usedAttributes['lines'] ?? false;
18+
$this->useIndexAttributes = $usedAttributes['indexes'] ?? false;
19+
}
20+
21+
}

‎tests/PHPStan/Ast/Attributes/AttributesTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\PhpDocParser\Parser\PhpDocParser;
99
use PHPStan\PhpDocParser\Parser\TokenIterator;
1010
use PHPStan\PhpDocParser\Parser\TypeParser;
11+
use PHPStan\PhpDocParser\ParserConfig;
1112
use PHPUnit\Framework\TestCase;
1213

1314
final class AttributesTest extends TestCase
@@ -18,9 +19,11 @@ final class AttributesTest extends TestCase
1819
protected function setUp(): void
1920
{
2021
parent::setUp();
21-
$lexer = new Lexer();
22-
$constExprParser = new ConstExprParser();
23-
$phpDocParser = new PhpDocParser(new TypeParser($constExprParser), $constExprParser);
22+
23+
$config = new ParserConfig([]);
24+
$lexer = new Lexer($config);
25+
$constExprParser = new ConstExprParser($config);
26+
$phpDocParser = new PhpDocParser($config, new TypeParser($config, $constExprParser), $constExprParser);
2427

2528
$input = '/** @var string */';
2629
$tokens = new TokenIterator($lexer->tokenize($input));

‎tests/PHPStan/Parser/ConstExprParserTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
1717
use PHPStan\PhpDocParser\Ast\NodeTraverser;
1818
use PHPStan\PhpDocParser\Lexer\Lexer;
19+
use PHPStan\PhpDocParser\ParserConfig;
1920
use PHPUnit\Framework\TestCase;
2021

2122
class ConstExprParserTest extends TestCase
@@ -28,8 +29,9 @@ class ConstExprParserTest extends TestCase
2829
protected function setUp(): void
2930
{
3031
parent::setUp();
31-
$this->lexer = new Lexer();
32-
$this->constExprParser = new ConstExprParser();
32+
$config = new ParserConfig([]);
33+
$this->lexer = new Lexer($config);
34+
$this->constExprParser = new ConstExprParser($config);
3335
}
3436

3537

@@ -67,10 +69,11 @@ public function testParse(string $input, ConstExprNode $expectedExpr, int $nextT
6769
public function testVerifyAttributes(string $input): void
6870
{
6971
$tokens = new TokenIterator($this->lexer->tokenize($input));
70-
$constExprParser = new ConstExprParser([
72+
$config = new ParserConfig([
7173
'lines' => true,
7274
'indexes' => true,
7375
]);
76+
$constExprParser = new ConstExprParser($config);
7477
$visitor = new NodeCollectingVisitor();
7578
$traverser = new NodeTraverser([$visitor]);
7679
$traverser->traverse([$constExprParser->parse($tokens)]);

‎tests/PHPStan/Parser/FuzzyTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Iterator;
66
use PHPStan\PhpDocParser\Lexer\Lexer;
7+
use PHPStan\PhpDocParser\ParserConfig;
78
use PHPUnit\Framework\TestCase;
89
use Symfony\Component\Process\Process;
910
use function file_get_contents;
@@ -28,9 +29,10 @@ class FuzzyTest extends TestCase
2829
protected function setUp(): void
2930
{
3031
parent::setUp();
31-
$this->lexer = new Lexer();
32-
$this->typeParser = new TypeParser(new ConstExprParser());
33-
$this->constExprParser = new ConstExprParser();
32+
$config = new ParserConfig([]);
33+
$this->lexer = new Lexer($config);
34+
$this->typeParser = new TypeParser($config, new ConstExprParser($config));
35+
$this->constExprParser = new ConstExprParser($config);
3436
}
3537

3638
/**

‎tests/PHPStan/Parser/PhpDocParserTest.php

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode;
6565
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
6666
use PHPStan\PhpDocParser\Lexer\Lexer;
67+
use PHPStan\PhpDocParser\ParserConfig;
6768
use PHPUnit\Framework\TestCase;
6869
use function count;
6970
use function sprintf;
@@ -79,10 +80,11 @@ class PhpDocParserTest extends TestCase
7980
protected function setUp(): void
8081
{
8182
parent::setUp();
82-
$this->lexer = new Lexer();
83-
$constExprParser = new ConstExprParser();
84-
$typeParser = new TypeParser($constExprParser);
85-
$this->phpDocParser = new PhpDocParser($typeParser, $constExprParser, []);
83+
$config = new ParserConfig([]);
84+
$this->lexer = new Lexer($config);
85+
$constExprParser = new ConstExprParser($config);
86+
$typeParser = new TypeParser($config, $constExprParser);
87+
$this->phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);
8688
}
8789

8890

@@ -6807,13 +6809,13 @@ public function dataLinesAndIndexes(): iterable
68076809
public function testLinesAndIndexes(string $phpDoc, array $childrenLines): void
68086810
{
68096811
$tokens = new TokenIterator($this->lexer->tokenize($phpDoc));
6810-
$usedAttributes = [
6812+
$config = new ParserConfig([
68116813
'lines' => true,
68126814
'indexes' => true,
6813-
];
6814-
$constExprParser = new ConstExprParser($usedAttributes);
6815-
$typeParser = new TypeParser($constExprParser, $usedAttributes);
6816-
$phpDocParser = new PhpDocParser($typeParser, $constExprParser, $usedAttributes);
6815+
]);
6816+
$constExprParser = new ConstExprParser($config);
6817+
$typeParser = new TypeParser($config, $constExprParser);
6818+
$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);
68176819
$phpDocNode = $phpDocParser->parse($tokens);
68186820
$children = $phpDocNode->children;
68196821
$this->assertCount(count($childrenLines), $children);
@@ -6891,13 +6893,13 @@ public function dataDeepNodesLinesAndIndexes(): iterable
68916893
public function testDeepNodesLinesAndIndexes(string $phpDoc, array $nodeAttributes): void
68926894
{
68936895
$tokens = new TokenIterator($this->lexer->tokenize($phpDoc));
6894-
$usedAttributes = [
6896+
$config = new ParserConfig([
68956897
'lines' => true,
68966898
'indexes' => true,
6897-
];
6898-
$constExprParser = new ConstExprParser($usedAttributes);
6899-
$typeParser = new TypeParser($constExprParser, $usedAttributes);
6900-
$phpDocParser = new PhpDocParser($typeParser, $constExprParser, $usedAttributes);
6899+
]);
6900+
$constExprParser = new ConstExprParser($config);
6901+
$typeParser = new TypeParser($config, $constExprParser);
6902+
$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);
69016903
$visitor = new NodeCollectingVisitor();
69026904
$traverser = new NodeTraverser([$visitor]);
69036905
$traverser->traverse([$phpDocParser->parse($tokens)]);
@@ -6964,13 +6966,13 @@ public function dataReturnTypeLinesAndIndexes(): iterable
69646966
public function testReturnTypeLinesAndIndexes(string $phpDoc, array $lines): void
69656967
{
69666968
$tokens = new TokenIterator($this->lexer->tokenize($phpDoc));
6967-
$usedAttributes = [
6969+
$config = new ParserConfig([
69686970
'lines' => true,
69696971
'indexes' => true,
6970-
];
6971-
$constExprParser = new ConstExprParser($usedAttributes);
6972-
$typeParser = new TypeParser($constExprParser, $usedAttributes);
6973-
$phpDocParser = new PhpDocParser($typeParser, $constExprParser, $usedAttributes);
6972+
]);
6973+
$constExprParser = new ConstExprParser($config);
6974+
$typeParser = new TypeParser($config, $constExprParser);
6975+
$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);
69746976
$phpDocNode = $phpDocParser->parse($tokens);
69756977
$returnTag = $phpDocNode->getReturnTagValues()[0];
69766978
$type = $returnTag->type;
@@ -7014,10 +7016,13 @@ public function testReturnTypeLinesAndIndexes(string $phpDoc, array $lines): voi
70147016
*/
70157017
public function testVerifyAttributes(string $label, string $input): void
70167018
{
7017-
$usedAttributes = ['lines' => true, 'indexes' => true];
7018-
$constExprParser = new ConstExprParser($usedAttributes);
7019-
$typeParser = new TypeParser($constExprParser, $usedAttributes);
7020-
$phpDocParser = new PhpDocParser($typeParser, $constExprParser, $usedAttributes);
7019+
$config = new ParserConfig([
7020+
'lines' => true,
7021+
'indexes' => true,
7022+
]);
7023+
$constExprParser = new ConstExprParser($config);
7024+
$typeParser = new TypeParser($config, $constExprParser);
7025+
$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);
70217026
$tokens = new TokenIterator($this->lexer->tokenize($input));
70227027

70237028
$visitor = new NodeCollectingVisitor();
@@ -7344,9 +7349,10 @@ public function testTextBetweenTagsBelongsToDescription(
73447349
PhpDocNode $expectedPhpDocNode
73457350
): void
73467351
{
7347-
$constExprParser = new ConstExprParser();
7348-
$typeParser = new TypeParser($constExprParser);
7349-
$phpDocParser = new PhpDocParser($typeParser, $constExprParser, []);
7352+
$config = new ParserConfig([]);
7353+
$constExprParser = new ConstExprParser($config);
7354+
$typeParser = new TypeParser($config, $constExprParser);
7355+
$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);
73507356

73517357
$tokens = new TokenIterator($this->lexer->tokenize($input));
73527358
$actualPhpDocNode = $phpDocParser->parse($tokens);

‎tests/PHPStan/Parser/TokenIteratorTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\PhpDocParser\Parser;
44

55
use PHPStan\PhpDocParser\Lexer\Lexer;
6+
use PHPStan\PhpDocParser\ParserConfig;
67
use PHPUnit\Framework\TestCase;
78
use const PHP_EOL;
89

@@ -46,11 +47,12 @@ public function dataGetDetectedNewline(): iterable
4647
*/
4748
public function testGetDetectedNewline(string $phpDoc, ?string $expectedNewline): void
4849
{
49-
$lexer = new Lexer();
50+
$config = new ParserConfig([]);
51+
$lexer = new Lexer($config);
5052
$tokens = new TokenIterator($lexer->tokenize($phpDoc));
51-
$constExprParser = new ConstExprParser();
52-
$typeParser = new TypeParser($constExprParser);
53-
$phpDocParser = new PhpDocParser($typeParser, $constExprParser);
53+
$constExprParser = new ConstExprParser($config);
54+
$typeParser = new TypeParser($config, $constExprParser);
55+
$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser);
5456
$phpDocParser->parse($tokens);
5557
$this->assertSame($expectedNewline, $tokens->getDetectedNewline());
5658
}

‎tests/PHPStan/Parser/TypeParserTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
3232
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
3333
use PHPStan\PhpDocParser\Lexer\Lexer;
34+
use PHPStan\PhpDocParser\ParserConfig;
3435
use PHPStan\PhpDocParser\Printer\Printer;
3536
use PHPUnit\Framework\TestCase;
3637
use function get_class;
@@ -47,8 +48,9 @@ class TypeParserTest extends TestCase
4748
protected function setUp(): void
4849
{
4950
parent::setUp();
50-
$this->lexer = new Lexer();
51-
$this->typeParser = new TypeParser(new ConstExprParser());
51+
$config = new ParserConfig([]);
52+
$this->lexer = new Lexer($config);
53+
$this->typeParser = new TypeParser($config, new ConstExprParser($config));
5254
}
5355

5456

@@ -115,8 +117,8 @@ public function testVerifyAttributes(string $input, $expectedResult): void
115117
$this->expectExceptionMessage($expectedResult->getMessage());
116118
}
117119

118-
$usedAttributes = ['lines' => true, 'indexes' => true];
119-
$typeParser = new TypeParser(new ConstExprParser($usedAttributes), $usedAttributes);
120+
$config = new ParserConfig(['lines' => true, 'indexes' => true]);
121+
$typeParser = new TypeParser($config, new ConstExprParser($config));
120122
$tokens = new TokenIterator($this->lexer->tokenize($input));
121123

122124
$visitor = new NodeCollectingVisitor();
@@ -3069,11 +3071,11 @@ public function testLinesAndIndexes(string $input, array $assertions): void
30693071
{
30703072
$tokensArray = $this->lexer->tokenize($input);
30713073
$tokens = new TokenIterator($tokensArray);
3072-
$usedAttributes = [
3074+
$config = new ParserConfig([
30733075
'lines' => true,
30743076
'indexes' => true,
3075-
];
3076-
$typeParser = new TypeParser(new ConstExprParser(), $usedAttributes);
3077+
]);
3078+
$typeParser = new TypeParser($config, new ConstExprParser($config));
30773079
$typeNode = $typeParser->parse($tokens);
30783080

30793081
foreach ($assertions as [$callable, $expectedContent, $startLine, $endLine, $startIndex, $endIndex]) {

‎tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PHPStan\PhpDocParser\Parser\PhpDocParser;
2222
use PHPStan\PhpDocParser\Parser\TokenIterator;
2323
use PHPStan\PhpDocParser\Parser\TypeParser;
24+
use PHPStan\PhpDocParser\ParserConfig;
2425
use PHPUnit\Framework\TestCase;
2526
use function file_get_contents;
2627

@@ -98,14 +99,14 @@ public function enterNode(PhpNode $phpNode)
9899

99100
$phpDoc = $phpNode->getDocComment()->getText();
100101

101-
$usedAttributes = ['lines' => true, 'indexes' => true];
102-
$constExprParser = new ConstExprParser($usedAttributes);
102+
$config = new ParserConfig(['lines' => true, 'indexes' => true]);
103+
$constExprParser = new ConstExprParser($config);
103104
$phpDocParser = new PhpDocParser(
104-
new TypeParser($constExprParser, $usedAttributes),
105+
$config,
106+
new TypeParser($config, $constExprParser),
105107
$constExprParser,
106-
$usedAttributes,
107108
);
108-
$lexer = new Lexer();
109+
$lexer = new Lexer($config);
109110
$tokens = new TokenIterator($lexer->tokenize($phpDoc));
110111
$phpDocNode = $phpDocParser->parse($tokens);
111112
$cloningTraverser = new NodeTraverser([new NodeVisitor\CloningVisitor()]);

‎tests/PHPStan/Printer/PrinterTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
use PHPStan\PhpDocParser\Parser\PhpDocParser;
4848
use PHPStan\PhpDocParser\Parser\TokenIterator;
4949
use PHPStan\PhpDocParser\Parser\TypeParser;
50+
use PHPStan\PhpDocParser\ParserConfig;
5051
use PHPUnit\Framework\TestCase;
5152
use function array_pop;
5253
use function array_splice;
@@ -65,13 +66,13 @@ class PrinterTest extends TestCase
6566

6667
protected function setUp(): void
6768
{
68-
$usedAttributes = ['lines' => true, 'indexes' => true];
69-
$constExprParser = new ConstExprParser($usedAttributes);
70-
$this->typeParser = new TypeParser($constExprParser, $usedAttributes);
69+
$config = new ParserConfig(['lines' => true, 'indexes' => true]);
70+
$constExprParser = new ConstExprParser($config);
71+
$this->typeParser = new TypeParser($config, $constExprParser);
7172
$this->phpDocParser = new PhpDocParser(
73+
$config,
7274
$this->typeParser,
7375
$constExprParser,
74-
$usedAttributes,
7576
);
7677
}
7778

@@ -1857,7 +1858,8 @@ public function enterNode(Node $node)
18571858
*/
18581859
public function testPrintFormatPreserving(string $phpDoc, string $expectedResult, NodeVisitor $visitor): void
18591860
{
1860-
$lexer = new Lexer();
1861+
$config = new ParserConfig([]);
1862+
$lexer = new Lexer($config);
18611863
$tokens = new TokenIterator($lexer->tokenize($phpDoc));
18621864
$phpDocNode = $this->phpDocParser->parse($tokens);
18631865
$cloningTraverser = new NodeTraverser([new NodeVisitor\CloningVisitor()]);
@@ -2007,7 +2009,8 @@ public function testPrintType(TypeNode $node, string $expectedResult): void
20072009
$phpDoc = $printer->print($node);
20082010
$this->assertSame($expectedResult, $phpDoc);
20092011

2010-
$lexer = new Lexer();
2012+
$config = new ParserConfig([]);
2013+
$lexer = new Lexer($config);
20112014
$this->assertEquals(
20122015
$this->unsetAttributes($node),
20132016
$this->unsetAttributes($this->typeParser->parse(new TokenIterator($lexer->tokenize($phpDoc)))),
@@ -2043,7 +2046,8 @@ public function testPrintPhpDocNode(PhpDocNode $node, string $expectedResult): v
20432046
$phpDoc = $printer->print($node);
20442047
$this->assertSame($expectedResult, $phpDoc);
20452048

2046-
$lexer = new Lexer();
2049+
$config = new ParserConfig([]);
2050+
$lexer = new Lexer($config);
20472051
$this->assertEquals(
20482052
$this->unsetAttributes($node),
20492053
$this->unsetAttributes($this->phpDocParser->parse(new TokenIterator($lexer->tokenize($phpDoc)))),

0 commit comments

Comments
 (0)
Please sign in to comment.