|
| 1 | +package com.cedricziel.idea.typo3.tca.codeInspection; |
| 2 | + |
| 3 | +import com.cedricziel.idea.typo3.psi.PhpElementsUtil; |
| 4 | +import com.cedricziel.idea.typo3.util.TCAUtil; |
| 5 | +import com.intellij.codeInsight.daemon.GroupNames; |
| 6 | +import com.intellij.codeInspection.ProblemsHolder; |
| 7 | +import com.intellij.psi.PsiElementVisitor; |
| 8 | +import com.jetbrains.php.lang.inspections.PhpInspection; |
| 9 | +import com.jetbrains.php.lang.psi.elements.PhpPsiElement; |
| 10 | +import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; |
| 11 | +import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor; |
| 12 | +import org.jetbrains.annotations.Nls; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | + |
| 17 | +import static com.cedricziel.idea.typo3.psi.PhpElementsUtil.extractArrayIndexFromValue; |
| 18 | + |
| 19 | +public class InvalidQuantityInspection extends PhpInspection { |
| 20 | + @Nls |
| 21 | + @NotNull |
| 22 | + @Override |
| 23 | + public String getGroupDisplayName() { |
| 24 | + return GroupNames.BUGS_GROUP_NAME; |
| 25 | + } |
| 26 | + |
| 27 | + @NotNull |
| 28 | + public String getDisplayName() { |
| 29 | + return "Config key only accepts integer values"; |
| 30 | + } |
| 31 | + |
| 32 | + @NotNull |
| 33 | + @Override |
| 34 | + public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) { |
| 35 | + return new PhpElementVisitor() { |
| 36 | + @Override |
| 37 | + public void visitPhpElement(PhpPsiElement element) { |
| 38 | + |
| 39 | + boolean isArrayStringValue = PhpElementsUtil.isStringArrayValue().accepts(element); |
| 40 | + if (!isArrayStringValue) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + String arrayIndex = extractArrayIndexFromValue(element); |
| 45 | + if (arrayIndex != null && Arrays.asList(TCAUtil.TCA_NUMERIC_CONFIG_KEYS).contains(arrayIndex)) { |
| 46 | + if (element instanceof StringLiteralExpression) { |
| 47 | + problemsHolder.registerProblem(element, "Config key only accepts integer values"); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + } |
| 53 | +} |
0 commit comments