Search in sources :

Example 1 with GroupStatement

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

the class BadExceptionsProcessingInspector method buildVisitor.

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

        @Override
        public void visitPhpTry(@NotNull Try tryStatement) {
            final GroupStatement body = ExpressionSemanticUtil.getGroupStatement(tryStatement);
            final int expressionsCount = body == null ? 0 : ExpressionSemanticUtil.countExpressionsInGroup(body);
            if (expressionsCount > 3) {
                holder.registerProblem(tryStatement.getFirstChild(), MessagesPresentationUtil.prefixWithEa(messagePattern.replace("%c%", String.valueOf(expressionsCount))));
            }
        }

        @Override
        public void visitPhpCatch(@NotNull Catch catchStatement) {
            final Variable variable = catchStatement.getException();
            final GroupStatement body = ExpressionSemanticUtil.getGroupStatement(catchStatement);
            if (variable != null && body != null) {
                final String variableName = variable.getName();
                if (!StringUtils.isEmpty(variableName)) {
                    /* incomplete catch statement */
                    boolean isVariableUsed = false;
                    for (final Variable usedVariable : PsiTreeUtil.findChildrenOfType(body, Variable.class)) {
                        if (usedVariable.getName().equals(variableName)) {
                            isVariableUsed = true;
                            break;
                        }
                    }
                    if (!isVariableUsed) {
                        if (ExpressionSemanticUtil.countExpressionsInGroup(body) == 0) {
                            holder.registerProblem(variable, MessagesPresentationUtil.prefixWithEa(messageFailSilently));
                        } else {
                            holder.registerProblem(variable, MessagesPresentationUtil.prefixWithEa(messageChainedException));
                        }
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) GroupStatement(com.jetbrains.php.lang.psi.elements.GroupStatement) Variable(com.jetbrains.php.lang.psi.elements.Variable) Catch(com.jetbrains.php.lang.psi.elements.Catch) Try(com.jetbrains.php.lang.psi.elements.Try) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GroupStatement

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

the class UnnecessaryElseFixer method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PsiElement expression = null == element ? null : element.getParent();
    final PsiElement newline = PhpPsiElementFactory.createFromText(project, PsiWhiteSpace.class, "\n");
    if (null == expression || null == newline || project.isDisposed()) {
        return;
    }
    if (expression instanceof Else) {
        final Else elseStatement = (Else) expression;
        final If parentIfExpression = (If) expression.getParent();
        final PsiElement parentIfHolder = parentIfExpression.getParent();
        if (elseStatement.getFirstPsiChild() instanceof If) {
            /* handle 'else if' */
            final If nestedIfCopy = (If) elseStatement.getFirstPsiChild().copy();
            elseStatement.delete();
            parentIfHolder.addAfter(nestedIfCopy, parentIfExpression);
            parentIfHolder.addAfter(newline, parentIfExpression);
            return;
        }
        if (elseStatement.getFirstPsiChild() instanceof GroupStatement) {
            /* handle 'else {} ' */
            final GroupStatement elseBodyCopy = (GroupStatement) elseStatement.getFirstPsiChild().copy();
            elseStatement.delete();
            final PsiElement startBracket = elseBodyCopy.getFirstChild();
            final PsiElement endBracket = elseBodyCopy.getLastChild();
            PsiElement last = endBracket.getPrevSibling();
            if (last instanceof PsiWhiteSpace) {
                last = last.getPrevSibling();
            }
            while (null != last) {
                if (last != endBracket && last != startBracket) {
                    parentIfHolder.addAfter(last, parentIfExpression);
                }
                last = last.getPrevSibling();
            }
            /* trailing spacing should not be influence by else body */
            final PsiElement trailingSpaceCandidate = parentIfExpression.getNextSibling();
            if (trailingSpaceCandidate instanceof PsiWhiteSpace) {
                trailingSpaceCandidate.replace(newline);
            } else {
                parentIfHolder.addAfter(newline, parentIfExpression);
            }
            return;
        }
    }
    if (expression instanceof ElseIf) {
        /* handle 'elseif': messed up to not loose cursor after fixing... */
        final ElseIf elseIfStatement = (ElseIf) expression;
        final If parentIfExpression = (If) expression.getParent();
        /* back up original if */
        final If newIf = PhpPsiElementFactory.createPhpPsiFromText(project, If.class, "if (true) {\n}");
        newIf.getCondition().replace(parentIfExpression.getCondition());
        ExpressionSemanticUtil.getGroupStatement(newIf).replace(ExpressionSemanticUtil.getGroupStatement(parentIfExpression));
        /* drop the elseif, backup resulted construct */
        parentIfExpression.getCondition().replace(elseIfStatement.getCondition().copy());
        ExpressionSemanticUtil.getGroupStatement(parentIfExpression).replace(ExpressionSemanticUtil.getGroupStatement(elseIfStatement).copy());
        elseIfStatement.delete();
        final PsiElement followUpIf = parentIfExpression.copy();
        /* insert following up if, which was backed up */
        final PsiElement parentIfHolder = parentIfExpression.getParent();
        parentIfHolder.addAfter(followUpIf, parentIfExpression);
        parentIfHolder.addAfter(newline, parentIfExpression);
        /* actualize if-statement with clean one */
        parentIfHolder.addBefore(newIf, parentIfExpression);
        parentIfExpression.delete();
    }
}
Also used : ElseIf(com.jetbrains.php.lang.psi.elements.ElseIf) GroupStatement(com.jetbrains.php.lang.psi.elements.GroupStatement) Else(com.jetbrains.php.lang.psi.elements.Else) If(com.jetbrains.php.lang.psi.elements.If) ElseIf(com.jetbrains.php.lang.psi.elements.ElseIf) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 3 with GroupStatement

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

the class EncryptionInitializationVectorRandomnessInspector method buildVisitor.

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

        @Override
        public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
            final String functionName = reference.getName();
            /* variable functions are not supported, as we are checking 2 different extensions functions */
            if (functionName != null && (functionName.equals("openssl_encrypt") || functionName.equals("mcrypt_encrypt"))) {
                final PsiElement[] arguments = reference.getParameters();
                if (arguments.length != 5 || arguments[4] == null || arguments[4].getText().isEmpty()) {
                    return;
                }
                /* discover and inspect possible values */
                final Set<PsiElement> values = PossibleValuesDiscoveryUtil.discover(arguments[4]);
                if (!values.isEmpty()) {
                    /* check all possible values */
                    final List<String> reporting = new ArrayList<>();
                    for (final PsiElement source : values) {
                        if (OpenapiTypesUtil.isFunctionReference(source)) {
                            final String sourceName = ((FunctionReference) source).getName();
                            if (sourceName != null && secureFunctions.contains(sourceName)) {
                                continue;
                            }
                        }
                        reporting.add(source.getText());
                    }
                    if (!reporting.isEmpty() && !this.isAggregatedGeneration(values)) {
                        /* sort reporting list to produce testable results */
                        Collections.sort(reporting);
                        final String ivFunction = functionName.startsWith("openssl_") ? "openssl_random_pseudo_bytes" : "mcrypt_create_iv";
                        holder.registerProblem(arguments[4], MessagesPresentationUtil.prefixWithEa(String.format(messagePattern, ivFunction, String.join(", ", reporting))), ProblemHighlightType.GENERIC_ERROR);
                    }
                    reporting.clear();
                }
                values.clear();
            }
        }

        private boolean isAggregatedGeneration(@NotNull Set<PsiElement> candidates) {
            if (candidates.size() == 1) {
                final PsiElement candidate = candidates.iterator().next();
                if (candidate instanceof FunctionReference) {
                    final PsiElement resolved = OpenapiResolveUtil.resolveReference((PsiReference) candidate);
                    if (resolved instanceof Function) {
                        final GroupStatement body = ExpressionSemanticUtil.getGroupStatement(resolved);
                        if (body != null) {
                            return PsiTreeUtil.findChildrenOfType(body, FunctionReference.class).stream().anyMatch(c -> secureFunctions.contains(c.getName()));
                        }
                    }
                }
            }
            return false;
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Function(com.jetbrains.php.lang.psi.elements.Function) GroupStatement(com.jetbrains.php.lang.psi.elements.GroupStatement) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GroupStatement

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

the class ExpressionSemanticUtilTest method testGetLastStatement.

public void testGetLastStatement() {
    Project project = myFixture.getProject();
    GroupStatement statement;
    String blockWithCommentsOnly = "{ /** dock-block*/ /* comment*/ /** doc-block*/ }";
    statement = PhpPsiElementFactory.createFromText(project, GroupStatement.class, blockWithCommentsOnly);
    assertNotNull(statement);
    assertNull(ExpressionSemanticUtil.getLastStatement(statement));
    String blockWith2Statements = "{ /** dock-block*/ echo 1; return 2; /** doc-block*/ }";
    statement = PhpPsiElementFactory.createFromText(project, GroupStatement.class, blockWith2Statements);
    assertNotNull(statement);
    PsiElement lastStatement = ExpressionSemanticUtil.getLastStatement(statement);
    assertNotNull(lastStatement);
    assertInstanceOf(lastStatement, PhpReturn.class);
}
Also used : Project(com.intellij.openapi.project.Project) GroupStatement(com.jetbrains.php.lang.psi.elements.GroupStatement) PsiElement(com.intellij.psi.PsiElement)

Example 5 with GroupStatement

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

the class ExpressionSemanticUtilTest method testCountExpressionsInGroup.

public void testCountExpressionsInGroup() {
    Project project = myFixture.getProject();
    GroupStatement statement;
    String blockWithCommentsOnly = "{ /** dock-block*/ /* comment*/ /** doc-block*/ }";
    statement = PhpPsiElementFactory.createFromText(project, GroupStatement.class, blockWithCommentsOnly);
    assertNotNull(statement);
    assertEquals(0, ExpressionSemanticUtil.countExpressionsInGroup(statement));
    String blockWith2Statements = "{ /** dock-block*/ echo 1; return 2; /** doc-block*/ }";
    statement = PhpPsiElementFactory.createFromText(project, GroupStatement.class, blockWith2Statements);
    assertNotNull(statement);
    assertEquals(2, ExpressionSemanticUtil.countExpressionsInGroup(statement));
}
Also used : Project(com.intellij.openapi.project.Project) GroupStatement(com.jetbrains.php.lang.psi.elements.GroupStatement)

Aggregations

GroupStatement (com.jetbrains.php.lang.psi.elements.GroupStatement)7 PsiElement (com.intellij.psi.PsiElement)4 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)4 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)2 Function (com.jetbrains.php.lang.psi.elements.Function)2 Variable (com.jetbrains.php.lang.psi.elements.Variable)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 PhpLanguageLevel (com.jetbrains.php.config.PhpLanguageLevel)1 Catch (com.jetbrains.php.lang.psi.elements.Catch)1 Else (com.jetbrains.php.lang.psi.elements.Else)1 ElseIf (com.jetbrains.php.lang.psi.elements.ElseIf)1 ForeachStatement (com.jetbrains.php.lang.psi.elements.ForeachStatement)1 FunctionReference (com.jetbrains.php.lang.psi.elements.FunctionReference)1 If (com.jetbrains.php.lang.psi.elements.If)1 MultiassignmentExpression (com.jetbrains.php.lang.psi.elements.MultiassignmentExpression)1 PhpGoto (com.jetbrains.php.lang.psi.elements.PhpGoto)1 PhpGotoLabel (com.jetbrains.php.lang.psi.elements.PhpGotoLabel)1 Try (com.jetbrains.php.lang.psi.elements.Try)1