Skip to content
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 @@ -31,6 +31,7 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool

private static class MyPsiElementVisitor extends PsiElementVisitor {
@NotNull private final ProblemsHolder holder;
private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector;
private ElementPattern<?> namedArgumentPattern;

MyPsiElementVisitor(@NotNull ProblemsHolder holder) {
Expand All @@ -40,14 +41,18 @@ private static class MyPsiElementVisitor extends PsiElementVisitor {
@Override
public void visitElement(@NotNull PsiElement element) {
if (getNamedArgumentPattern().accepts(element)) {
if (isSupportedDefinition(element) && ServiceContainerUtil.hasMissingYamlNamedArgumentForInspection(element, new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()))) {
if (isSupportedDefinition(element) && ServiceContainerUtil.hasMissingYamlNamedArgumentForInspection(element, getLazyServiceCollector())) {
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}

super.visitElement(element);
}

private ContainerCollectionResolver.LazyServiceCollector getLazyServiceCollector() {
return lazyServiceCollector != null ? lazyServiceCollector : (lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(holder.getProject()));
}

private ElementPattern<?> getNamedArgumentPattern() {
return namedArgumentPattern != null ? namedArgumentPattern : (namedArgumentPattern = YamlElementPatternHelper.getNamedArgumentPattern());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull Psi
return false;
}

if (!PhpElementsUtil.hasClassOrInterface(project, ROUTE_ATTRIBUTE_CLASS)) {
if (hasRouteAttribute(method)) {
return false;
}

if (hasRouteAttribute(method)) {
if (!isControllerClass(phpClass)) {
return false;
}

return isControllerClass(phpClass);
return PhpElementsUtil.hasClassOrInterface(project, ROUTE_ATTRIBUTE_CLASS);
}

private boolean hasRouteAttribute(@NotNull Method method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull Psi
return false;
}

if (PhpElementsUtil.isInstanceOf(phpClass, "\\Symfony\\Component\\Console\\Command\\Command")) {
if (phpClass.getAttributes(AS_COMMAND_ATTRIBUTE).isEmpty()) {
return false;
}

if (phpClass.getAttributes(AS_COMMAND_ATTRIBUTE).isEmpty()) {
if (PhpElementsUtil.isInstanceOf(phpClass, "\\Symfony\\Component\\Console\\Command\\Command")) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.routing.PhpRouteReferenceContributor;
import fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper;
Expand Down Expand Up @@ -38,7 +39,7 @@ private static class MyPsiElementVisitor extends PsiElementVisitor {

@Override
public void visitElement(@NotNull PsiElement element) {
if(getMethodWithFirstStringPattern().accepts(element)) {
if(element instanceof StringLiteralExpression && getMethodWithFirstStringPattern().accepts(element)) {
String contents = PhpElementsUtil.getStringValue(element);
if(StringUtils.isNotBlank(contents)) {
invoke(contents, element, holder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean is
return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
invoke(holder, element);
if (element instanceof StringLiteralExpression stringLiteralExpression) {
invoke(holder, stringLiteralExpression);
}
super.visitElement(element);
}
};
}

private void invoke(@NotNull ProblemsHolder holder, @NotNull PsiElement psiElement) {
if (!(psiElement instanceof StringLiteralExpression)) {
return;
}

String templateNameIfMissing = getTemplateNameIfMissing((StringLiteralExpression) psiElement);
private void invoke(@NotNull ProblemsHolder holder, @NotNull StringLiteralExpression psiElement) {
String templateNameIfMissing = getTemplateNameIfMissing(psiElement);
if(templateNameIfMissing == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,25 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class TwigHtmlLineMarkerProvider implements LineMarkerProvider {
@Override
public void collectSlowLineMarkers(@NotNull List<? extends PsiElement> psiElements, @NotNull Collection<? super LineMarkerInfo<?>> results) {
if (psiElements.isEmpty() || !Symfony2ProjectComponent.isEnabled(psiElements.get(0))) {
if (psiElements.isEmpty() || !Symfony2ProjectComponent.isEnabled(psiElements.getFirst())) {
return;
}

PsiFile file = psiElements.get(0).getContainingFile();
PsiFile file = psiElements.getFirst().getContainingFile();
if (!isSupportedFile(file)) {
return;
}

Map<String, Boolean> componentBlockExistsCache = new HashMap<>();

for (PsiElement psiElement : psiElements) {
if (!(psiElement instanceof XmlToken xmlToken) || xmlToken.getNode().getElementType() != XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN) {
continue;
Expand All @@ -50,7 +53,7 @@ public void collectSlowLineMarkers(@NotNull List<? extends PsiElement> psiElemen
continue;
}

LineMarkerInfo<?> blockOverride = attachTwigComponentBlockOverride(xmlAttributeValue, xmlToken);
LineMarkerInfo<?> blockOverride = attachTwigComponentBlockOverride(xmlAttributeValue, xmlToken, componentBlockExistsCache);
if (blockOverride != null) {
results.add(blockOverride);
}
Expand All @@ -68,7 +71,7 @@ public static boolean isSupportedFile(@Nullable PsiFile file) {
}

@Nullable
protected LineMarkerInfo<?> attachTwigComponentBlockOverride(@NotNull XmlAttributeValue xmlAttributeValue, @NotNull PsiElement lineMarkerTarget) {
protected LineMarkerInfo<?> attachTwigComponentBlockOverride(@NotNull XmlAttributeValue xmlAttributeValue, @NotNull PsiElement lineMarkerTarget, @NotNull Map<String, Boolean> componentBlockExistsCache) {
if (!(xmlAttributeValue.getParent() instanceof XmlAttribute xmlAttribute) || !"name".equals(xmlAttribute.getName())) {
return null;
}
Expand Down Expand Up @@ -100,7 +103,8 @@ protected LineMarkerInfo<?> attachTwigComponentBlockOverride(@NotNull XmlAttribu
}

Project project = xmlAttributeValue.getProject();
if (!hasComponentBlock(project, componentName, blockName)) {
String cacheKey = componentName + "\0" + blockName;
if (!componentBlockExistsCache.computeIfAbsent(cacheKey, ignored -> hasComponentBlock(project, componentName, blockName))) {
return null;
}

Expand All @@ -111,6 +115,11 @@ protected LineMarkerInfo<?> attachTwigComponentBlockOverride(@NotNull XmlAttribu
return builder.createLineMarkerInfo(lineMarkerTarget);
}

@Nullable
protected LineMarkerInfo<?> attachTwigComponentBlockOverride(@NotNull XmlAttributeValue xmlAttributeValue, @NotNull PsiElement lineMarkerTarget) {
return attachTwigComponentBlockOverride(xmlAttributeValue, lineMarkerTarget, new HashMap<>());
}

@Nullable
protected String resolveComponentName(@NotNull Project project, @NotNull String rawComponentName) {
return UxUtil.resolveTwigComponentName(project, rawComponentName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ private static class AssetPackageElementVisitor extends PsiElementVisitor {
public void visitElement(@NotNull PsiElement element) {
if (element instanceof StringLiteralExpression) {
MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter((StringLiteralExpression) element);
if (methodReference != null && (PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, "\\Symfony\\Component\\Asset\\Packages", "getUrl")
String methodName = methodReference != null ? methodReference.getName() : null;
if (methodName != null && ("getUrl".equals(methodName) || "getVersion".equals(methodName)) && (PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, "\\Symfony\\Component\\Asset\\Packages", "getUrl")
|| PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, "\\Symfony\\Component\\Asset\\Packages", "getVersion")
|| PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, "\\Symfony\\Component\\Asset\\PackageInterface", "getUrl")
|| PhpElementsUtil.isMethodReferenceInstanceOf(methodReference, "\\Symfony\\Component\\Asset\\PackageInterface", "getVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
public class TemplateMissingAnnotationPhpAttributeLocalInspection extends LocalInspectionTool {
@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) {
Expand All @@ -38,7 +42,7 @@ public void visitElement(@NotNull PsiElement element) {

if (element instanceof PhpAttribute) {
String fqn = ((PhpAttribute) element).getFQN();
if (fqn != null && Arrays.stream(TwigUtil.TEMPLATE_ANNOTATION_CLASS).anyMatch(s -> PhpElementsUtil.isInstanceOf(element.getProject(), fqn, s))) {
if (fqn != null && PhpElementsUtil.isEqualClassName(fqn, TwigUtil.TEMPLATE_ANNOTATION_CLASS)) {
annotate((PhpAttribute) element, holder);
}
}
Expand Down Expand Up @@ -74,10 +78,6 @@ private void annotate(@NotNull PhpAttribute phpAttribute, @NotNull ProblemsHolde
}

private void annotate(@NotNull PhpDocTag phpDocTag, @NotNull ProblemsHolder holder) {
if(!Symfony2ProjectComponent.isEnabled(phpDocTag.getProject())) {
return;
}

PhpDocTagAnnotation phpDocAnnotationContainer = AnnotationUtil.getPhpDocAnnotationContainer(phpDocTag);
if (phpDocAnnotationContainer == null || !PhpElementsUtil.isEqualClassName(phpDocAnnotationContainer.getPhpClass(), TwigUtil.TEMPLATE_ANNOTATION_CLASS)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.jetbrains.twig.TwigTokenTypes;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.NonNull;

/**
* asset('<caret>')
Expand All @@ -36,7 +39,12 @@ private static class MyPsiElementVisitor extends PsiElementVisitor {
}

@Override
public void visitElement(PsiElement element) {
public void visitElement(@NonNull PsiElement element) {
if (!(element instanceof LeafPsiElement) || element.getNode().getElementType() != TwigTokenTypes.STRING_TEXT) {
super.visitElement(element);
return;
}

if(getAssetPattern().accepts(element) && TwigUtil.isValidStringWithoutInterpolatedOrConcat(element)) {
invoke(element, holder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.patterns.ElementPattern;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
Expand Down Expand Up @@ -51,7 +52,13 @@ private static class MyPsiElementVisitor extends PsiElementVisitor {
@Override
public void visitElement(@NotNull PsiElement element) {
// Fast pre-filter: only STRING_TEXT elements can be enum/enum_cases arguments
if (element instanceof LeafPsiElement && element.getNode() == null || element.getNode().getElementType() != TwigTokenTypes.STRING_TEXT) {
if (!(element instanceof LeafPsiElement)) {
super.visitElement(element);
return;
}

ASTNode node = element.getNode();
if (node == null || node.getElementType() != TwigTokenTypes.STRING_TEXT) {
super.visitElement(element);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.jetbrains.twig.TwigTokenTypes;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.templating.TwigPattern;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
Expand Down Expand Up @@ -48,6 +50,11 @@ private static class MyPsiElementVisitor extends PsiElementVisitor {

@Override
public void visitElement(PsiElement element) {
if (!(element instanceof LeafPsiElement) || element.getNode().getElementType() != TwigTokenTypes.STRING_TEXT) {
super.visitElement(element);
return;
}

if((getTemplateFileReferencePattern().accepts(element) || getIncludeFunctionPattern().accepts(element)) && TwigUtil.isValidStringWithoutInterpolatedOrConcat(element)) {
invoke(element, holder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean is
return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
invoke(holder, element);
if (element instanceof StringLiteralExpression stringLiteralExpression) {
invoke(holder, stringLiteralExpression);
}
super.visitElement(element);
}
};
}

private void invoke(@NotNull ProblemsHolder holder, @NotNull PsiElement psiElement) {
private void invoke(@NotNull ProblemsHolder holder, @NotNull StringLiteralExpression psiElement) {
ParameterListOwner methodReferenceOrNewExpression = TranslationUtil.getTranslationFunctionContext(psiElement);
if (methodReferenceOrNewExpression == null) {
return;
Expand All @@ -58,7 +60,7 @@ private void invoke(@NotNull ProblemsHolder holder, @NotNull PsiElement psiEleme
&& TranslationUtil.isTranslationReference(methodReferenceOrNewExpression);

if (isSupportedAttributeInsideContext) {
annotateTranslationDomain((StringLiteralExpression) psiElement, holder);
annotateTranslationDomain(psiElement, holder);
}
}

Expand All @@ -70,7 +72,7 @@ private void invoke(@NotNull ProblemsHolder holder, @NotNull PsiElement psiEleme
if (domainParameter >= 0) {
ParameterBag currentIndex = PsiElementUtils.getCurrentParameterIndex(psiElement);
if(currentIndex != null && currentIndex.getIndex() == domainParameter) {
annotateTranslationDomain((StringLiteralExpression) psiElement, holder);
annotateTranslationDomain(psiElement, holder);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ public class PhpTranslationKeyInspection extends LocalInspectionTool {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
if(!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}

return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
invoke(holder, element);
if (element instanceof StringLiteralExpression stringLiteralExpression) {
invoke(holder, stringLiteralExpression);
}
super.visitElement(element);
}
};
}

private void invoke(@NotNull ProblemsHolder holder, @NotNull PsiElement psiElement) {
private void invoke(@NotNull ProblemsHolder holder, @NotNull StringLiteralExpression psiElement) {
ParameterListOwner methodReferenceOrNewExpression = TranslationUtil.getTranslationFunctionContext(psiElement);
if (methodReferenceOrNewExpression == null) {
return;
Expand All @@ -55,12 +57,12 @@ private void invoke(@NotNull ProblemsHolder holder, @NotNull PsiElement psiEleme
PsiElement domainElement = parameterList.getParameter("domain", PhpTranslationDomainInspection.getDomainParameter(methodReferenceOrNewExpression));
if(domainElement == null) {
// no domain found; fallback to default domain
annotateTranslationKey((StringLiteralExpression) psiElement, "messages", holder);
annotateTranslationKey(psiElement, "messages", holder);
} else {
// resolve string in parameter
String domain = PhpElementsUtil.getStringValue(domainElement);
if(domain != null) {
annotateTranslationKey((StringLiteralExpression) psiElement, domain, holder);
annotateTranslationKey(psiElement, domain, holder);
}
}
}
Expand Down
Loading
Loading