-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1176 from bohdan-harniuk/1100-add-virtual-type-cl…
…ass-existence-inspection 1100: Added inspection to check if type attr value in the virtual type tag exists
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
resources/inspectionDescriptions/InvalidVirtualTypeSourceClassInspection.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<html> | ||
<body> | ||
<p> | ||
Validates if value in the type attribute inside the <virtualType/> tag of di.xml files contains valid class, | ||
interface, factory or proxy name. | ||
</p> | ||
<p>This inspection checks type attribute of the <virtualType/> tag.</p> | ||
<p>This inspection supports next types:</p> | ||
<ul> | ||
<li>PHP classes</li> | ||
<li>PHP interfaces</li> | ||
<li>PHP classes or interfaces with added Factory or \Proxy suffixes</li> | ||
</ul> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
.../magento/idea/magento2plugin/inspections/xml/InvalidVirtualTypeSourceClassInspection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
package com.magento.idea.magento2plugin.inspections.xml; | ||
|
||
import com.intellij.codeInspection.ProblemHighlightType; | ||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.codeInspection.XmlSuppressableInspectionTool; | ||
import com.intellij.psi.PsiElementVisitor; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.XmlElementVisitor; | ||
import com.intellij.psi.xml.XmlAttribute; | ||
import com.intellij.psi.xml.XmlTag; | ||
import com.magento.idea.magento2plugin.bundles.InspectionBundle; | ||
import com.magento.idea.magento2plugin.inspections.validator.InspectionValidator; | ||
import com.magento.idea.magento2plugin.inspections.validator.NotEmptyValidator; | ||
import com.magento.idea.magento2plugin.inspections.validator.PhpClassExistenceValidator; | ||
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class InvalidVirtualTypeSourceClassInspection extends XmlSuppressableInspectionTool { | ||
|
||
@Override | ||
public @NotNull PsiElementVisitor buildVisitor( | ||
final @NotNull ProblemsHolder problemsHolder, | ||
final boolean isOnTheFly | ||
) { | ||
return new XmlElementVisitor() { | ||
|
||
private final InspectionBundle inspectionBundle = new InspectionBundle(); | ||
private final InspectionValidator phpClassExistenceValidator = | ||
new PhpClassExistenceValidator(problemsHolder.getProject()); | ||
private final InspectionValidator notEmptyValidator = new NotEmptyValidator(); | ||
|
||
@Override | ||
public void visitXmlAttribute(final XmlAttribute attribute) { | ||
final PsiFile file = attribute.getContainingFile(); | ||
final XmlTag tag = attribute.getParent(); | ||
|
||
if (file == null | ||
|| tag == null | ||
|| !file.getName().equals(ModuleDiXml.FILE_NAME) | ||
|| !attribute.getName().equals(ModuleDiXml.TYPE_ATTR) | ||
|| !tag.getName().equals(ModuleDiXml.VIRTUAL_TYPE_TAG) | ||
) { | ||
return; | ||
} | ||
|
||
if (attribute.getValue() == null | ||
|| attribute.getValueElement() == null | ||
|| attribute.getValueElement().getText().isEmpty() | ||
) { | ||
return; | ||
} | ||
|
||
if (!notEmptyValidator.validate(attribute.getValue())) { | ||
problemsHolder.registerProblem( | ||
attribute.getValueElement(), | ||
inspectionBundle.message( | ||
"inspection.error.idAttributeCanNotBeEmpty", | ||
attribute.getName() | ||
), | ||
ProblemHighlightType.ERROR | ||
); | ||
} | ||
|
||
if (!phpClassExistenceValidator.validate(attribute.getValue())) { | ||
problemsHolder.registerProblem( | ||
attribute.getValueElement(), | ||
inspectionBundle.message( | ||
"inspection.warning.class.does.not.exist", | ||
attribute.getValue() | ||
), | ||
ProblemHighlightType.WARNING | ||
); | ||
} | ||
} | ||
}; | ||
} | ||
} |