Search in sources :

Example 1 with ClassReference

use of com.jetbrains.php.lang.psi.elements.ClassReference in project idea-php-typo3-plugin by cedricziel.

the class ClassNameMatcherInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
    return new PhpElementVisitor() {

        @Override
        public void visitPhpElement(PhpPsiElement element) {
            if (!PlatformPatterns.psiElement(PhpElementTypes.CLASS_REFERENCE).accepts(element)) {
                return;
            }
            Set<String> constants = getDeprecatedClasses(element);
            ClassReference classReference = (ClassReference) element;
            if (constants.contains(classReference.getFQN())) {
                problemsHolder.registerProblem(element, "Class removed with TYPO3 9, consider using an alternative");
            }
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ClassReference

use of com.jetbrains.php.lang.psi.elements.ClassReference in project phpinspectionsea by kalessil.

the class ThrowRawExceptionInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpThrowExpression(@NotNull PhpThrowExpression expression) {
            final PsiElement argument = expression.getArgument();
            if (argument instanceof NewExpression) {
                final NewExpression newExpression = (NewExpression) argument;
                final ClassReference classReference = newExpression.getClassReference();
                final String classFqn = classReference == null ? null : classReference.getFQN();
                if (classFqn != null) {
                    if (classFqn.equals("\\Exception")) {
                        holder.registerProblem(classReference, MessagesPresentationUtil.prefixWithEa(messageRawException), new TheLocalFix());
                    } else if (REPORT_MISSING_ARGUMENTS && newExpression.getParameters().length == 0) {
                        final PsiElement resolved = OpenapiResolveUtil.resolveReference(classReference);
                        if (resolved instanceof PhpClass && this.isTarget((PhpClass) resolved)) {
                            holder.registerProblem(newExpression, MessagesPresentationUtil.prefixWithEa(messageNoArguments));
                        }
                    }
                }
            }
        }

        private boolean isTarget(@NotNull PhpClass clazz) {
            final Method constructor = clazz.getConstructor();
            return constructor != null && constructor.getParameters().length == 3 && clazz.findOwnFieldByName("message", false) == null;
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) NewExpression(com.jetbrains.php.lang.psi.elements.NewExpression) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PhpThrowExpression(com.kalessil.phpStorm.phpInspectionsEA.openApi.elements.PhpThrowExpression) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) Method(com.jetbrains.php.lang.psi.elements.Method) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ClassReference

use of com.jetbrains.php.lang.psi.elements.ClassReference in project phpinspectionsea by kalessil.

the class DateIntervalSpecificationInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpNewExpression(@NotNull NewExpression expression) {
            /* before inspecting check parameters amount */
            final PsiElement[] params = expression.getParameters();
            if (params.length == 1) {
                final ClassReference classReference = expression.getClassReference();
                final String classFQN = classReference == null ? null : classReference.getFQN();
                if (classFQN == null || !classFQN.equals("\\DateInterval")) {
                    /* TODO: child classes support */
                    return;
                }
                /* now try getting string literal and test against valid patterns */
                final StringLiteralExpression pattern = ExpressionSemanticUtil.resolveAsStringLiteral(params[0]);
                if (pattern != null && pattern.getFirstPsiChild() == null) {
                    final String input = pattern.getContents();
                    if (!regexRegular.matcher(input).find() && !regexDateTimeAlike.matcher(input).find()) {
                        holder.registerProblem(pattern, MessagesPresentationUtil.prefixWithEa(message));
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) NewExpression(com.jetbrains.php.lang.psi.elements.NewExpression) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ClassReference

use of com.jetbrains.php.lang.psi.elements.ClassReference in project phpinspectionsea by kalessil.

the class EfferentObjectCouplingInspector method buildVisitor.

@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpClass(@NotNull PhpClass clazz) {
            final PsiElement nameIdentifier = NamedElementUtil.getNameIdentifier(clazz);
            if (nameIdentifier != null) {
                final Set<String> references = PsiTreeUtil.findChildrenOfType(clazz, ClassReference.class).stream().map(ClassReference::getFQN).collect(Collectors.toSet());
                final int count = references.size();
                if (count >= optionCouplingLimit) {
                    holder.registerProblem(nameIdentifier, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), count));
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ClassReference

use of com.jetbrains.php.lang.psi.elements.ClassReference in project phpinspectionsea by kalessil.

the class IsCountableCanBeUsedInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            final String functionName = reference.getName();
            if (functionName != null && functionName.equals("is_array")) {
                final boolean isTargetVersion = PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP740);
                if (isTargetVersion) {
                    final PsiElement[] arguments = reference.getParameters();
                    final PsiElement parent = reference.getParent();
                    if (parent instanceof BinaryExpression && arguments.length == 1) {
                        final BinaryExpression binary = (BinaryExpression) parent;
                        final IElementType operation = binary.getOperationType();
                        if (operation == PhpTokenTypes.opOR) {
                            /* find the high-level binary expression */
                            BinaryExpression context = binary;
                            while (context instanceof BinaryExpression) {
                                PsiElement up = context.getParent();
                                while (up instanceof ParenthesizedExpression) {
                                    up = up.getParent();
                                }
                                if (up instanceof BinaryExpression && ((BinaryExpression) up).getOperationType() == PhpTokenTypes.opOR) {
                                    context = (BinaryExpression) up;
                                } else {
                                    break;
                                }
                            }
                            /* check the pattern */
                            final List<PsiElement> fragments = this.extract(context, PhpTokenTypes.opOR);
                            if (!fragments.isEmpty()) {
                                if (fragments.size() > 1) {
                                    for (final PsiElement fragment : fragments) {
                                        if (fragment != reference && fragment instanceof BinaryExpression) {
                                            final BinaryExpression candidate = (BinaryExpression) fragment;
                                            if (candidate.getOperationType() == PhpTokenTypes.kwINSTANCEOF) {
                                                final PsiElement clazz = candidate.getRightOperand();
                                                if (clazz instanceof ClassReference && "Countable".equals(((ClassReference) clazz).getName())) {
                                                    final PsiElement subject = candidate.getLeftOperand();
                                                    if (subject != null && OpenapiEquivalenceUtil.areEqual(subject, arguments[0])) {
                                                        final String argument = subject.getText();
                                                        holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(message), argument, argument, argument));
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                fragments.clear();
                            }
                        }
                    }
                }
            }
        }

        @NotNull
        private List<PsiElement> extract(@NotNull BinaryExpression binary, @Nullable IElementType operator) {
            final List<PsiElement> result = new ArrayList<>();
            if (binary.getOperationType() == operator) {
                Stream.of(binary.getLeftOperand(), binary.getRightOperand()).filter(Objects::nonNull).map(ExpressionSemanticUtil::getExpressionTroughParenthesis).forEach(expression -> {
                    if (expression instanceof BinaryExpression) {
                        result.addAll(this.extract((BinaryExpression) expression, operator));
                    } else {
                        result.add(expression);
                    }
                });
            } else {
                result.add(binary);
            }
            return result;
        }
    };
}
Also used : ParenthesizedExpression(com.jetbrains.php.lang.psi.elements.ParenthesizedExpression) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull) IElementType(com.intellij.psi.tree.IElementType) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) BinaryExpression(com.jetbrains.php.lang.psi.elements.BinaryExpression) Objects(java.util.Objects) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ClassReference (com.jetbrains.php.lang.psi.elements.ClassReference)10 PsiElement (com.intellij.psi.PsiElement)8 NotNull (org.jetbrains.annotations.NotNull)8 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)7 PhpClass (com.jetbrains.php.lang.psi.elements.PhpClass)4 IElementType (com.intellij.psi.tree.IElementType)2 BinaryExpression (com.jetbrains.php.lang.psi.elements.BinaryExpression)2 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)2 NewExpression (com.jetbrains.php.lang.psi.elements.NewExpression)2 ParenthesizedExpression (com.jetbrains.php.lang.psi.elements.ParenthesizedExpression)2 ArrayList (java.util.ArrayList)2 Objects (java.util.Objects)2 Nullable (org.jetbrains.annotations.Nullable)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ClassConstantReference (com.jetbrains.php.lang.psi.elements.ClassConstantReference)1 Function (com.jetbrains.php.lang.psi.elements.Function)1 Method (com.jetbrains.php.lang.psi.elements.Method)1 MethodReference (com.jetbrains.php.lang.psi.elements.MethodReference)1 PhpPsiElement (com.jetbrains.php.lang.psi.elements.PhpPsiElement)1 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)1