Search in sources :

Example 1 with PhpInstruction

use of com.jetbrains.php.codeInsight.controlFlow.instructions.PhpInstruction in project phpinspectionsea by kalessil.

the class MultipleReturnStatementsInspector method buildVisitor.

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

        @Override
        public void visitPhpMethod(@NotNull Method method) {
            final PsiElement nameIdentifier = NamedElementUtil.getNameIdentifier(method);
            if (nameIdentifier != null && !method.isAbstract()) {
                final PhpExitPointInstruction exitPoint = method.getControlFlow().getExitPoint();
                int returnsCount = 0;
                for (final PhpInstruction instruction : OpenapiElementsUtil.getPredecessors(exitPoint)) {
                    if (instruction instanceof PhpReturnInstruction) {
                        ++returnsCount;
                    }
                }
                if (returnsCount >= SCREAM_THRESHOLD) {
                    holder.registerProblem(nameIdentifier, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), returnsCount), ProblemHighlightType.GENERIC_ERROR);
                } else if (returnsCount >= COMPLAIN_THRESHOLD) {
                    holder.registerProblem(nameIdentifier, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), returnsCount), ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) PhpInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpInstruction) Method(com.jetbrains.php.lang.psi.elements.Method) PhpReturnInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpReturnInstruction) PhpExitPointInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpExitPointInstruction) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 PhpExitPointInstruction (com.jetbrains.php.codeInsight.controlFlow.instructions.PhpExitPointInstruction)1 PhpInstruction (com.jetbrains.php.codeInsight.controlFlow.instructions.PhpInstruction)1 PhpReturnInstruction (com.jetbrains.php.codeInsight.controlFlow.instructions.PhpReturnInstruction)1 Method (com.jetbrains.php.lang.psi.elements.Method)1 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)1 NotNull (org.jetbrains.annotations.NotNull)1