Search in sources :

Example 1 with NewExpression

use of com.jetbrains.php.lang.psi.elements.NewExpression 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 2 with NewExpression

use of com.jetbrains.php.lang.psi.elements.NewExpression 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)

Aggregations

PsiElement (com.intellij.psi.PsiElement)2 ClassReference (com.jetbrains.php.lang.psi.elements.ClassReference)2 NewExpression (com.jetbrains.php.lang.psi.elements.NewExpression)2 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)2 NotNull (org.jetbrains.annotations.NotNull)2 Method (com.jetbrains.php.lang.psi.elements.Method)1 PhpClass (com.jetbrains.php.lang.psi.elements.PhpClass)1 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)1 PhpThrowExpression (com.kalessil.phpStorm.phpInspectionsEA.openApi.elements.PhpThrowExpression)1