1111
1212namespace Translation \Extractor \Visitor ;
1313
14- use Doctrine \Common \Annotations \DocParser ;
1514use PhpParser \Node ;
15+ use PHPStan \PhpDocParser \Ast \ConstExpr \DoctrineConstExprStringNode ;
16+ use PHPStan \PhpDocParser \Ast \PhpDoc \Doctrine \DoctrineTagValueNode ;
17+ use PHPStan \PhpDocParser \Lexer \Lexer ;
18+ use PHPStan \PhpDocParser \Parser \ConstExprParser ;
19+ use PHPStan \PhpDocParser \Parser \PhpDocParser ;
20+ use PHPStan \PhpDocParser \Parser \TokenIterator ;
21+ use PHPStan \PhpDocParser \Parser \TypeParser ;
22+ use PHPStan \PhpDocParser \ParserConfig ;
1623use Symfony \Component \Finder \SplFileInfo ;
17- use Translation \Extractor \Annotation \Desc ;
18- use Translation \Extractor \Annotation \Ignore ;
1924use Translation \Extractor \Model \Error ;
2025use Translation \Extractor \Model \SourceCollection ;
2126use Translation \Extractor \Model \SourceLocation ;
2732 */
2833abstract class BaseVisitor implements Visitor
2934{
30- private ?DocParser $ docParser = null ;
35+ protected ?Lexer $ lexer = null ;
36+ protected ?PhpDocParser $ phpDocParser = null ;
3137
3238 protected ?SourceCollection $ collection = null ;
3339 protected SplFileInfo $ file ;
@@ -54,9 +60,11 @@ protected function addError(Node $node, string $errorMessage): void
5460 $ line = $ node ->getAttribute ('startLine ' );
5561 }
5662 if (null !== $ docComment ) {
57- $ context = 'file ' .$ file .' near line ' .$ line ;
58- foreach ($ this ->getDocParser ()->parse ($ docComment ->getText (), $ context ) as $ annotation ) {
59- if ($ annotation instanceof Ignore) {
63+ $ phpDocNode = $ this ->getPhpDocParser ()->parse (
64+ new TokenIterator ($ this ->lexer ->tokenize ($ docComment ->getText ()))
65+ );
66+ foreach ($ phpDocNode ->getTags () as $ tag ) {
67+ if ('@Ignore ' === $ tag ->name ) {
6068 return ;
6169 }
6270 }
@@ -78,36 +86,38 @@ protected function getLocation(string $text, int $line, ?Node $node = null, arra
7886 {
7987 $ file = $ this ->getAbsoluteFilePath ();
8088 if (null !== $ node && null !== $ docComment = $ node ->getDocComment ()) {
81- $ parserContext = 'file ' .$ file .' near line ' .$ line ;
82- foreach ($ this ->getDocParser ()->parse ($ docComment ->getText (), $ parserContext ) as $ annotation ) {
83- if ($ annotation instanceof Ignore) {
89+ $ phpDocNode = $ this ->getPhpDocParser ()->parse (
90+ new TokenIterator ($ this ->lexer ->tokenize ($ docComment ->getText ()))
91+ );
92+ foreach ($ phpDocNode ->getTags () as $ tag ) {
93+ if ('@Ignore ' === $ tag ->name ) {
8494 return null ;
85- } elseif ($ annotation instanceof Desc) {
86- $ context ['desc ' ] = $ annotation ->text ;
95+ } elseif ('@Desc ' === $ tag ->name && $ tag ->value instanceof DoctrineTagValueNode) {
96+ if ([] !== $ tag ->value ->annotation ->arguments ) {
97+ $ context ['desc ' ] = DoctrineConstExprStringNode::unescape ($ tag ->value ->annotation ->arguments [0 ]->value );
98+ }
8799 }
88100 }
89101 }
90102
91103 return new SourceLocation ($ text , $ file , $ line , $ context );
92104 }
93105
94- private function getDocParser (): DocParser
106+ protected function getPhpDocParser (): PhpDocParser
95107 {
96- if (null === $ this ->docParser ) {
97- $ this ->docParser = new DocParser ();
98-
99- $ this ->docParser ->setImports ([
100- 'ignore ' => Ignore::class,
101- 'desc ' => Desc::class,
102- ]);
103- $ this ->docParser ->setIgnoreNotImportedAnnotations (true );
108+ if (null === $ this ->phpDocParser ) {
109+ $ config = new ParserConfig (usedAttributes: []);
110+ $ this ->lexer = new Lexer ($ config );
111+ $ constExprParser = new ConstExprParser ($ config );
112+ $ typeParser = new TypeParser ($ config , $ constExprParser );
113+ $ this ->phpDocParser = new PhpDocParser ($ config , $ typeParser , $ constExprParser );
104114 }
105115
106- return $ this ->docParser ;
116+ return $ this ->phpDocParser ;
107117 }
108118
109- public function setDocParser ( DocParser $ docParser ): void
119+ public function setPhpDocParser ( PhpDocParser $ phpDocParser ): void
110120 {
111- $ this ->docParser = $ docParser ;
121+ $ this ->phpDocParser = $ phpDocParser ;
112122 }
113123}
0 commit comments