-
Notifications
You must be signed in to change notification settings - Fork 105
Developed preference xml inspections #578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
VitaliyBoyko
merged 13 commits into
magento:4.0.0-develop
from
Iamwade:developed-preference-xml-inspections
May 6, 2021
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
88dfb3d
Develop preference xml inspections
Iamwade 6af755e
Corrected the manual file
Iamwade 6e24fa5
fixed merge conflict
Iamwade e03abae
refactoring code and adding tests
Iamwade 2b22440
fixed merge conflict
Iamwade 62fa5a9
fixed check style conflict
Iamwade d66ee46
fixed PMD conflict
Iamwade 9a5672a
fixed PMD conflict and tests
Iamwade 961f8c9
fixed code after review
Iamwade 2efa4be
fixed code test file after review
Iamwade d92b020
update branch
Iamwade 084b53a
fixed error when adding an attribute with empty value
Iamwade ea56035
Merge branch '4.0.0-develop' into developed-preference-xml-inspections
Iamwade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
18 changes: 18 additions & 0 deletions
18
resources/inspectionDescriptions/PreferenceXmlInspections.html
This file contains hidden or 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,18 @@ | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<html> | ||
<body> | ||
<p> | ||
Validation of attributes inside the "preference" tag | ||
</p> | ||
<p>This inspection checks the "for" and "type" attribute of the <preference/> tag for:</p> | ||
<ul> | ||
<li>the correct path to the class or interface</li> | ||
<li>existence of attribute values</li> | ||
</ul> | ||
</body> | ||
</html> |
This file contains hidden or 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
This file contains hidden or 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
138 changes: 138 additions & 0 deletions
138
src/com/magento/idea/magento2plugin/inspections/xml/PreferenceDeclarationInspection.java
This file contains hidden or 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,138 @@ | ||
/* | ||
* 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.PsiElement; | ||
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; | ||
|
||
@SuppressWarnings({"PMD.ExcessiveMethodLength", "PMD.NPathComplexity"}) | ||
public class PreferenceDeclarationInspection extends XmlSuppressableInspectionTool { | ||
|
||
@Override | ||
public @NotNull PsiElementVisitor buildVisitor( | ||
final @NotNull ProblemsHolder problemsHolder, | ||
final boolean isOnTheFly | ||
) { | ||
return new XmlElementVisitor() { | ||
|
||
Iamwade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private final InspectionBundle inspectionBundle = new InspectionBundle(); | ||
private final InspectionValidator phpClassExistenceValidator = | ||
new PhpClassExistenceValidator(problemsHolder.getProject()); | ||
private final InspectionValidator notEmptyValidator = new NotEmptyValidator(); | ||
|
||
@Override | ||
public void visitXmlTag(final XmlTag xmlTag) { | ||
final PsiFile file = xmlTag.getContainingFile(); | ||
|
||
if (!file.getName().equals(ModuleDiXml.FILE_NAME) | ||
|| !xmlTag.getName().equals(ModuleDiXml.PREFERENCE_TAG_NAME)) { | ||
return; | ||
} | ||
|
||
final XmlAttribute preferenceForAttribute = | ||
xmlTag.getAttribute(ModuleDiXml.PREFERENCE_ATTR_FOR); | ||
|
||
if (preferenceForAttribute == null | ||
|| preferenceForAttribute.getValue() == null | ||
|| preferenceForAttribute.getValueElement() == null | ||
|| preferenceForAttribute.getValueElement().getText().isEmpty()) { | ||
return; | ||
} | ||
|
||
if (!notEmptyValidator.validate(preferenceForAttribute.getValue())) { | ||
reportCouldNotBeEmpty( | ||
preferenceForAttribute.getValueElement(), | ||
preferenceForAttribute.getName() | ||
); | ||
} | ||
|
||
final XmlAttribute preferenceTypeAttribute = | ||
xmlTag.getAttribute(ModuleDiXml.TYPE_ATTR); | ||
|
||
if (preferenceTypeAttribute == null | ||
|| preferenceTypeAttribute.getValue() == null | ||
|| preferenceTypeAttribute.getValueElement() == null | ||
|| preferenceTypeAttribute.getValueElement().getText().isEmpty()) { | ||
return; | ||
} | ||
|
||
if (!notEmptyValidator.validate(preferenceTypeAttribute.getValue())) { | ||
reportCouldNotBeEmpty( | ||
preferenceTypeAttribute.getValueElement(), | ||
preferenceTypeAttribute.getName() | ||
); | ||
} | ||
|
||
if (!phpClassExistenceValidator.validate(preferenceForAttribute.getValue())) { | ||
reportClassDoesNotExists( | ||
preferenceForAttribute.getValueElement(), | ||
preferenceForAttribute.getValue() | ||
); | ||
} | ||
|
||
if (!phpClassExistenceValidator.validate(preferenceTypeAttribute.getValue())) { | ||
reportClassDoesNotExists( | ||
preferenceTypeAttribute.getValueElement(), | ||
preferenceTypeAttribute.getValue() | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Report Attribute Value could not be empty. | ||
* | ||
* @param psiElement PsiElement | ||
* @param messageParams Object... | ||
*/ | ||
private void reportCouldNotBeEmpty( | ||
final @NotNull PsiElement psiElement, | ||
final Object... messageParams | ||
) { | ||
problemsHolder.registerProblem( | ||
psiElement, | ||
inspectionBundle.message( | ||
"inspection.error.idAttributeCanNotBeEmpty", | ||
messageParams | ||
), | ||
ProblemHighlightType.ERROR | ||
); | ||
} | ||
|
||
/** | ||
* Report class does not exists. | ||
* | ||
* @param psiElement PsiElement | ||
* @param messageParams Object... | ||
*/ | ||
private void reportClassDoesNotExists( | ||
final @NotNull PsiElement psiElement, | ||
final Object... messageParams | ||
) { | ||
problemsHolder.registerProblem( | ||
psiElement, | ||
inspectionBundle.message( | ||
"inspection.warning.class.does.not.exist", | ||
messageParams | ||
), | ||
ProblemHighlightType.WARNING | ||
); | ||
} | ||
}; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
testData/inspections/xml/PreferenceDeclarationInspection/attrArgForValueIsEmpty/di.xml
This file contains hidden or 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,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="" type="Foo\Bar\Model\Logger"/> | ||
</config> |
5 changes: 5 additions & 0 deletions
5
testData/inspections/xml/PreferenceDeclarationInspection/attrArgTypeValueIsEmpty/di.xml
This file contains hidden or 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,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="Foo\Bar\Model\Logger" type=""/> | ||
</config> |
5 changes: 5 additions & 0 deletions
5
testData/inspections/xml/PreferenceDeclarationInspection/attrForClassExists/di.xml
This file contains hidden or 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,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="Foo\Bar\Model\Logger" type=""/> | ||
</config> |
5 changes: 5 additions & 0 deletions
5
testData/inspections/xml/PreferenceDeclarationInspection/attrTypeClassExists/di.xml
This file contains hidden or 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,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="" type="Foo\Bar\Model\Logger"/> | ||
</config> |
5 changes: 5 additions & 0 deletions
5
testData/inspections/xml/PreferenceDeclarationInspection/classAttrForDoesNotExists/di.xml
This file contains hidden or 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,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="Not\Existent\Class" type=""/> | ||
</config> |
5 changes: 5 additions & 0 deletions
5
testData/inspections/xml/PreferenceDeclarationInspection/classAttrForIsExist/di.xml
This file contains hidden or 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,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="Magento\Catalog\Api\ProductRepositoryInterface" type="Foo\Bar\Model\Logger"/> | ||
</config> |
5 changes: 5 additions & 0 deletions
5
testData/inspections/xml/PreferenceDeclarationInspection/classAttrTypeDoesNotExists/di.xml
This file contains hidden or 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,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="" type="Not\Existent\Class"/> | ||
</config> |
5 changes: 5 additions & 0 deletions
5
testData/inspections/xml/PreferenceDeclarationInspection/classAttrTypeIsExist/di.xml
This file contains hidden or 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,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="Magento\Catalog\Api\ProductRepositoryInterface" type="Foo\Bar\Model\Logger"/> | ||
</config> |
150 changes: 150 additions & 0 deletions
150
.../com/magento/idea/magento2plugin/inspections/xml/PreferenceDeclarationInspectionTest.java
This file contains hidden or 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,150 @@ | ||
/* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
package com.magento.idea.magento2plugin.inspections.xml; | ||
|
||
import com.magento.idea.magento2plugin.magento.files.ModuleDiXml; | ||
|
||
public class PreferenceDeclarationInspectionTest extends InspectionXmlFixtureTestCase { | ||
|
||
private static final String ARGUMENT_VALUE_IS_EMPTY = | ||
"inspection.error.idAttributeCanNotBeEmpty"; | ||
private static final String CLASS_DOES_NOT_EXIST = | ||
"inspection.warning.class.does.not.exist"; | ||
private static final String EXISTENT_CLASS_ONE = | ||
"Magento\\Catalog\\Api\\ProductRepositoryInterface"; | ||
private static final String EXISTENT_CLASS_TWO = | ||
"Foo\\Bar\\Model\\Logger"; | ||
private static final String NOT_EXISTENT_CLASS = | ||
"Not\\Existent\\Class"; | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
myFixture.enableInspections(PreferenceDeclarationInspection.class); | ||
} | ||
|
||
/** | ||
* Test for an error for the "for" attribute because it is empty. | ||
* <preference for="" type="Foo\Bar\Model\Logger"/> | ||
*/ | ||
public void testAttrArgForValueIsEmpty() { | ||
configureFixture(); | ||
|
||
final String forAttrIsEmptyMessage = inspectionBundle.message( | ||
ARGUMENT_VALUE_IS_EMPTY, | ||
ModuleDiXml.PREFERENCE_ATTR_FOR | ||
); | ||
|
||
assertHasHighlighting(forAttrIsEmptyMessage); | ||
} | ||
|
||
/** | ||
* Test for an error for the "type" attribute because it is empty. | ||
* <preference for="Foo\Bar\Model\Logger" type="Foo\Bar\Model\Logger"/> | ||
*/ | ||
public void testAttrArgTypeValueIsEmpty() { | ||
Iamwade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
configureFixture(); | ||
|
||
final String forAttrIsEmptyMessage = inspectionBundle.message( | ||
ARGUMENT_VALUE_IS_EMPTY, | ||
ModuleDiXml.TYPE_ATTR | ||
); | ||
|
||
assertHasHighlighting(forAttrIsEmptyMessage); | ||
} | ||
|
||
/** | ||
* Test for an no error for the "for" attribute because this class exists. | ||
* <preference for="Foo\Bar\Model\Logger" type=""/> | ||
*/ | ||
public void testAttrForClassExists() { | ||
Iamwade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
configureFixture(); | ||
|
||
final String typeAttrIsEmptyMessage = inspectionBundle.message( | ||
ARGUMENT_VALUE_IS_EMPTY, | ||
ModuleDiXml.PREFERENCE_ATTR_FOR | ||
); | ||
|
||
assertHasNoHighlighting(typeAttrIsEmptyMessage); | ||
} | ||
|
||
/** | ||
* Test for an no error for the "type" attribute because this class exists. | ||
* <preference for="" type="Foo\Bar\Model\Logger"/> | ||
*/ | ||
public void testAttrTypeClassExists() { | ||
configureFixture(); | ||
|
||
final String typeAttrIsEmptyMessage = inspectionBundle.message( | ||
ARGUMENT_VALUE_IS_EMPTY, | ||
ModuleDiXml.TYPE_ATTR | ||
); | ||
|
||
assertHasNoHighlighting(typeAttrIsEmptyMessage); | ||
} | ||
|
||
/** | ||
* Test for throwing an error for a class that does not exist for the "for" attribute. | ||
*/ | ||
public void testClassAttrForDoesNotExists() { | ||
configureFixture(); | ||
|
||
final String forClassDoesNotExists = inspectionBundle.message( | ||
CLASS_DOES_NOT_EXIST, | ||
NOT_EXISTENT_CLASS | ||
); | ||
|
||
assertHasHighlighting(forClassDoesNotExists); | ||
} | ||
|
||
/** | ||
* Test for the absence of an error in the presence of | ||
* classes or interfaces specified for preferences. | ||
*/ | ||
public void testClassAttrForIsExist() { | ||
configureFixture(); | ||
|
||
final String classOneExists = inspectionBundle.message( | ||
CLASS_DOES_NOT_EXIST, | ||
EXISTENT_CLASS_ONE | ||
); | ||
|
||
assertHasNoHighlighting(classOneExists); | ||
} | ||
|
||
/** | ||
* Test for throwing an error for a class that does not exist for the "type" attribute. | ||
*/ | ||
public void testClassAttrTypeDoesNotExists() { | ||
configureFixture(); | ||
|
||
final String forClassDoesNotExists = inspectionBundle.message( | ||
CLASS_DOES_NOT_EXIST, | ||
NOT_EXISTENT_CLASS | ||
); | ||
|
||
assertHasHighlighting(forClassDoesNotExists); | ||
} | ||
|
||
/** | ||
* Test for the absence of an error in the presence of | ||
* classes or interfaces specified for preferences. | ||
*/ | ||
public void testClassAttrTypeIsExist() { | ||
configureFixture(); | ||
|
||
final String classOneExists = inspectionBundle.message( | ||
CLASS_DOES_NOT_EXIST, | ||
EXISTENT_CLASS_TWO | ||
); | ||
|
||
assertHasNoHighlighting(classOneExists); | ||
} | ||
|
||
private void configureFixture() { | ||
myFixture.configureByFile(getFixturePath(ModuleDiXml.FILE_NAME)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.