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

provide inspection language attributes for tag implementations, container / route deprecated #2367

Merged
merged 1 commit into from
Apr 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.lang.Language;
import com.intellij.lang.xml.XMLLanguage;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
Expand All @@ -23,7 +21,6 @@
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.YAMLTokenTypes;
import org.jetbrains.yaml.psi.YAMLCompoundValue;
import org.jetbrains.yaml.psi.YAMLKeyValue;
Expand All @@ -34,98 +31,118 @@
/**
* @author Daniel Espendiller <[email protected]>
*/
public class TaggedExtendsInterfaceClassInspection extends LocalInspectionTool {
public class TaggedExtendsInterfaceClassInspection {
public static class TaggedExtendsInterfaceClassInspectionYaml extends LocalInspectionTool {
public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}

@NotNull
@Override
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}
return new PsiElementVisitor() {
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector;

return new PsiElementVisitor() {
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector;
@Override
public void visitElement(@NotNull PsiElement element) {
visitYamlElement(element, holder);
super.visitElement(element);
}

@Override
public void visitElement(@NotNull PsiElement element) {
Language language = element.getLanguage();
private void visitYamlElement(@NotNull PsiElement psiElement, @NotNull ProblemsHolder holder) {
if (YamlElementPatternHelper.getSingleLineScalarKey("class").accepts(psiElement)) {

if (language == YAMLLanguage.INSTANCE) {
visitYamlElement(element, holder, this.createLazyServiceCollector());
} else if (language == XMLLanguage.INSTANCE) {
visitXmlElement(element, holder, this.createLazyServiceCollector());
}
// class: '\Foo'
String text = PsiElementUtils.trimQuote(psiElement.getText());
if (StringUtils.isBlank(text)) {
return;
}

super.visitElement(element);
}
PsiElement yamlScalar = psiElement.getParent();
if (!(yamlScalar instanceof YAMLScalar)) {
return;
}

private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> createLazyServiceCollector() {
if (this.serviceCollector == null) {
this.serviceCollector = NotNullLazyValue.lazy(() -> new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
PsiElement classKey = yamlScalar.getParent();
if (classKey instanceof YAMLKeyValue) {
PsiElement yamlCompoundValue = classKey.getParent();
if (yamlCompoundValue instanceof YAMLCompoundValue) {
PsiElement serviceKeyValue = yamlCompoundValue.getParent();
if (serviceKeyValue instanceof YAMLKeyValue) {
Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) serviceKeyValue);
if (!tags.isEmpty()) {
registerTaggedProblems(psiElement, tags, text, holder, this.createLazyServiceCollector());
}
}
}
}
} else if (psiElement.getNode().getElementType() == YAMLTokenTypes.SCALAR_KEY && YamlElementPatternHelper.getServiceIdKeyValuePattern().accepts(psiElement.getParent())) {
// Foobar\Foo: ~
String text = PsiElementUtils.getText(psiElement);
if (StringUtils.isNotBlank(text) && YamlHelper.isClassServiceId(text) && text.contains("\\")) {
PsiElement yamlKeyValue = psiElement.getParent();
if (yamlKeyValue instanceof YAMLKeyValue && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "resource") == null && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "exclude") == null) {
Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) yamlKeyValue);
if (!tags.isEmpty()) {
registerTaggedProblems(psiElement, tags, text, holder, this.createLazyServiceCollector());
}
}
}
}
}

