77
88import com .intellij .codeInspection .ProblemHighlightType ;
99import com .intellij .codeInspection .ProblemsHolder ;
10+ import com .intellij .codeInspection .XmlSuppressableInspectionTool ;
11+ import com .intellij .psi .PsiElement ;
1012import com .intellij .psi .PsiElementVisitor ;
1113import com .intellij .psi .PsiFile ;
1214import com .intellij .psi .XmlElementVisitor ;
13- import com .intellij .psi .util .PsiTreeUtil ;
1415import com .intellij .psi .xml .XmlAttribute ;
15- import com .intellij .psi .xml .XmlDocument ;
1616import com .intellij .psi .xml .XmlTag ;
17- import com .jetbrains .php .lang .inspections .PhpInspection ;
18- import com .jetbrains .php .lang .psi .elements .PhpClass ;
1917import com .magento .idea .magento2plugin .bundles .InspectionBundle ;
18+ import com .magento .idea .magento2plugin .inspections .validator .InspectionValidator ;
19+ import com .magento .idea .magento2plugin .inspections .validator .NotEmptyValidator ;
20+ import com .magento .idea .magento2plugin .inspections .validator .PhpClassExistenceValidator ;
2021import com .magento .idea .magento2plugin .magento .files .ModuleDiXml ;
21- import com .magento .idea .magento2plugin .util .GetPhpClassByFQN ;
2222import org .jetbrains .annotations .NotNull ;
23- import org .jetbrains .annotations .Nullable ;
2423
25- @ SuppressWarnings ({"PMD.ExcessiveMethodLength" , "PMD.NPathComplexity" })
26- public class PreferenceDeclarationInspection extends PhpInspection {
24+ public class PreferenceDeclarationInspection extends XmlSuppressableInspectionTool {
2725
2826 @ Override
2927 public @ NotNull PsiElementVisitor buildVisitor (
@@ -33,99 +31,103 @@ public class PreferenceDeclarationInspection extends PhpInspection {
3331 return new XmlElementVisitor () {
3432 private final InspectionBundle inspectionBundle = new InspectionBundle ();
3533
34+ private final InspectionValidator phpClassExistenceValidator =
35+ new PhpClassExistenceValidator (problemsHolder .getProject ());
36+ private final InspectionValidator notEmptyValidator = new NotEmptyValidator ();
37+
3638 @ Override
37- public void visitFile (final @ NotNull PsiFile file ) {
38- if (!file .getName ().equals (ModuleDiXml .FILE_NAME )) {
39+ public void visitXmlTag (final XmlTag xmlTag ) {
40+ final PsiFile file = xmlTag .getContainingFile ();
41+
42+ if (!file .getName ().equals (ModuleDiXml .FILE_NAME )
43+ || !xmlTag .getName ().equals (ModuleDiXml .PREFERENCE_TAG_NAME )) {
3944 return ;
4045 }
4146
42- final XmlTag [] xmlTags = getFileXmlTags (file );
47+ final XmlAttribute preferenceForAttribute =
48+ xmlTag .getAttribute (ModuleDiXml .PREFERENCE_ATTR_FOR );
49+ final XmlAttribute preferenceTypeAttribute =
50+ xmlTag .getAttribute (ModuleDiXml .TYPE_ATTR );
4351
44- if (xmlTags == null ) {
52+ if (preferenceForAttribute == null
53+ || preferenceForAttribute .getValue () == null
54+ || preferenceForAttribute .getValueElement () == null ) {
4555 return ;
4656 }
4757
48- for (final XmlTag preferenceXmlTag : xmlTags ) {
49- if (!preferenceXmlTag .getName ().equals (ModuleDiXml .PREFERENCE_TAG_NAME )) {
50- continue ;
51- }
52-
53- final XmlAttribute preferenceForAttribute =
54- preferenceXmlTag .getAttribute (ModuleDiXml .PREFERENCE_ATTR_FOR );
55- final XmlAttribute preferenceTypeAttribute =
56- preferenceXmlTag .getAttribute (ModuleDiXml .TYPE_ATTR );
57-
58- if (preferenceForAttribute == null || preferenceTypeAttribute == null ) {
59- continue ;
60- }
58+ if (!notEmptyValidator .validate (preferenceForAttribute .getValue ())) {
59+ reportCouldNotBeEmpty (
60+ preferenceForAttribute .getValueElement (),
61+ preferenceForAttribute .getName ()
62+ );
63+ }
6164
62- final Boolean isPreferenceForClassExists =
63- isXmlAttributeValueClassExists (preferenceForAttribute );
65+ if (preferenceTypeAttribute == null
66+ || preferenceTypeAttribute .getValue () == null
67+ || preferenceTypeAttribute .getValueElement () == null ) {
68+ return ;
69+ }
6470
65- if (isPreferenceForClassExists != null && !isPreferenceForClassExists ) {
66- reportClassDoesntExistsProblem (preferenceForAttribute );
67- }
71+ if (!notEmptyValidator .validate (preferenceTypeAttribute .getValue ())) {
72+ reportCouldNotBeEmpty (
73+ preferenceTypeAttribute .getValueElement (),
74+ preferenceTypeAttribute .getName ()
75+ );
76+ }
6877
69- final Boolean isPreferenceTypeClassExists =
70- isXmlAttributeValueClassExists (preferenceTypeAttribute );
78+ if (!phpClassExistenceValidator .validate (preferenceForAttribute .getValue ())) {
79+ reportClassDoesNotExists (
80+ preferenceForAttribute .getValueElement (),
81+ preferenceForAttribute .getValue ()
82+ );
83+ }
7184
72- if (isPreferenceTypeClassExists != null && !isPreferenceTypeClassExists ) {
73- reportClassDoesntExistsProblem (preferenceTypeAttribute );
74- }
85+ if (!phpClassExistenceValidator .validate (preferenceTypeAttribute .getValue ())) {
86+ reportClassDoesNotExists (
87+ preferenceTypeAttribute .getValueElement (),
88+ preferenceTypeAttribute .getValue ()
89+ );
7590 }
7691 }
7792
7893 /**
79- * Output a warning in the xml class .
94+ * Report Attribute Value could not be empty .
8095 *
81- * @param xmlAttribute XmlAttribute
96+ * @param psiElement PsiElement
97+ * @param messageParams Object...
8298 */
83- private void reportClassDoesntExistsProblem ( final @ NotNull XmlAttribute xmlAttribute ) {
84- if ( xmlAttribute . getValueElement () == null ) {
85- return ;
86- }
99+ private void reportCouldNotBeEmpty (
100+ final @ NotNull PsiElement psiElement ,
101+ final Object ... messageParams
102+ ) {
87103 problemsHolder .registerProblem (
88- xmlAttribute .getValueElement (),
89- inspectionBundle .message ("inspection.preference.error.notExist" ),
90- ProblemHighlightType .WARNING
104+ psiElement ,
105+ inspectionBundle .message (
106+ "inspection.error.idAttributeCanNotBeEmpty" ,
107+ messageParams
108+ ),
109+ ProblemHighlightType .ERROR
91110 );
92111 }
93112
94113 /**
95- * Checks the existence of the php class in the specified directory.
96- *
97- * @param xmlAttribute XmlAttribute
114+ * Report class does not exists.
98115 *
99- * @return attributeValueClass
116+ * @param psiElement PsiElement
117+ * @param messageParams Object...
100118 */
101- private @ Nullable Boolean isXmlAttributeValueClassExists (
102- final @ NotNull XmlAttribute xmlAttribute
119+ private void reportClassDoesNotExists (
120+ final @ NotNull PsiElement psiElement ,
121+ final Object ... messageParams
103122 ) {
104- final String attributeValue = xmlAttribute .getValue ();
105-
106- if (attributeValue == null ) {
107- return null ;
108- }
109-
110- final PhpClass attributeValueClass =
111- GetPhpClassByFQN .getInstance (xmlAttribute .getProject ())
112- .execute (attributeValue );
113-
114- return attributeValueClass != null ;
115- }
116-
117- /**
118- * Get all children xml tags for root xml tag of file.
119- *
120- * @param file PsiFile
121- *
122- * @return XmlTag[]
123- */
124- private @ Nullable XmlTag [] getFileXmlTags (final @ NotNull PsiFile file ) {
125- final XmlDocument xmlDocument = PsiTreeUtil .getChildOfType (file , XmlDocument .class );
126- final XmlTag xmlRootTag = PsiTreeUtil .getChildOfType (xmlDocument , XmlTag .class );
127-
128- return PsiTreeUtil .getChildrenOfType (xmlRootTag , XmlTag .class );
123+ problemsHolder .registerProblem (
124+ psiElement ,
125+ inspectionBundle .message (
126+ "inspection.warning.class.does.not.exist" ,
127+ messageParams
128+ ),
129+ ProblemHighlightType .WARNING
130+ );
129131 }
130132 };
131133 }
0 commit comments