7
7
8
8
import com .intellij .codeInspection .ProblemHighlightType ;
9
9
import com .intellij .codeInspection .ProblemsHolder ;
10
+ import com .intellij .codeInspection .XmlSuppressableInspectionTool ;
11
+ import com .intellij .psi .PsiElement ;
10
12
import com .intellij .psi .PsiElementVisitor ;
11
13
import com .intellij .psi .PsiFile ;
12
14
import com .intellij .psi .XmlElementVisitor ;
13
- import com .intellij .psi .util .PsiTreeUtil ;
14
15
import com .intellij .psi .xml .XmlAttribute ;
15
- import com .intellij .psi .xml .XmlDocument ;
16
16
import com .intellij .psi .xml .XmlTag ;
17
- import com .jetbrains .php .lang .inspections .PhpInspection ;
18
- import com .jetbrains .php .lang .psi .elements .PhpClass ;
19
17
import 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 ;
20
21
import com .magento .idea .magento2plugin .magento .files .ModuleDiXml ;
21
- import com .magento .idea .magento2plugin .util .GetPhpClassByFQN ;
22
22
import org .jetbrains .annotations .NotNull ;
23
- import org .jetbrains .annotations .Nullable ;
24
23
25
- @ SuppressWarnings ({"PMD.ExcessiveMethodLength" , "PMD.NPathComplexity" })
26
- public class PreferenceDeclarationInspection extends PhpInspection {
24
+ public class PreferenceDeclarationInspection extends XmlSuppressableInspectionTool {
27
25
28
26
@ Override
29
27
public @ NotNull PsiElementVisitor buildVisitor (
@@ -33,99 +31,103 @@ public class PreferenceDeclarationInspection extends PhpInspection {
33
31
return new XmlElementVisitor () {
34
32
private final InspectionBundle inspectionBundle = new InspectionBundle ();
35
33
34
+ private final InspectionValidator phpClassExistenceValidator =
35
+ new PhpClassExistenceValidator (problemsHolder .getProject ());
36
+ private final InspectionValidator notEmptyValidator = new NotEmptyValidator ();
37
+
36
38
@ 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 )) {
39
44
return ;
40
45
}
41
46
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 );
43
51
44
- if (xmlTags == null ) {
52
+ if (preferenceForAttribute == null
53
+ || preferenceForAttribute .getValue () == null
54
+ || preferenceForAttribute .getValueElement () == null ) {
45
55
return ;
46
56
}
47
57
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
+ }
61
64
62
- final Boolean isPreferenceForClassExists =
63
- isXmlAttributeValueClassExists (preferenceForAttribute );
65
+ if (preferenceTypeAttribute == null
66
+ || preferenceTypeAttribute .getValue () == null
67
+ || preferenceTypeAttribute .getValueElement () == null ) {
68
+ return ;
69
+ }
64
70
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
+ }
68
77
69
- final Boolean isPreferenceTypeClassExists =
70
- isXmlAttributeValueClassExists (preferenceTypeAttribute );
78
+ if (!phpClassExistenceValidator .validate (preferenceForAttribute .getValue ())) {
79
+ reportClassDoesNotExists (
80
+ preferenceForAttribute .getValueElement (),
81
+ preferenceForAttribute .getValue ()
82
+ );
83
+ }
71
84
72
- if (isPreferenceTypeClassExists != null && !isPreferenceTypeClassExists ) {
73
- reportClassDoesntExistsProblem (preferenceTypeAttribute );
74
- }
85
+ if (!phpClassExistenceValidator .validate (preferenceTypeAttribute .getValue ())) {
86
+ reportClassDoesNotExists (
87
+ preferenceTypeAttribute .getValueElement (),
88
+ preferenceTypeAttribute .getValue ()
89
+ );
75
90
}
76
91
}
77
92
78
93
/**
79
- * Output a warning in the xml class .
94
+ * Report Attribute Value could not be empty .
80
95
*
81
- * @param xmlAttribute XmlAttribute
96
+ * @param psiElement PsiElement
97
+ * @param messageParams Object...
82
98
*/
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
+ ) {
87
103
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
91
110
);
92
111
}
93
112
94
113
/**
95
- * Checks the existence of the php class in the specified directory.
96
- *
97
- * @param xmlAttribute XmlAttribute
114
+ * Report class does not exists.
98
115
*
99
- * @return attributeValueClass
116
+ * @param psiElement PsiElement
117
+ * @param messageParams Object...
100
118
*/
101
- private @ Nullable Boolean isXmlAttributeValueClassExists (
102
- final @ NotNull XmlAttribute xmlAttribute
119
+ private void reportClassDoesNotExists (
120
+ final @ NotNull PsiElement psiElement ,
121
+ final Object ... messageParams
103
122
) {
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
+ );
129
131
}
130
132
};
131
133
}
0 commit comments