1111
1212namespace Translation \Extractor \Visitor ;
1313
14+ use Doctrine \Common \Annotations \DocParser ;
15+ use PhpParser \Node ;
1416use Symfony \Component \Finder \SplFileInfo ;
17+ use Translation \Extractor \Annotation \Ignore ;
18+ use Translation \Extractor \Model \Error ;
1519use Translation \Extractor \Model \SourceCollection ;
1620
1721/**
@@ -31,6 +35,11 @@ abstract class BaseVisitor implements Visitor
3135 */
3236 protected $ file ;
3337
38+ /**
39+ * @var DocParser
40+ */
41+ private $ docParser ;
42+
3443 public function init (SourceCollection $ collection , SplFileInfo $ file )
3544 {
3645 $ this ->collection = $ collection ;
@@ -41,4 +50,55 @@ protected function getAbsoluteFilePath()
4150 {
4251 return $ this ->file ->getRealPath ();
4352 }
53+
54+ /**
55+ * @param Node $node
56+ * @param string $errorMessage
57+ */
58+ protected function addError (Node $ node , $ errorMessage )
59+ {
60+ $ docComment = $ node ->getDocComment ();
61+ $ file = $ this ->getAbsoluteFilePath ();
62+
63+ if (property_exists ($ node , 'value ' )) {
64+ $ line = $ node ->value ->getAttribute ('startLine ' );
65+ } else {
66+ $ line = $ node ->getAttribute ('startLine ' );
67+ }
68+ if (null !== $ docComment ) {
69+ $ context = 'file ' .$ file .' near line ' .$ line ;
70+ foreach ($ this ->getDocParser ()->parse ($ docComment ->getText (), $ context ) as $ annotation ) {
71+ if ($ annotation instanceof Ignore) {
72+ return ;
73+ }
74+ }
75+ }
76+
77+ $ this ->collection ->addError (new Error ($ errorMessage , $ file , $ line ));
78+ }
79+
80+ /**
81+ * @return DocParser
82+ */
83+ private function getDocParser ()
84+ {
85+ if (null === $ this ->docParser ) {
86+ $ this ->docParser = new DocParser ();
87+
88+ $ this ->docParser ->setImports ([
89+ 'ignore ' => Ignore::class,
90+ ]);
91+ $ this ->docParser ->setIgnoreNotImportedAnnotations (true );
92+ }
93+
94+ return $ this ->docParser ;
95+ }
96+
97+ /**
98+ * @param DocParser $docParser
99+ */
100+ public function setDocParser (DocParser $ docParser )
101+ {
102+ $ this ->docParser = $ docParser ;
103+ }
44104}
0 commit comments