diff --git a/README.md b/README.md index 99d5ba9f1ba46883e0ab04502400f96f67019926..415c1a5e4b60d6655c0bc73be5de081a1894bc38 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use PHPStan\PhpDocParser\Parser\ConstExprParser; use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; @@ -41,10 +42,11 @@ use PHPStan\PhpDocParser\Parser\TypeParser; // basic setup -$lexer = new Lexer(); -$constExprParser = new ConstExprParser(); -$typeParser = new TypeParser($constExprParser); -$phpDocParser = new PhpDocParser($typeParser, $constExprParser); +$config = new ParserConfig(usedAttributes: []); +$lexer = new Lexer($config); +$constExprParser = new ConstExprParser($config); +$typeParser = new TypeParser($config, $constExprParser); +$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); // parsing and reading a PHPDoc string @@ -72,6 +74,7 @@ use PHPStan\PhpDocParser\Ast\NodeVisitor\CloningVisitor; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use PHPStan\PhpDocParser\Parser\ConstExprParser; use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; @@ -80,12 +83,11 @@ use PHPStan\PhpDocParser\Printer\Printer; // basic setup with enabled required lexer attributes -$usedAttributes = ['lines' => true, 'indexes' => true]; - -$lexer = new Lexer(); -$constExprParser = new ConstExprParser(true, true, $usedAttributes); -$typeParser = new TypeParser($constExprParser, true, $usedAttributes); -$phpDocParser = new PhpDocParser($typeParser, $constExprParser, true, true, $usedAttributes); +$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true]); +$lexer = new Lexer($config); +$constExprParser = new ConstExprParser($config); +$typeParser = new TypeParser($config, $constExprParser); +$phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); $tokens = new TokenIterator($lexer->tokenize('/** @param Lorem $a */')); $phpDocNode = $phpDocParser->parse($tokens); // PhpDocNode diff --git a/src/Lexer/Lexer.php b/src/Lexer/Lexer.php index c090e62af337d57d079dbcca7647f84734cecdfd..be85fa9f468644c1a713eb4b5354518006aedd08 100644 --- a/src/Lexer/Lexer.php +++ b/src/Lexer/Lexer.php @@ -2,6 +2,7 @@ namespace PHPStan\PhpDocParser\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use function implode; use function preg_match_all; use const PREG_SET_ORDER; @@ -94,8 +95,16 @@ class Lexer public const TYPE_OFFSET = 1; public const LINE_OFFSET = 2; + private ParserConfig $config; // @phpstan-ignore property.onlyWritten + private ?string $regexp = null; + public function __construct(ParserConfig $config) + { + $this->config = $config; + } + + /** * @return list<array{string, int, int}> */ diff --git a/src/Parser/ConstExprParser.php b/src/Parser/ConstExprParser.php index de476406c155bcbd14c3133cb6f417e05a013d07..396b8d7c735b4142d5d4b52a8248d90ba3e49410 100644 --- a/src/Parser/ConstExprParser.php +++ b/src/Parser/ConstExprParser.php @@ -4,27 +4,22 @@ namespace PHPStan\PhpDocParser\Parser; use PHPStan\PhpDocParser\Ast; use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use function str_replace; use function strtolower; class ConstExprParser { - private bool $useLinesAttributes; - - private bool $useIndexAttributes; + private ParserConfig $config; private bool $parseDoctrineStrings; - /** - * @param array{lines?: bool, indexes?: bool} $usedAttributes - */ public function __construct( - array $usedAttributes = [] + ParserConfig $config ) { - $this->useLinesAttributes = $usedAttributes['lines'] ?? false; - $this->useIndexAttributes = $usedAttributes['indexes'] ?? false; + $this->config = $config; $this->parseDoctrineStrings = false; } @@ -33,12 +28,7 @@ class ConstExprParser */ public function toDoctrine(): self { - $self = new self( - [ - 'lines' => $this->useLinesAttributes, - 'indexes' => $this->useIndexAttributes, - ], - ); + $self = new self($this->config); $self->parseDoctrineStrings = true; return $self; } @@ -286,12 +276,12 @@ class ConstExprParser */ private function enrichWithAttributes(TokenIterator $tokens, Ast\ConstExpr\ConstExprNode $node, int $startLine, int $startIndex): Ast\ConstExpr\ConstExprNode { - if ($this->useLinesAttributes) { + if ($this->config->useLinesAttributes) { $node->setAttribute(Ast\Attribute::START_LINE, $startLine); $node->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine()); } - if ($this->useIndexAttributes) { + if ($this->config->useIndexAttributes) { $node->setAttribute(Ast\Attribute::START_INDEX, $startIndex); $node->setAttribute(Ast\Attribute::END_INDEX, $tokens->endIndexOfLastRelevantToken()); } diff --git a/src/Parser/PhpDocParser.php b/src/Parser/PhpDocParser.php index 3dd1526443f7d5881acfabf340ed0036add12409..e4aa77b39abb6cfe5cd0fe25b7ef5c98b9ee38c0 100644 --- a/src/Parser/PhpDocParser.php +++ b/src/Parser/PhpDocParser.php @@ -10,6 +10,7 @@ use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode; use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine; use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use PHPStan\ShouldNotHappenException; use function array_key_exists; use function array_values; @@ -29,30 +30,24 @@ class PhpDocParser Lexer::TOKEN_INTERSECTION, ]; + private ParserConfig $config; + private TypeParser $typeParser; private ConstExprParser $constantExprParser; private ConstExprParser $doctrineConstantExprParser; - private bool $useLinesAttributes; - - private bool $useIndexAttributes; - - /** - * @param array{lines?: bool, indexes?: bool} $usedAttributes - */ public function __construct( + ParserConfig $config, TypeParser $typeParser, - ConstExprParser $constantExprParser, - array $usedAttributes = [] + ConstExprParser $constantExprParser ) { + $this->config = $config; $this->typeParser = $typeParser; $this->constantExprParser = $constantExprParser; $this->doctrineConstantExprParser = $constantExprParser->toDoctrine(); - $this->useLinesAttributes = $usedAttributes['lines'] ?? false; - $this->useIndexAttributes = $usedAttributes['indexes'] ?? false; } @@ -172,12 +167,12 @@ class PhpDocParser */ private function enrichWithAttributes(TokenIterator $tokens, Ast\Node $tag, int $startLine, int $startIndex): Ast\Node { - if ($this->useLinesAttributes) { + if ($this->config->useLinesAttributes) { $tag->setAttribute(Ast\Attribute::START_LINE, $startLine); $tag->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine()); } - if ($this->useIndexAttributes) { + if ($this->config->useIndexAttributes) { $tag->setAttribute(Ast\Attribute::START_INDEX, $startIndex); $tag->setAttribute(Ast\Attribute::END_INDEX, $tokens->endIndexOfLastRelevantToken()); } diff --git a/src/Parser/TypeParser.php b/src/Parser/TypeParser.php index a9e5883309033113e2cd79d24eeca67c399bc2e0..a6256473a5152593c1d96b4e58da46f8c65ebddb 100644 --- a/src/Parser/TypeParser.php +++ b/src/Parser/TypeParser.php @@ -6,6 +6,7 @@ use LogicException; use PHPStan\PhpDocParser\Ast; use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode; use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use function in_array; use function str_replace; use function strlen; @@ -15,23 +16,17 @@ use function substr_compare; class TypeParser { - private ConstExprParser $constExprParser; - - private bool $useLinesAttributes; + private ParserConfig $config; - private bool $useIndexAttributes; + private ConstExprParser $constExprParser; - /** - * @param array{lines?: bool, indexes?: bool} $usedAttributes - */ public function __construct( - ConstExprParser $constExprParser, - array $usedAttributes = [] + ParserConfig $config, + ConstExprParser $constExprParser ) { + $this->config = $config; $this->constExprParser = $constExprParser; - $this->useLinesAttributes = $usedAttributes['lines'] ?? false; - $this->useIndexAttributes = $usedAttributes['indexes'] ?? false; } /** @phpstan-impure */ @@ -64,12 +59,12 @@ class TypeParser */ public function enrichWithAttributes(TokenIterator $tokens, Ast\Node $type, int $startLine, int $startIndex): Ast\Node { - if ($this->useLinesAttributes) { + if ($this->config->useLinesAttributes) { $type->setAttribute(Ast\Attribute::START_LINE, $startLine); $type->setAttribute(Ast\Attribute::END_LINE, $tokens->currentTokenLine()); } - if ($this->useIndexAttributes) { + if ($this->config->useIndexAttributes) { $type->setAttribute(Ast\Attribute::START_INDEX, $startIndex); $type->setAttribute(Ast\Attribute::END_INDEX, $tokens->endIndexOfLastRelevantToken()); } diff --git a/src/ParserConfig.php b/src/ParserConfig.php new file mode 100644 index 0000000000000000000000000000000000000000..b3a8889a1b5dcb9e68d5dfb9ec9ef28af32a6dde --- /dev/null +++ b/src/ParserConfig.php @@ -0,0 +1,21 @@ +<?php declare(strict_types = 1); + +namespace PHPStan\PhpDocParser; + +class ParserConfig +{ + + public bool $useLinesAttributes; + + public bool $useIndexAttributes; + + /** + * @param array{lines?: bool, indexes?: bool} $usedAttributes + */ + public function __construct(array $usedAttributes) + { + $this->useLinesAttributes = $usedAttributes['lines'] ?? false; + $this->useIndexAttributes = $usedAttributes['indexes'] ?? false; + } + +} diff --git a/tests/PHPStan/Ast/Attributes/AttributesTest.php b/tests/PHPStan/Ast/Attributes/AttributesTest.php index 8be0d2f2fe9f5c27d539785f59cd144f04edcc24..f513b1fad52a8eabc569f9865834fcdaef9a9f80 100644 --- a/tests/PHPStan/Ast/Attributes/AttributesTest.php +++ b/tests/PHPStan/Ast/Attributes/AttributesTest.php @@ -8,6 +8,7 @@ use PHPStan\PhpDocParser\Parser\ConstExprParser; use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; +use PHPStan\PhpDocParser\ParserConfig; use PHPUnit\Framework\TestCase; final class AttributesTest extends TestCase @@ -18,9 +19,11 @@ final class AttributesTest extends TestCase protected function setUp(): void { parent::setUp(); - $lexer = new Lexer(); - $constExprParser = new ConstExprParser(); - $phpDocParser = new PhpDocParser(new TypeParser($constExprParser), $constExprParser); + + $config = new ParserConfig([]); + $lexer = new Lexer($config); + $constExprParser = new ConstExprParser($config); + $phpDocParser = new PhpDocParser($config, new TypeParser($config, $constExprParser), $constExprParser); $input = '/** @var string */'; $tokens = new TokenIterator($lexer->tokenize($input)); diff --git a/tests/PHPStan/Parser/ConstExprParserTest.php b/tests/PHPStan/Parser/ConstExprParserTest.php index 5f7fc43f95e9a65b819147d2a0714f964b3d4dd5..18922e864dfde215a3089c7c36b652961032c695 100644 --- a/tests/PHPStan/Parser/ConstExprParserTest.php +++ b/tests/PHPStan/Parser/ConstExprParserTest.php @@ -16,6 +16,7 @@ use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode; use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode; use PHPStan\PhpDocParser\Ast\NodeTraverser; use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use PHPUnit\Framework\TestCase; class ConstExprParserTest extends TestCase @@ -28,8 +29,9 @@ class ConstExprParserTest extends TestCase protected function setUp(): void { parent::setUp(); - $this->lexer = new Lexer(); - $this->constExprParser = new ConstExprParser(); + $config = new ParserConfig([]); + $this->lexer = new Lexer($config); + $this->constExprParser = new ConstExprParser($config); } @@ -67,10 +69,11 @@ class ConstExprParserTest extends TestCase public function testVerifyAttributes(string $input): void { $tokens = new TokenIterator($this->lexer->tokenize($input)); - $constExprParser = new ConstExprParser([ + $config = new ParserConfig([ 'lines' => true, 'indexes' => true, ]); + $constExprParser = new ConstExprParser($config); $visitor = new NodeCollectingVisitor(); $traverser = new NodeTraverser([$visitor]); $traverser->traverse([$constExprParser->parse($tokens)]); diff --git a/tests/PHPStan/Parser/FuzzyTest.php b/tests/PHPStan/Parser/FuzzyTest.php index d3c3ed7ee09979f2bfd0ea44ef269930647bddbc..9491bf69be8a737d296d75f84b5b608ca6cae858 100644 --- a/tests/PHPStan/Parser/FuzzyTest.php +++ b/tests/PHPStan/Parser/FuzzyTest.php @@ -4,6 +4,7 @@ namespace PHPStan\PhpDocParser\Parser; use Iterator; use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use PHPUnit\Framework\TestCase; use Symfony\Component\Process\Process; use function file_get_contents; @@ -28,9 +29,10 @@ class FuzzyTest extends TestCase protected function setUp(): void { parent::setUp(); - $this->lexer = new Lexer(); - $this->typeParser = new TypeParser(new ConstExprParser()); - $this->constExprParser = new ConstExprParser(); + $config = new ParserConfig([]); + $this->lexer = new Lexer($config); + $this->typeParser = new TypeParser($config, new ConstExprParser($config)); + $this->constExprParser = new ConstExprParser($config); } /** diff --git a/tests/PHPStan/Parser/PhpDocParserTest.php b/tests/PHPStan/Parser/PhpDocParserTest.php index aaf3d2b6b06dd0da8fc605aba49323215422bebd..3bd2c54613ed011b637b3299ba6c7377416d9fcc 100644 --- a/tests/PHPStan/Parser/PhpDocParserTest.php +++ b/tests/PHPStan/Parser/PhpDocParserTest.php @@ -64,6 +64,7 @@ use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode; use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use PHPUnit\Framework\TestCase; use function count; use function sprintf; @@ -79,10 +80,11 @@ class PhpDocParserTest extends TestCase protected function setUp(): void { parent::setUp(); - $this->lexer = new Lexer(); - $constExprParser = new ConstExprParser(); - $typeParser = new TypeParser($constExprParser); - $this->phpDocParser = new PhpDocParser($typeParser, $constExprParser, []); + $config = new ParserConfig([]); + $this->lexer = new Lexer($config); + $constExprParser = new ConstExprParser($config); + $typeParser = new TypeParser($config, $constExprParser); + $this->phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); } @@ -6807,13 +6809,13 @@ Finder::findFiles('*.php') public function testLinesAndIndexes(string $phpDoc, array $childrenLines): void { $tokens = new TokenIterator($this->lexer->tokenize($phpDoc)); - $usedAttributes = [ + $config = new ParserConfig([ 'lines' => true, 'indexes' => true, - ]; - $constExprParser = new ConstExprParser($usedAttributes); - $typeParser = new TypeParser($constExprParser, $usedAttributes); - $phpDocParser = new PhpDocParser($typeParser, $constExprParser, $usedAttributes); + ]); + $constExprParser = new ConstExprParser($config); + $typeParser = new TypeParser($config, $constExprParser); + $phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); $phpDocNode = $phpDocParser->parse($tokens); $children = $phpDocNode->children; $this->assertCount(count($childrenLines), $children); @@ -6891,13 +6893,13 @@ Finder::findFiles('*.php') public function testDeepNodesLinesAndIndexes(string $phpDoc, array $nodeAttributes): void { $tokens = new TokenIterator($this->lexer->tokenize($phpDoc)); - $usedAttributes = [ + $config = new ParserConfig([ 'lines' => true, 'indexes' => true, - ]; - $constExprParser = new ConstExprParser($usedAttributes); - $typeParser = new TypeParser($constExprParser, $usedAttributes); - $phpDocParser = new PhpDocParser($typeParser, $constExprParser, $usedAttributes); + ]); + $constExprParser = new ConstExprParser($config); + $typeParser = new TypeParser($config, $constExprParser); + $phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); $visitor = new NodeCollectingVisitor(); $traverser = new NodeTraverser([$visitor]); $traverser->traverse([$phpDocParser->parse($tokens)]); @@ -6964,13 +6966,13 @@ Finder::findFiles('*.php') public function testReturnTypeLinesAndIndexes(string $phpDoc, array $lines): void { $tokens = new TokenIterator($this->lexer->tokenize($phpDoc)); - $usedAttributes = [ + $config = new ParserConfig([ 'lines' => true, 'indexes' => true, - ]; - $constExprParser = new ConstExprParser($usedAttributes); - $typeParser = new TypeParser($constExprParser, $usedAttributes); - $phpDocParser = new PhpDocParser($typeParser, $constExprParser, $usedAttributes); + ]); + $constExprParser = new ConstExprParser($config); + $typeParser = new TypeParser($config, $constExprParser); + $phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); $phpDocNode = $phpDocParser->parse($tokens); $returnTag = $phpDocNode->getReturnTagValues()[0]; $type = $returnTag->type; @@ -7014,10 +7016,13 @@ Finder::findFiles('*.php') */ public function testVerifyAttributes(string $label, string $input): void { - $usedAttributes = ['lines' => true, 'indexes' => true]; - $constExprParser = new ConstExprParser($usedAttributes); - $typeParser = new TypeParser($constExprParser, $usedAttributes); - $phpDocParser = new PhpDocParser($typeParser, $constExprParser, $usedAttributes); + $config = new ParserConfig([ + 'lines' => true, + 'indexes' => true, + ]); + $constExprParser = new ConstExprParser($config); + $typeParser = new TypeParser($config, $constExprParser); + $phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); $tokens = new TokenIterator($this->lexer->tokenize($input)); $visitor = new NodeCollectingVisitor(); @@ -7344,9 +7349,10 @@ Finder::findFiles('*.php') PhpDocNode $expectedPhpDocNode ): void { - $constExprParser = new ConstExprParser(); - $typeParser = new TypeParser($constExprParser); - $phpDocParser = new PhpDocParser($typeParser, $constExprParser, []); + $config = new ParserConfig([]); + $constExprParser = new ConstExprParser($config); + $typeParser = new TypeParser($config, $constExprParser); + $phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); $tokens = new TokenIterator($this->lexer->tokenize($input)); $actualPhpDocNode = $phpDocParser->parse($tokens); diff --git a/tests/PHPStan/Parser/TokenIteratorTest.php b/tests/PHPStan/Parser/TokenIteratorTest.php index 8f2b4c9de67b3d39ea60141d66f092d31d2170a4..3cc51fc1a572d6c26553bb00a4c27ad15c4dc125 100644 --- a/tests/PHPStan/Parser/TokenIteratorTest.php +++ b/tests/PHPStan/Parser/TokenIteratorTest.php @@ -3,6 +3,7 @@ namespace PHPStan\PhpDocParser\Parser; use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use PHPUnit\Framework\TestCase; use const PHP_EOL; @@ -46,11 +47,12 @@ class TokenIteratorTest extends TestCase */ public function testGetDetectedNewline(string $phpDoc, ?string $expectedNewline): void { - $lexer = new Lexer(); + $config = new ParserConfig([]); + $lexer = new Lexer($config); $tokens = new TokenIterator($lexer->tokenize($phpDoc)); - $constExprParser = new ConstExprParser(); - $typeParser = new TypeParser($constExprParser); - $phpDocParser = new PhpDocParser($typeParser, $constExprParser); + $constExprParser = new ConstExprParser($config); + $typeParser = new TypeParser($config, $constExprParser); + $phpDocParser = new PhpDocParser($config, $typeParser, $constExprParser); $phpDocParser->parse($tokens); $this->assertSame($expectedNewline, $tokens->getDetectedNewline()); } diff --git a/tests/PHPStan/Parser/TypeParserTest.php b/tests/PHPStan/Parser/TypeParserTest.php index cd50db40b3783a0adfffe0ed78ea179752771b7a..de9d723083142cb0971a36426528dcf8af77e171 100644 --- a/tests/PHPStan/Parser/TypeParserTest.php +++ b/tests/PHPStan/Parser/TypeParserTest.php @@ -31,6 +31,7 @@ use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode; use PHPStan\PhpDocParser\Ast\Type\TypeNode; use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\ParserConfig; use PHPStan\PhpDocParser\Printer\Printer; use PHPUnit\Framework\TestCase; use function get_class; @@ -47,8 +48,9 @@ class TypeParserTest extends TestCase protected function setUp(): void { parent::setUp(); - $this->lexer = new Lexer(); - $this->typeParser = new TypeParser(new ConstExprParser()); + $config = new ParserConfig([]); + $this->lexer = new Lexer($config); + $this->typeParser = new TypeParser($config, new ConstExprParser($config)); } @@ -115,8 +117,8 @@ class TypeParserTest extends TestCase $this->expectExceptionMessage($expectedResult->getMessage()); } - $usedAttributes = ['lines' => true, 'indexes' => true]; - $typeParser = new TypeParser(new ConstExprParser($usedAttributes), $usedAttributes); + $config = new ParserConfig(['lines' => true, 'indexes' => true]); + $typeParser = new TypeParser($config, new ConstExprParser($config)); $tokens = new TokenIterator($this->lexer->tokenize($input)); $visitor = new NodeCollectingVisitor(); @@ -3069,11 +3071,11 @@ class TypeParserTest extends TestCase { $tokensArray = $this->lexer->tokenize($input); $tokens = new TokenIterator($tokensArray); - $usedAttributes = [ + $config = new ParserConfig([ 'lines' => true, 'indexes' => true, - ]; - $typeParser = new TypeParser(new ConstExprParser(), $usedAttributes); + ]); + $typeParser = new TypeParser($config, new ConstExprParser($config)); $typeNode = $typeParser->parse($tokens); foreach ($assertions as [$callable, $expectedContent, $startLine, $endLine, $startIndex, $endIndex]) { diff --git a/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php b/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php index 36ad360ba300c1d4d40109bf98d3d0422d74fb3b..d2f2c361a47b399717b17990143c552fd6085dd9 100644 --- a/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php +++ b/tests/PHPStan/Printer/IntegrationPrinterWithPhpParserTest.php @@ -21,6 +21,7 @@ use PHPStan\PhpDocParser\Parser\ConstExprParser; use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; +use PHPStan\PhpDocParser\ParserConfig; use PHPUnit\Framework\TestCase; use function file_get_contents; @@ -98,14 +99,14 @@ class IntegrationPrinterWithPhpParserTest extends TestCase $phpDoc = $phpNode->getDocComment()->getText(); - $usedAttributes = ['lines' => true, 'indexes' => true]; - $constExprParser = new ConstExprParser($usedAttributes); + $config = new ParserConfig(['lines' => true, 'indexes' => true]); + $constExprParser = new ConstExprParser($config); $phpDocParser = new PhpDocParser( - new TypeParser($constExprParser, $usedAttributes), + $config, + new TypeParser($config, $constExprParser), $constExprParser, - $usedAttributes, ); - $lexer = new Lexer(); + $lexer = new Lexer($config); $tokens = new TokenIterator($lexer->tokenize($phpDoc)); $phpDocNode = $phpDocParser->parse($tokens); $cloningTraverser = new NodeTraverser([new NodeVisitor\CloningVisitor()]); diff --git a/tests/PHPStan/Printer/PrinterTest.php b/tests/PHPStan/Printer/PrinterTest.php index 7ed808c70f385805c79644c0d08a61647aafff16..b4ca62445e17b01f85022a7454a451f9ef50c7c5 100644 --- a/tests/PHPStan/Printer/PrinterTest.php +++ b/tests/PHPStan/Printer/PrinterTest.php @@ -47,6 +47,7 @@ use PHPStan\PhpDocParser\Parser\ConstExprParser; use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; +use PHPStan\PhpDocParser\ParserConfig; use PHPUnit\Framework\TestCase; use function array_pop; use function array_splice; @@ -65,13 +66,13 @@ class PrinterTest extends TestCase protected function setUp(): void { - $usedAttributes = ['lines' => true, 'indexes' => true]; - $constExprParser = new ConstExprParser($usedAttributes); - $this->typeParser = new TypeParser($constExprParser, $usedAttributes); + $config = new ParserConfig(['lines' => true, 'indexes' => true]); + $constExprParser = new ConstExprParser($config); + $this->typeParser = new TypeParser($config, $constExprParser); $this->phpDocParser = new PhpDocParser( + $config, $this->typeParser, $constExprParser, - $usedAttributes, ); } @@ -1857,7 +1858,8 @@ class PrinterTest extends TestCase */ public function testPrintFormatPreserving(string $phpDoc, string $expectedResult, NodeVisitor $visitor): void { - $lexer = new Lexer(); + $config = new ParserConfig([]); + $lexer = new Lexer($config); $tokens = new TokenIterator($lexer->tokenize($phpDoc)); $phpDocNode = $this->phpDocParser->parse($tokens); $cloningTraverser = new NodeTraverser([new NodeVisitor\CloningVisitor()]); @@ -2007,7 +2009,8 @@ class PrinterTest extends TestCase $phpDoc = $printer->print($node); $this->assertSame($expectedResult, $phpDoc); - $lexer = new Lexer(); + $config = new ParserConfig([]); + $lexer = new Lexer($config); $this->assertEquals( $this->unsetAttributes($node), $this->unsetAttributes($this->typeParser->parse(new TokenIterator($lexer->tokenize($phpDoc)))), @@ -2043,7 +2046,8 @@ class PrinterTest extends TestCase $phpDoc = $printer->print($node); $this->assertSame($expectedResult, $phpDoc); - $lexer = new Lexer(); + $config = new ParserConfig([]); + $lexer = new Lexer($config); $this->assertEquals( $this->unsetAttributes($node), $this->unsetAttributes($this->phpDocParser->parse(new TokenIterator($lexer->tokenize($phpDoc)))),