|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.dic.inspection; |
| 2 | + |
| 3 | +import com.intellij.codeInspection.LocalInspectionTool; |
| 4 | +import com.intellij.codeInspection.ProblemHighlightType; |
| 5 | +import com.intellij.codeInspection.ProblemsHolder; |
| 6 | +import com.intellij.ide.highlighter.XmlFileType; |
| 7 | +import com.intellij.psi.PsiElement; |
| 8 | +import com.intellij.psi.PsiElementVisitor; |
| 9 | +import com.intellij.psi.PsiFile; |
| 10 | +import com.intellij.psi.PsiRecursiveElementVisitor; |
| 11 | +import com.intellij.psi.xml.XmlAttributeValue; |
| 12 | +import com.jetbrains.php.lang.PhpFileType; |
| 13 | +import com.jetbrains.php.lang.psi.elements.MethodReference; |
| 14 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2InterfacesUtil; |
| 15 | +import fr.adrienbrault.idea.symfony2plugin.config.xml.XmlHelper; |
| 16 | +import fr.adrienbrault.idea.symfony2plugin.config.yaml.YamlElementPatternHelper; |
| 17 | +import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils; |
| 18 | +import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper; |
| 19 | +import org.apache.commons.lang.StringUtils; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | +import org.jetbrains.yaml.YAMLFileType; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 25 | + */ |
| 26 | +public class CaseSensitivityServiceInspection extends LocalInspectionTool { |
| 27 | + |
| 28 | + public static final String SYMFONY_LOWERCASE_LETTERS_FOR_SERVICE = "Symfony: lowercase letters for service"; |
| 29 | + |
| 30 | + @NotNull |
| 31 | + public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 32 | + |
| 33 | + PsiFile psiFile = holder.getFile(); |
| 34 | + if(psiFile.getFileType() == PhpFileType.INSTANCE) { |
| 35 | + phpVisitor(holder); |
| 36 | + } else if(psiFile.getFileType() == YAMLFileType.YML) { |
| 37 | + yamlVisitor(holder); |
| 38 | + } else if(psiFile.getFileType() == XmlFileType.INSTANCE) { |
| 39 | + xmlVisitor(holder); |
| 40 | + } |
| 41 | + |
| 42 | + return super.buildVisitor(holder, isOnTheFly); |
| 43 | + } |
| 44 | + |
| 45 | + private void yamlVisitor(final @NotNull ProblemsHolder holder) { |
| 46 | + |
| 47 | + holder.getFile().acceptChildren(new PsiRecursiveElementVisitor() { |
| 48 | + @Override |
| 49 | + public void visitElement(PsiElement psiElement) { |
| 50 | + |
| 51 | + // @TODO: support key itself |
| 52 | + if (YamlElementPatternHelper.getServiceDefinition().accepts(psiElement) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(psiElement)) { |
| 53 | + String serviceName = YamlHelper.trimSpecialSyntaxServiceName(psiElement.getText()); |
| 54 | + // dont mark "@", "@?", "@@" escaping and expressions |
| 55 | + if (serviceName.length() > 2 && !serviceName.startsWith("=") && !serviceName.startsWith("@") && !serviceName.equals(serviceName.toLowerCase())) { |
| 56 | + holder.registerProblem(psiElement, SYMFONY_LOWERCASE_LETTERS_FOR_SERVICE, ProblemHighlightType.WEAK_WARNING); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + super.visitElement(psiElement); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + private void phpVisitor(final @NotNull ProblemsHolder holder) { |
| 66 | + |
| 67 | + holder.getFile().acceptChildren(new PsiRecursiveElementVisitor() { |
| 68 | + @Override |
| 69 | + public void visitElement(PsiElement element) { |
| 70 | + MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter(element); |
| 71 | + if (methodReference != null && new Symfony2InterfacesUtil().isContainerGetCall(methodReference)) { |
| 72 | + String serviceName = Symfony2InterfacesUtil.getFirstArgumentStringValue(methodReference); |
| 73 | + if(serviceName != null && !serviceName.equals(serviceName.toLowerCase())) { |
| 74 | + holder.registerProblem(element, SYMFONY_LOWERCASE_LETTERS_FOR_SERVICE, ProblemHighlightType.WEAK_WARNING); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + super.visitElement(element); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + private void xmlVisitor(final @NotNull ProblemsHolder holder) { |
| 84 | + holder.getFile().acceptChildren(new PsiRecursiveElementVisitor() { |
| 85 | + @Override |
| 86 | + public void visitElement(PsiElement psiElement) { |
| 87 | + if(psiElement instanceof XmlAttributeValue && (XmlHelper.getArgumentServiceIdPattern().accepts(psiElement) || XmlHelper.getServiceIdNamePattern().accepts(psiElement))) { |
| 88 | + String serviceName = ((XmlAttributeValue) psiElement).getValue(); |
| 89 | + if(StringUtils.isNotBlank(serviceName) && !serviceName.equals(serviceName.toLowerCase())) { |
| 90 | + holder.registerProblem(psiElement, SYMFONY_LOWERCASE_LETTERS_FOR_SERVICE, ProblemHighlightType.WEAK_WARNING); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + super.visitElement(psiElement); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments