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 9d57f3d

Browse files
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)
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
/**

0 commit comments

Comments
(0)

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