Skip to content

Commit ae40c5f

Browse files
author
Vitaliy
authored
Merge pull request #62 from konarshankar07/inspection-cacheable-false--task-29
#29 :- Inspection for using a block cacheable="false" attribute within default layout
2 parents 3554036 + 49559bd commit ae40c5f

File tree

6 files changed

+99
-0
lines changed

6 files changed

+99
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Create a New Magento 2 Module action
1212
* Code Inspection: Duplicated Observer Usage in events XML
1313
* Create a Plugin class for a class public method action
14+
* Code Inspection: Warning regarding Cacheable false attribute in default XML
1415

1516
0.3.0
1617
=============

resources/META-INF/plugin.xml

+7
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@
117117
level="WARNING"
118118
implementationClass="com.magento.idea.magento2plugin.inspections.xml.ObserverDeclarationInspection"/>
119119

120+
<localInspection language="XML" groupPath="XML"
121+
shortName="CacheableFalseInDefaultLayoutInspection"
122+
displayName="Inspection for disabled cache site-wide"
123+
groupName="Magento 2"
124+
enabledByDefault="true" level="WARNING"
125+
implementationClass="com.magento.idea.magento2plugin.inspections.xml.CacheableFalseInDefaultLayoutInspection"/>
126+
120127
<libraryRoot id=".phpstorm.meta.php" path=".phpstorm.meta.php/" runtime="false"/>
121128

122129
<internalFileTemplate name="Magento Module Composer"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<html>
8+
<body>
9+
<p>
10+
This inspection detects `cacheable` attribute which is set to be false in default.xml and its result to be whole site uncacheable.
11+
</p>
12+
<p>
13+
<a href="https://devdocs.magento.com/guides/v2.3/extension-dev-guide/cache/page-caching.html#cache-over-cacheable">Read more about caching</a>
14+
</p>
15+
</body>
16+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.inspections.xml;
6+
7+
import com.intellij.codeInspection.ProblemHighlightType;
8+
import com.intellij.codeInspection.ProblemsHolder;
9+
import com.intellij.codeInspection.XmlSuppressableInspectionTool;
10+
import com.intellij.psi.PsiElementVisitor;
11+
import com.intellij.psi.XmlElementVisitor;
12+
import com.intellij.psi.xml.XmlAttribute;
13+
import com.magento.idea.magento2plugin.inspections.xml.fix.XmlRemoveCacheableAttributeQuickFix;
14+
import com.magento.idea.magento2plugin.magento.files.LayoutXml;
15+
import org.jetbrains.annotations.NotNull;
16+
17+
public class CacheableFalseInDefaultLayoutInspection extends XmlSuppressableInspectionTool {
18+
@NotNull
19+
@Override
20+
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, final boolean isOnTheFly) {
21+
return new XmlElementVisitor() {
22+
private static final String CacheDisableProblemDescription = "Cacheable false attribute on the default layout will disable cache site-wide";
23+
@Override
24+
public void visitXmlAttribute(XmlAttribute attribute) {
25+
String fileName = holder.getFile().getName();
26+
if (!fileName.equals(LayoutXml.DefaultFileName)) return;
27+
final String text = attribute.getValue();
28+
final String attributeName = attribute.getName();
29+
if (!attributeName.equals(LayoutXml.CacheableAttributeName)) return;
30+
if (!attribute.getParent().getName().equals(LayoutXml.BlockAttributeTagName)) return;
31+
if (text == null) return;
32+
if (text.equals(LayoutXml.CacheableAttributeFalseValue)) {
33+
holder.registerProblem(attribute, CacheDisableProblemDescription,
34+
ProblemHighlightType.WARNING,
35+
new XmlRemoveCacheableAttributeQuickFix());
36+
}
37+
}
38+
};
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.inspections.xml.fix;
6+
7+
import com.intellij.codeInspection.LocalQuickFix;
8+
import com.intellij.codeInspection.ProblemDescriptor;
9+
import com.intellij.openapi.project.Project;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
public class XmlRemoveCacheableAttributeQuickFix implements LocalQuickFix {
13+
@NotNull
14+
@Override
15+
public String getFamilyName() {
16+
return "Remove cacheable attribute";
17+
}
18+
19+
@Override
20+
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
21+
descriptor.getPsiElement().delete();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
package com.magento.idea.magento2plugin.magento.files;
6+
7+
public class LayoutXml {
8+
public static String DefaultFileName = "default.xml";
9+
public static String CacheableAttributeName = "cacheable";
10+
public static String CacheableAttributeFalseValue = "false";
11+
public static String BlockAttributeTagName = "block";
12+
}

0 commit comments

Comments
 (0)