Skip to content
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

#29 :- Inspection for using a block cacheable="false" attribute within default layout #62

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=============
Expand Down
7 changes: 7 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@
level="WARNING"
implementationClass="com.magento.idea.magento2plugin.inspections.xml.ObserverDeclarationInspection"/>

<localInspection language="XML" groupPath="XML"
shortName="CacheableFalseInDefaultLayoutInspection"
displayName="Inspection for disabled cache site-wide"
groupName="Magento 2"
enabledByDefault="true" level="WARNING"
implementationClass="com.magento.idea.magento2plugin.inspections.xml.CacheableFalseInDefaultLayoutInspection"/>

<libraryRoot id=".phpstorm.meta.php" path=".phpstorm.meta.php/" runtime="false"/>

<internalFileTemplate name="Magento Module Composer"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<html>
<body>
<p>
This inspection detects `cacheable` attribute which is set to be false in default.xml and its result to be whole site uncacheable.
</p>
<p>
<a href="https://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching.html#cache-over-cacheable">Read more about caching</a>
</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -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());
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
12 changes: 12 additions & 0 deletions src/com/magento/idea/magento2plugin/magento/files/LayoutXml.java
Original file line number Diff line number Diff line change
@@ -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";
}