Search in sources :

Example 1 with PhpReturnInstruction

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

the class MultipleReturnStatementsInspector method buildVisitor.

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

        @Override
        public void visitPhpMethod(@NotNull Method method) {
            final PhpClass clazz = method.getContainingClass();
            final PsiElement nameIdentifier = NamedElementUtil.getNameIdentifier(method);
            if (nameIdentifier != null && clazz != null && !clazz.isTrait()) {
                final PhpExitPointInstruction exitPoint = method.getControlFlow().getExitPoint();
                int returnsCount = 0;
                for (final PhpInstruction instruction : OpenapiElementsUtil.getPredecessors(exitPoint)) {
                    if (instruction instanceof PhpReturnInstruction) {
                        ++returnsCount;
                    }
                }
                if (returnsCount > 1) {
                    final ProblemHighlightType level = returnsCount > 3 ? ProblemHighlightType.GENERIC_ERROR : ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
                    final String message = String.format(messagePattern, returnsCount);
                    holder.registerProblem(nameIdentifier, message, level);
                }
            }
        }
    };
}
Also used : PhpInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpInstruction) PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) Method(com.jetbrains.php.lang.psi.elements.Method) PhpReturnInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpReturnInstruction) PhpExitPointInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpExitPointInstruction) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)1 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 PhpClass (com.jetbrains.php.lang.psi.elements.PhpClass)1 PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)1 NotNull (org.jetbrains.annotations.NotNull)1