return this.serviceCollector;
}
};
}
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> createLazyServiceCollector() {
if (this.serviceCollector == null) {
this.serviceCollector = NotNullLazyValue.lazy(() -> new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
}

private void visitXmlElement(@NotNull PsiElement element, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {
String className = getClassNameFromServiceDefinition(element);
if (className != null) {
XmlTag parentOfType = PsiTreeUtil.getParentOfType(element, XmlTag.class);
if (parentOfType != null) {
// attach problems to string value only
PsiElement[] psiElements = element.getChildren();
if (psiElements.length > 2) {
registerTaggedProblems(psiElements[1], FormUtil.getTags(parentOfType), className, holder, lazyServiceCollector);
return this.serviceCollector;
}
}
};
}
}

private void visitYamlElement(@NotNull PsiElement psiElement, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {
if (YamlElementPatternHelper.getSingleLineScalarKey("class").accepts(psiElement)) {
public static class TaggedExtendsInterfaceClassInspectionXml extends LocalInspectionTool {
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector;

// class: '\Foo'
String text = PsiElementUtils.trimQuote(psiElement.getText());
if (StringUtils.isBlank(text)) {
return;
public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}

PsiElement yamlScalar = psiElement.getParent();
if (!(yamlScalar instanceof YAMLScalar)) {
return;
}
return new PsiElementVisitor() {
private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> serviceCollector;

@Override
public void visitElement(@NotNull PsiElement element) {
visitXmlElement(element, holder);
super.visitElement(element);
}

PsiElement classKey = yamlScalar.getParent();
if (classKey instanceof YAMLKeyValue) {
PsiElement yamlCompoundValue = classKey.getParent();
if (yamlCompoundValue instanceof YAMLCompoundValue) {
PsiElement serviceKeyValue = yamlCompoundValue.getParent();
if (serviceKeyValue instanceof YAMLKeyValue) {
Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) serviceKeyValue);
if (!tags.isEmpty()) {
registerTaggedProblems(psiElement, tags, text, holder, lazyServiceCollector);
private void visitXmlElement(@NotNull PsiElement element, @NotNull ProblemsHolder holder) {
String className = getClassNameFromServiceDefinition(element);
if (className != null) {
XmlTag parentOfType = PsiTreeUtil.getParentOfType(element, XmlTag.class);
if (parentOfType != null) {
// attach problems to string value only
PsiElement[] psiElements = element.getChildren();
if (psiElements.length > 2) {
registerTaggedProblems(psiElements[1], FormUtil.getTags(parentOfType), className, holder, createLazyServiceCollector());
}
}
}
}
}
} else if (psiElement.getNode().getElementType() == YAMLTokenTypes.SCALAR_KEY && YamlElementPatternHelper.getServiceIdKeyValuePattern().accepts(psiElement.getParent())) {
// Foobar\Foo: ~
String text = PsiElementUtils.getText(psiElement);
if (StringUtils.isNotBlank(text) && YamlHelper.isClassServiceId(text) && text.contains("\\")) {
PsiElement yamlKeyValue = psiElement.getParent();
if (yamlKeyValue instanceof YAMLKeyValue && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "resource") == null && YamlHelper.getYamlKeyValue((YAMLKeyValue) yamlKeyValue, "exclude") == null) {
Set<String> tags = YamlHelper.collectServiceTags((YAMLKeyValue) yamlKeyValue);
if (!tags.isEmpty()) {
registerTaggedProblems(psiElement, tags, text, holder, lazyServiceCollector);

private NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> createLazyServiceCollector() {
if (this.serviceCollector == null) {
this.serviceCollector = NotNullLazyValue.lazy(() -> new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
}

return this.serviceCollector;
}
}
};
}
}

private void registerTaggedProblems(@NotNull PsiElement source, @NotNull Set<String> tags, @NotNull String serviceClass, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {
private static void registerTaggedProblems(@NotNull PsiElement source, @NotNull Set<String> tags, @NotNull String serviceClass, @NotNull ProblemsHolder holder, @NotNull NotNullLazyValue<ContainerCollectionResolver.LazyServiceCollector> lazyServiceCollector) {
if (tags.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,69 @@
/**
* @author Daniel Espendiller <[email protected]>
*/
public class ContainerSettingDeprecatedInspection extends LocalInspectionTool {
public class ContainerSettingDeprecatedInspection {
public static class ContainerSettingDeprecatedInspectionYaml extends LocalInspectionTool {
public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}

@NotNull
public 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 (element instanceof YAMLKeyValue) {
registerYmlRoutePatternProblem(holder, (YAMLKeyValue) element);
}

return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if(element instanceof XmlAttribute) {
registerXmlAttributeProblem(holder, (XmlAttribute) element);
} else if(element instanceof YAMLKeyValue) {
registerYmlRoutePatternProblem(holder, (YAMLKeyValue) element);
super.visitElement(element);
}
};
}
}

super.visitElement(element);
public static class ContainerSettingDeprecatedInspectionXml 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 (element instanceof XmlAttribute) {
registerXmlAttributeProblem(holder, (XmlAttribute) element);
}

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

private void registerYmlRoutePatternProblem(@NotNull ProblemsHolder holder, @NotNull YAMLKeyValue element) {
private static void registerYmlRoutePatternProblem(@NotNull ProblemsHolder holder, @NotNull YAMLKeyValue element) {
String s = PsiElementUtils.trimQuote(element.getKeyText());
if(("factory_class".equals(s) || "factory_method".equals(s) || "factory_service".equals(s)) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
if (("factory_class".equals(s) || "factory_method".equals(s) || "factory_service".equals(s)) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
// services:
// foo:
// factory_*:
registerProblem(holder, element.getKey());
}
}

private void registerXmlAttributeProblem(@NotNull ProblemsHolder holder, @NotNull XmlAttribute xmlAttribute) {
private static void registerXmlAttributeProblem(@NotNull ProblemsHolder holder, @NotNull XmlAttribute xmlAttribute) {
String name = xmlAttribute.getName();
if(!("factory-class".equals(name) || "factory-method".equals(name) || "factory-service".equals(name))) {
if (!("factory-class".equals(name) || "factory-method".equals(name) || "factory-service".equals(name))) {
return;
}

XmlTag xmlTagRoute = PsiElementAssertUtil.getParentOfTypeWithNameOrNull(xmlAttribute, XmlTag.class, "service");
if(xmlTagRoute != null) {
if (xmlTagRoute != null) {
registerProblem(holder, xmlAttribute.getFirstChild());
}
}

private void registerProblem(@NotNull ProblemsHolder holder, @Nullable PsiElement target) {
if(target == null) {
private static void registerProblem(@NotNull ProblemsHolder holder, @Nullable PsiElement target) {
if (target == null) {
return;
}

Expand Down
Loading
Loading