33import com .intellij .codeInspection .LocalInspectionTool ;
44import com .intellij .codeInspection .ProblemHighlightType ;
55import com .intellij .codeInspection .ProblemsHolder ;
6+ import com .intellij .lang .Language ;
7+ import com .intellij .lang .xml .XMLLanguage ;
68import com .intellij .psi .PsiElement ;
79import com .intellij .psi .PsiElementVisitor ;
8- import com .intellij .psi .PsiFile ;
9- import com .intellij .psi .PsiRecursiveElementVisitor ;
10- import com .intellij .psi .xml .XmlFile ;
1110import com .intellij .psi .xml .XmlText ;
1211import fr .adrienbrault .idea .symfony2plugin .config .xml .XmlHelper ;
1312import fr .adrienbrault .idea .symfony2plugin .dic .container .util .ServiceContainerUtil ;
1413import org .apache .commons .lang .StringUtils ;
1514import org .jetbrains .annotations .NotNull ;
16- import org .jetbrains .yaml .psi . YAMLFile ;
15+ import org .jetbrains .yaml .YAMLLanguage ;
1716import org .jetbrains .yaml .psi .YAMLScalar ;
1817
1918/**
@@ -27,61 +26,48 @@ public class ContainerConstantInspection extends LocalInspectionTool {
2726 public PsiElementVisitor buildVisitor (final @ NotNull ProblemsHolder holder , boolean isOnTheFly ) {
2827 return new PsiElementVisitor () {
2928 @ Override
30- public void visitFile (@ NotNull PsiFile psiFile ) {
31- if (psiFile instanceof XmlFile ) {
32- xmlVisitor (holder , psiFile );
33- } else if (psiFile instanceof YAMLFile ) {
34- yamlVisitor (holder , psiFile );
29+ public void visitElement (@ NotNull PsiElement element ) {
30+ Language language = element .getLanguage ();
31+
32+ if (language == YAMLLanguage .INSTANCE ) {
33+ if (element instanceof YAMLScalar yamlScalar ) {
34+ visitYamlElement (yamlScalar , holder );
35+ }
36+ } else if (language == XMLLanguage .INSTANCE ) {
37+ visitXmlElement (element , holder );
3538 }
39+
40+ super .visitElement (element );
3641 }
3742 };
3843 }
39- private void yamlVisitor (@ NotNull ProblemsHolder holder , @ NotNull PsiFile psiFile ) {
40- psiFile .acceptChildren (new PsiRecursiveElementVisitor () {
41- @ Override
42- public void visitElement (@ NotNull PsiElement psiElement ) {
43- if (psiElement instanceof YAMLScalar ) {
44- String textValue = ((YAMLScalar ) psiElement ).getTextValue ();
45- if (textValue .startsWith ("!php/const:" )) {
46- String constantName = textValue .substring (11 );
47- if (StringUtils .isNotBlank (constantName ) && ServiceContainerUtil .getTargetsForConstant (psiElement .getProject (), constantName ).size () == 0 ) {
48- holder .registerProblem (psiElement , MESSAGE , ProblemHighlightType .GENERIC_ERROR_OR_WARNING );
49- }
50- }
51- }
52-
53- super .visitElement (psiElement );
44+ private void visitYamlElement (@ NotNull YAMLScalar psiElement , @ NotNull ProblemsHolder holder ) {
45+ String textValue = ((YAMLScalar ) psiElement ).getTextValue ();
46+ if (textValue .startsWith ("!php/const:" )) {
47+ String constantName = textValue .substring (11 );
48+ if (StringUtils .isNotBlank (constantName ) && ServiceContainerUtil .getTargetsForConstant (psiElement .getProject (), constantName ).size () == 0 ) {
49+ holder .registerProblem (psiElement , MESSAGE , ProblemHighlightType .GENERIC_ERROR_OR_WARNING );
5450 }
55- });
51+ }
5652 }
5753
58- private void xmlVisitor (@ NotNull ProblemsHolder holder , @ NotNull PsiFile psiFile ) {
59- psiFile .acceptChildren (new PsiRecursiveElementVisitor () {
60- @ Override
61- public void visitElement (@ NotNull PsiElement psiElement ) {
62- if (!XmlHelper .getArgumentValueWithTypePattern ("constant" ).accepts (psiElement )) {
63- super .visitElement (psiElement );
64- return ;
65- }
54+ private void visitXmlElement (@ NotNull PsiElement psiElement , @ NotNull ProblemsHolder holder ) {
55+ if (!XmlHelper .getArgumentValueWithTypePattern ("constant" ).accepts (psiElement )) {
56+ return ;
57+ }
6658
67- PsiElement xmlText = psiElement .getParent ();
68- if (!(xmlText instanceof XmlText )) {
69- super .visitElement (psiElement );
70- return ;
71- }
59+ PsiElement xmlText = psiElement .getParent ();
60+ if (!(xmlText instanceof XmlText )) {
61+ return ;
62+ }
7263
73- String value = ((XmlText ) xmlText ).getValue ();
74- if (StringUtils .isBlank (value )) {
75- super .visitElement (psiElement );
76- return ;
77- }
64+ String value = ((XmlText ) xmlText ).getValue ();
65+ if (StringUtils .isBlank (value )) {
66+ return ;
67+ }
7868
79- if (ServiceContainerUtil .getTargetsForConstant (xmlText .getProject (), value ).size () == 0 ) {
80- holder .registerProblem (xmlText , MESSAGE , ProblemHighlightType .GENERIC_ERROR_OR_WARNING );
81- }
82-
83- super .visitElement (psiElement );
84- }
85- });
69+ if (ServiceContainerUtil .getTargetsForConstant (xmlText .getProject (), value ).size () == 0 ) {
70+ holder .registerProblem (xmlText , MESSAGE , ProblemHighlightType .GENERIC_ERROR_OR_WARNING );
71+ }
8672 }
8773}
0 commit comments