Skip to content

Commit

Permalink
Merge pull request #2366 from Haehnchen/feature/inspection-language
Browse files Browse the repository at this point in the history
provide inspection language attributes for deprecated class and controller method inspections
  • Loading branch information
Haehnchen authored Apr 28, 2024
2 parents c28c265 + 6c7f156 commit 24f534a
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 176 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.lang.Language;
import com.intellij.lang.xml.XMLLanguage;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.util.PsiTreeUtil;
Expand All @@ -18,90 +16,90 @@
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.yaml.YAMLLanguage;
import org.jetbrains.yaml.psi.YAMLKeyValue;

/**
* @author Daniel Espendiller <[email protected]>
*/
public class ControllerMethodInspection extends LocalInspectionTool {

@NotNull
@Override
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if(!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}
public class ControllerMethodInspection {
public static class ControllerMethodInspectionYaml extends LocalInspectionTool {
public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}

return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
Language language = element.getLanguage();
return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (YamlElementPatternHelper.getSingleLineScalarKey("_controller", "controller").accepts(element)) {
String text = PsiElementUtils.trimQuote(element.getText());
if (StringUtils.isNotBlank(text)) {
InspectionUtil.inspectController(element, text, holder, new YamlLazyRouteName(element));
}
}

if (language == YAMLLanguage.INSTANCE) {
visitYamlElement(element, holder);
} else if(language == XMLLanguage.INSTANCE) {
visitXmlElement(element, holder);
super.visitElement(element);
}

super.visitElement(element);
}
};
}

private void visitYamlElement(@NotNull PsiElement element, @NotNull ProblemsHolder holder) {
if (YamlElementPatternHelper.getSingleLineScalarKey("_controller", "controller").accepts(element)) {
String text = PsiElementUtils.trimQuote(element.getText());
if (StringUtils.isNotBlank(text)) {
InspectionUtil.inspectController(element, text, holder, new YamlLazyRouteName(element));
}
};
}
}

public void visitXmlElement(@NotNull PsiElement element, @NotNull ProblemsHolder holder) {
if(XmlHelper.getRouteControllerPattern().accepts(element)) {
String text = PsiElementUtils.trimQuote(element.getText());
if(StringUtils.isNotBlank(text)) {
InspectionUtil.inspectController(element, text, holder, new XmlLazyRouteName(element));
public static class ControllerMethodInspectionXml extends LocalInspectionTool {
public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}

return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if(XmlHelper.getRouteControllerPattern().accepts(element)) {
String text = PsiElementUtils.trimQuote(element.getText());
if(StringUtils.isNotBlank(text)) {
InspectionUtil.inspectController(element, text, holder, new XmlLazyRouteName(element));
}
}

super.visitElement(element);
}
};
}
}

