diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9b7c06c2a..ee625da64 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@
* Create a New Magento 2 Module action
* Code Inspection: Duplicated Observer Usage in events XML
* Create a Plugin class for a class public method action
+ * Code Inspection: Warning regarding Cacheable false attribute in default XML
0.3.0
=============
diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml
index b1dc4920a..480e048b8 100644
--- a/resources/META-INF/plugin.xml
+++ b/resources/META-INF/plugin.xml
@@ -117,6 +117,13 @@
level="WARNING"
implementationClass="com.magento.idea.magento2plugin.inspections.xml.ObserverDeclarationInspection"/>
+
+ This inspection detects `cacheable` attribute which is set to be false in default.xml and its result to be whole site uncacheable. +
+ + + diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/CacheableFalseInDefaultLayoutInspection.java b/src/com/magento/idea/magento2plugin/inspections/xml/CacheableFalseInDefaultLayoutInspection.java new file mode 100644 index 000000000..f99f43158 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/inspections/xml/CacheableFalseInDefaultLayoutInspection.java @@ -0,0 +1,40 @@ +/* + * 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.XmlElementVisitor; +import com.intellij.psi.xml.XmlAttribute; +import com.magento.idea.magento2plugin.inspections.xml.fix.XmlRemoveCacheableAttributeQuickFix; +import com.magento.idea.magento2plugin.magento.files.LayoutXml; +import org.jetbrains.annotations.NotNull; + +public class CacheableFalseInDefaultLayoutInspection extends XmlSuppressableInspectionTool { + @NotNull + @Override + public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, final boolean isOnTheFly) { + return new XmlElementVisitor() { + private static final String CacheDisableProblemDescription = "Cacheable false attribute on the default layout will disable cache site-wide"; + @Override + public void visitXmlAttribute(XmlAttribute attribute) { + String fileName = holder.getFile().getName(); + if (!fileName.equals(LayoutXml.DefaultFileName)) return; + final String text = attribute.getValue(); + final String attributeName = attribute.getName(); + if (!attributeName.equals(LayoutXml.CacheableAttributeName)) return; + if (!attribute.getParent().getName().equals(LayoutXml.BlockAttributeTagName)) return; + if (text == null) return; + if (text.equals(LayoutXml.CacheableAttributeFalseValue)) { + holder.registerProblem(attribute, CacheDisableProblemDescription, + ProblemHighlightType.WARNING, + new XmlRemoveCacheableAttributeQuickFix()); + } + } + }; + } +} diff --git a/src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java b/src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java new file mode 100644 index 000000000..6b74347f9 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/inspections/xml/fix/XmlRemoveCacheableAttributeQuickFix.java @@ -0,0 +1,23 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.inspections.xml.fix; + +import com.intellij.codeInspection.LocalQuickFix; +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.NotNull; + +public class XmlRemoveCacheableAttributeQuickFix implements LocalQuickFix { + @NotNull + @Override + public String getFamilyName() { + return "Remove cacheable attribute"; + } + + @Override + public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { + descriptor.getPsiElement().delete(); + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/LayoutXml.java b/src/com/magento/idea/magento2plugin/magento/files/LayoutXml.java new file mode 100644 index 000000000..647e2cec0 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/files/LayoutXml.java @@ -0,0 +1,12 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +package com.magento.idea.magento2plugin.magento.files; + +public class LayoutXml { + public static String DefaultFileName = "default.xml"; + public static String CacheableAttributeName = "cacheable"; + public static String CacheableAttributeFalseValue = "false"; + public static String BlockAttributeTagName = "block"; +}