Search in sources :

Example 1 with PhpUnset

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

the class UselessUnsetInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    /* foreach is also a case, but there is no way to get flow entry point in actual JB platform API */
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpMethod(@NotNull Method method) {
            this.inspectUsages(method.getParameters(), method);
        }

        @Override
        public void visitPhpFunction(@NotNull Function function) {
            this.inspectUsages(function.getParameters(), function);
        }

        private void inspectUsages(@NotNull Parameter[] parameters, @NotNull PhpScopeHolder scope) {
            if (parameters.length > 0) {
                final PhpEntryPointInstruction entry = scope.getControlFlow().getEntryPoint();
                for (final Parameter parameter : parameters) {
                    final String parameterName = parameter.getName();
                    if (!parameterName.isEmpty()) {
                        for (final PhpAccessVariableInstruction usage : OpenapiControlFlowUtil.getFollowingVariableAccessInstructions(entry, parameterName)) {
                            final PsiElement expression = usage.getAnchor();
                            final PsiElement parent = expression.getParent();
                            if (parent instanceof PhpUnset) {
                                int unsetParametersCount = ((PhpUnset) parent).getArguments().length;
                                final PsiElement target = (unsetParametersCount == 1 ? parent : expression);
                                holder.registerProblem(target, MessagesPresentationUtil.prefixWithEa(message), ProblemHighlightType.LIKE_UNUSED_SYMBOL);
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Function(com.jetbrains.php.lang.psi.elements.Function) PhpAccessVariableInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpAccessVariableInstruction) PhpEntryPointInstruction(com.jetbrains.php.codeInsight.controlFlow.instructions.PhpEntryPointInstruction) PhpScopeHolder(com.jetbrains.php.codeInsight.PhpScopeHolder) PhpUnset(com.jetbrains.php.lang.psi.elements.PhpUnset) Parameter(com.jetbrains.php.lang.psi.elements.Parameter) Method(com.jetbrains.php.lang.psi.elements.Method) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiElement (com.intellij.psi.PsiElement)1 PhpScopeHolder (com.jetbrains.php.codeInsight.PhpScopeHolder)1 PhpAccessVariableInstruction (com.jetbrains.php.codeInsight.controlFlow.instructions.PhpAccessVariableInstruction)1 PhpEntryPointInstruction (com.jetbrains.php.codeInsight.controlFlow.instructions.PhpEntryPointInstruction)1 Function (com.jetbrains.php.lang.psi.elements.Function)1 Method (com.jetbrains.php.lang.psi.elements.Method)1 Parameter (com.jetbrains.php.lang.psi.elements.Parameter)1 PhpUnset (com.jetbrains.php.lang.psi.elements.PhpUnset)1 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)1 NotNull (org.jetbrains.annotations.NotNull)1