private record YamlLazyRouteName(@NotNull PsiElement psiElement) implements InspectionUtil.LazyControllerNameResolve {
@Nullable
@Override
public String getRouteName() {
YAMLKeyValue defaultKeyValue = PsiTreeUtil.getParentOfType(this.psiElement.getParent(), YAMLKeyValue.class);
if (defaultKeyValue == null) {
return null;
}

YAMLKeyValue def = PsiTreeUtil.getParentOfType(defaultKeyValue, YAMLKeyValue.class);
if (def == null) {
return null;
}
@Override
public String getRouteName() {
YAMLKeyValue defaultKeyValue = PsiTreeUtil.getParentOfType(this.psiElement.getParent(), YAMLKeyValue.class);
if (defaultKeyValue == null) {
return null;
}

return YamlHelper.getYamlKeyName(def);
YAMLKeyValue def = PsiTreeUtil.getParentOfType(defaultKeyValue, YAMLKeyValue.class);
if (def == null) {
return null;
}

return YamlHelper.getYamlKeyName(def);
}
}

private record XmlLazyRouteName(@NotNull PsiElement psiElement) implements InspectionUtil.LazyControllerNameResolve {

@Nullable
@Override
public String getRouteName() {
XmlTag defaultTag = PsiTreeUtil.getParentOfType(this.psiElement, XmlTag.class);
if (defaultTag != null) {
XmlTag routeTag = PsiTreeUtil.getParentOfType(defaultTag, XmlTag.class);
if (routeTag != null) {
XmlAttribute id = routeTag.getAttribute("id");
if (id != null) {
return id.getValue();
}
@Override
public String getRouteName() {
XmlTag defaultTag = PsiTreeUtil.getParentOfType(this.psiElement, XmlTag.class);
if (defaultTag != null) {
XmlTag routeTag = PsiTreeUtil.getParentOfType(defaultTag, XmlTag.class);
if (routeTag != null) {
XmlAttribute id = routeTag.getAttribute("id");
if (id != null) {
return id.getValue();
}
}

return null;
}

return null;
}
}
}
42 changes: 35 additions & 7 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -355,19 +355,47 @@
implementationClass="fr.adrienbrault.idea.symfony2plugin.config.xml.inspection.XmlDuplicateParameterKeyInspection"/>


<localInspection groupPath="Symfony" shortName="YamlControllerMethod" displayName="Missing Controller Action"
<localInspection groupPath="Symfony" shortName="ControllerMethodYaml" displayName="Missing Controller Action in Yaml"
groupName="Route"
enabledByDefault="true" level="WARNING"
implementationClass="fr.adrienbrault.idea.symfony2plugin.routing.inspection.ControllerMethodInspection"/>
enabledByDefault="true"
level="WARNING"
language="yaml"
implementationClass="fr.adrienbrault.idea.symfony2plugin.routing.inspection.ControllerMethodInspection$ControllerMethodInspectionYaml"/>

<localInspection groupPath="Symfony" shortName="ControllerMethodXml" displayName="Missing Controller Action in XML"
groupName="Route"
enabledByDefault="true"
level="WARNING"
language="XML"
implementationClass="fr.adrienbrault.idea.symfony2plugin.routing.inspection.ControllerMethodInspection$ControllerMethodInspectionXml"/>

<localInspection groupPath="Symfony" shortName="YamlDeprecatedClasses" displayName="Deprecated Class"

<localInspection groupPath="Symfony" shortName="DeprecatedClassesYaml" displayName="Deprecated Class in Yaml"
groupName="Service"
enabledByDefault="true" level="WARNING"
implementationClass="fr.adrienbrault.idea.symfony2plugin.codeInspection.service.ServiceDeprecatedClassesInspection"/>
enabledByDefault="true"
level="WARNING"
language="yaml"
implementationClass="fr.adrienbrault.idea.symfony2plugin.codeInspection.service.ServiceDeprecatedClassesInspection$ServiceDeprecatedClassesInspectionYaml"/>

<localInspection groupPath="Symfony" shortName="DeprecatedClassesXml" displayName="Deprecated Class in XML"
groupName="Service"
enabledByDefault="true"
level="WARNING"
language="XML"
implementationClass="fr.adrienbrault.idea.symfony2plugin.codeInspection.service.ServiceDeprecatedClassesInspection$ServiceDeprecatedClassesInspectionXml"/>

<localInspection groupPath="Symfony" shortName="DeprecatedClassesPhp" displayName="Deprecated Class in PHP"
groupName="Service"
enabledByDefault="true"
level="WARNING"
language="PHP"
implementationClass="fr.adrienbrault.idea.symfony2plugin.codeInspection.service.ServiceDeprecatedClassesInspection$ServiceDeprecatedClassesInspectionPhp"/>


<localInspection groupPath="Symfony" shortName="TaggedExtendsInterfaceClass" displayName="Missing Tag extends/interface statement"
groupName="Service"
enabledByDefault="true" level="WARNING"
enabledByDefault="true"
level="WARNING"
implementationClass="fr.adrienbrault.idea.symfony2plugin.codeInspection.service.TaggedExtendsInterfaceClassInspection"/>

<localInspection groupPath="Symfony" shortName="EventMethodCall" displayName="Create Method"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<!-- tooltip end -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<!-- tooltip end -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<!-- tooltip end -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<!-- tooltip end -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<!-- tooltip end -->
</body>
</html>

This file was deleted.

0 comments on commit 24f534a

Please sign in to comment.