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
@@ -0,0 +1,48 @@
package fr.adrienbrault.idea.symfony2plugin.action.quickfix;

import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlPsiElementFactory;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.psi.YAMLKeyValue;

public class CorrectClassNameCasingYamlLocalQuickFix implements LocalQuickFix {

private final String replacementFQN;

public CorrectClassNameCasingYamlLocalQuickFix(String replacementFQN) {

this.replacementFQN = replacementFQN;
}

@Nls(capitalization = Nls.Capitalization.Sentence)
@NotNull
@Override
public String getFamilyName() {
return "YAML";
}

@Nls(capitalization = Nls.Capitalization.Sentence)
@NotNull
@Override
public String getName() {
return "Use " + replacementFQN;
}

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement psiElement1 = descriptor.getPsiElement();
YAMLKeyValue replacement = YamlPsiElementFactory.createFromText(
project,
YAMLKeyValue.class,
"class: " + replacementFQN
);

if (replacement != null && replacement.getValue() != null) {
psiElement1.replace(replacement.getValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void visitElement(PsiElement element) {
} else if(element.getNode().getElementType() == YAMLTokenTypes.TEXT) {
// @service
String text = element.getText();
if(text != null && StringUtils.isNotBlank(text) && text.startsWith("@")) {
if(StringUtils.isNotBlank(text) && text.startsWith("@")) {
this.problemRegistrar.attachDeprecatedProblem(element, text.substring(1), holder);
this.problemRegistrar.attachServiceDeprecatedProblem(element, text.substring(1), holder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.action.quickfix.CorrectClassNameCasingYamlLocalQuickFix;
import fr.adrienbrault.idea.symfony2plugin.config.yaml.YamlElementPatternHelper;
import fr.adrienbrault.idea.symfony2plugin.stubs.ContainerCollectionResolver;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
Expand All @@ -20,6 +22,10 @@
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class YamlClassInspection extends LocalInspectionTool {

public static final String MESSAGE_WRONG_CASING = "Wrong class casing";
public static final String MESSAGE_MISSING_CLASS = "Missing class";

@NotNull
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
Expand All @@ -29,7 +35,7 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool
return new PsiElementVisitor() {
@Override
public void visitElement(PsiElement psiElement) {
if(!((YamlElementPatternHelper.getSingleLineScalarKey("class", "factory_class").accepts(psiElement) || YamlElementPatternHelper.getParameterClassPattern().accepts(psiElement)) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(psiElement))) {
if (!((YamlElementPatternHelper.getSingleLineScalarKey("class", "factory_class").accepts(psiElement) || YamlElementPatternHelper.getParameterClassPattern().accepts(psiElement)) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(psiElement))) {
super.visitElement(psiElement);
return;
}
Expand All @@ -44,15 +50,18 @@ public void visitElement(PsiElement psiElement) {
private void invoke(@NotNull final PsiElement psiElement, @NotNull ProblemsHolder holder) {
String className = PsiElementUtils.getText(psiElement);

if(YamlHelper.isValidParameterName(className)) {
if (YamlHelper.isValidParameterName(className)) {
String resolvedParameter = ContainerCollectionResolver.resolveParameter(psiElement.getProject(), className);
if(resolvedParameter != null && PhpIndex.getInstance(psiElement.getProject()).getAnyByFQN(resolvedParameter).size() > 0) {
return ;
if (resolvedParameter != null && PhpIndex.getInstance(psiElement.getProject()).getAnyByFQN(resolvedParameter).size() > 0) {
return;
}
}

if(PhpElementsUtil.getClassInterface(psiElement.getProject(), className) == null) {
holder.registerProblem(psiElement, "Missing Class", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
PhpClass foundClass = PhpElementsUtil.getClassInterface(psiElement.getProject(), className);
if (foundClass == null) {
holder.registerProblem(psiElement, MESSAGE_MISSING_CLASS, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
} else if (!foundClass.getPresentableFQN().equals(className)) {
holder.registerProblem(psiElement, MESSAGE_WRONG_CASING, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new CorrectClassNameCasingYamlLocalQuickFix(foundClass.getPresentableFQN()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package fr.adrienbrault.idea.symfony2plugin.tests.dic.inspection;

import fr.adrienbrault.idea.symfony2plugin.dic.inspection.YamlClassInspection;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;

import java.io.File;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
* @see fr.adrienbrault.idea.symfony2plugin.dic.inspection.YamlClassInspection
Expand All @@ -20,15 +19,26 @@ public String getTestDataPath() {
}

public void testInspectionForClass() {
assertLocalInspectionContains("services.yml", "services:\n class: Args\\Fo<caret>oBar", "Missing Class");
assertLocalInspectionContains("services.yml", "services:\n class: 'Args\\Fo<caret>oBar'", "Missing Class");
assertLocalInspectionContains("services.yml", "services:\n class: \"Args\\Fo<caret>oBar\"", "Missing Class");
assertLocalInspectionContains("services.yml", "services:\n factory_class: Args\\Fo<caret>oBar", "Missing Class");
assertLocalInspectionNotContains("services.yml", "services:\n factory_class: Args\\Fo<caret>o", "Missing Class");

assertLocalInspectionContains("services.yml", "parameters:\n foo.class: Args\\Fo<caret>oBar", "Missing Class");
assertLocalInspectionContains("services.yml", "parameters:\n foo.class: 'Args\\Fo<caret>oBar'", "Missing Class");
assertLocalInspectionContains("services.yml", "parameters:\n foo.class: \"Args\\Fo<caret>oBar\"", "Missing Class");
assertLocalInspectionNotContains("services.yml", "parameters:\n foo.class: Args\\Fo<caret>o", "Missing Class");
assertLocalInspectionContains("services.yml", "services:\n class: Args\\Fo<caret>oBar", YamlClassInspection.MESSAGE_MISSING_CLASS);
assertLocalInspectionContains("services.yml", "services:\n class: 'Args\\Fo<caret>oBar'", YamlClassInspection.MESSAGE_MISSING_CLASS);
assertLocalInspectionContains("services.yml", "services:\n class: \"Args\\Fo<caret>oBar\"", YamlClassInspection.MESSAGE_MISSING_CLASS);
assertLocalInspectionContains("services.yml", "services:\n factory_class: Args\\Fo<caret>oBar", YamlClassInspection.MESSAGE_MISSING_CLASS);
assertLocalInspectionNotContains("services.yml", "services:\n factory_class: Args\\Fo<caret>o", YamlClassInspection.MESSAGE_MISSING_CLASS);

assertLocalInspectionContains("services.yml", "parameters:\n foo.class: Args\\Fo<caret>oBar", YamlClassInspection.MESSAGE_MISSING_CLASS);
assertLocalInspectionContains("services.yml", "parameters:\n foo.class: 'Args\\Fo<caret>oBar'", YamlClassInspection.MESSAGE_MISSING_CLASS);
assertLocalInspectionContains("services.yml", "parameters:\n foo.class: \"Args\\Fo<caret>oBar\"", YamlClassInspection.MESSAGE_MISSING_CLASS);
assertLocalInspectionNotContains("services.yml", "parameters:\n foo.class: Args\\Fo<caret>o", YamlClassInspection.MESSAGE_MISSING_CLASS);

assertLocalInspectionContains("services.yml", "services:\n class: Args\\Fo<caret>O", YamlClassInspection.MESSAGE_WRONG_CASING);
assertLocalInspectionContains("services.yml", "services:\n class: 'Args\\Fo<caret>O'", YamlClassInspection.MESSAGE_WRONG_CASING);
assertLocalInspectionContains("services.yml", "services:\n class: \"Args\\Fo<caret>O\"", YamlClassInspection.MESSAGE_WRONG_CASING);
assertLocalInspectionContains("services.yml", "services:\n factory_class: Args\\Fo<caret>O", YamlClassInspection.MESSAGE_WRONG_CASING);
assertLocalInspectionNotContains("services.yml", "services:\n factory_class: Args\\Fo<caret>o", YamlClassInspection.MESSAGE_WRONG_CASING);

assertLocalInspectionContains("services.yml", "parameters:\n foo.class: Args\\Fo<caret>O", YamlClassInspection.MESSAGE_WRONG_CASING);
assertLocalInspectionContains("services.yml", "parameters:\n foo.class: 'Args\\Fo<caret>O'", YamlClassInspection.MESSAGE_WRONG_CASING);
assertLocalInspectionContains("services.yml", "parameters:\n foo.class: \"Args\\Fo<caret>O\"", YamlClassInspection.MESSAGE_WRONG_CASING);
assertLocalInspectionNotContains("services.yml", "parameters:\n foo.class: Args\\Fo<caret>o", YamlClassInspection.MESSAGE_WRONG_CASING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
import de.espend.idea.php.annotation.extension.parameter.PhpAnnotationDocTagAnnotatorParameter;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
import fr.adrienbrault.idea.symfony2plugin.twig.annotation.TemplateAnnotationAnnotator;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;

import java.io.File;
import fr.adrienbrault.idea.symfony2plugin.twig.annotation.TemplateAnnotationAnnotator;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
Expand Down