-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathPhpAssetMissingInspection.java
More file actions
66 lines (58 loc) · 3.05 KB
/
PhpAssetMissingInspection.java
File metadata and controls
66 lines (58 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package fr.adrienbrault.idea.symfony2plugin.templating.inspection;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
/**
* PHP version of "Twig" "asset" function
*
* "Package*::getUrl"
* "Package*::getVersion"
*
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class PhpAssetMissingInspection extends LocalInspectionTool {
@NotNull
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
if(!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}
return new AssetPackageElementVisitor(holder);
}
private static class AssetPackageElementVisitor extends PsiElementVisitor {
private final ProblemsHolder holder;
AssetPackageElementVisitor(ProblemsHolder holder) {
this.holder = holder;
}
@Override
public void visitElement(@NotNull PsiElement element) {
if (element instanceof StringLiteralExpression) {
MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter((StringLiteralExpression) element);
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")
)) {
invoke((StringLiteralExpression) element, holder);
}
}
super.visitElement(element);
}
private void invoke(@NotNull StringLiteralExpression element, @NotNull ProblemsHolder holder) {
String asset = element.getContents();
if(StringUtils.isBlank(asset) || !TwigUtil.resolveAssetsFiles(element.getProject(), asset).isEmpty()) {
return;
}
holder.registerProblem(element, "Symfony: Missing asset");
}
}
}