|
| 1 | +package com.cedricziel.idea.typo3.provider; |
| 2 | + |
| 3 | +import com.cedricziel.idea.typo3.psi.PhpElementsUtil; |
| 4 | +import com.intellij.openapi.project.DumbService; |
| 5 | +import com.intellij.openapi.project.Project; |
| 6 | +import com.intellij.psi.PsiElement; |
| 7 | +import com.jetbrains.php.PhpIndex; |
| 8 | +import com.jetbrains.php.lang.psi.elements.MethodReference; |
| 9 | +import com.jetbrains.php.lang.psi.elements.PhpNamedElement; |
| 10 | +import com.jetbrains.php.lang.psi.elements.PhpReference; |
| 11 | +import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider2; |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | + |
| 14 | +import java.util.Collection; |
| 15 | +import java.util.Collections; |
| 16 | + |
| 17 | +/** |
| 18 | + * TypeProvider for `GeneralUtility::makeInstance` |
| 19 | + */ |
| 20 | +public class GeneralUtilityTypeProvider implements PhpTypeProvider2 { |
| 21 | + |
| 22 | + @Override |
| 23 | + public char getKey() { |
| 24 | + return '\u0205'; |
| 25 | + } |
| 26 | + |
| 27 | + @Nullable |
| 28 | + @Override |
| 29 | + public String getType(PsiElement psiElement) { |
| 30 | + if (DumbService.getInstance(psiElement.getProject()).isDumb()) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + if (!(psiElement instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "makeInstance")) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + MethodReference methodReference = (MethodReference) psiElement; |
| 39 | + if (methodReference.getParameters().length == 0) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + PsiElement firstParam = methodReference.getParameters()[0]; |
| 44 | + |
| 45 | + if (firstParam instanceof PhpReference) { |
| 46 | + PhpReference ref = (PhpReference) firstParam; |
| 47 | + if (ref.getText().toLowerCase().contains("::class")) { |
| 48 | + return methodReference.getSignature() + "%" + ref.getSignature(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Collection<? extends PhpNamedElement> getBySignature(String s, Project project) { |
| 57 | + int endIndex = s.lastIndexOf("%"); |
| 58 | + if (endIndex == -1) { |
| 59 | + return Collections.emptySet(); |
| 60 | + } |
| 61 | + |
| 62 | + // Get FQN from parameter string. |
| 63 | + // Example (PhpStorm 8): #K#C\Foo\Bar::get()%#K#C\Bar\Baz. -> \Bar\Baz. |
| 64 | + // Example (PhpStorm 9): #K#C\Foo\Bar::get()%#K#C\Bar\Baz.class -> \Bar\Baz.class |
| 65 | + String parameter = s.substring(endIndex + 5, s.length()); |
| 66 | + |
| 67 | + if (parameter.contains(".class")) { // for PhpStorm 9 |
| 68 | + parameter = parameter.replace(".class", ""); |
| 69 | + } |
| 70 | + |
| 71 | + if (parameter.contains(".")) { |
| 72 | + parameter = parameter.replace(".", ""); |
| 73 | + } |
| 74 | + |
| 75 | + return PhpIndex.getInstance(project).getAnyByFQN(parameter); |
| 76 | + } |
| 77 | +} |
0 commit comments