Search in sources :

Example 1 with Parameter

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

the class CanNotTakeArgumentsByReferenceStrategy method apply.

public static void apply(final Method method, final ProblemsHolder holder) {
    for (Parameter parameter : method.getParameters()) {
        if (parameter.isPassByRef() && null != method.getNameIdentifier()) {
            final String strMessage = strProblemDescription.replace("%m%", method.getName());
            holder.registerProblem(method.getNameIdentifier(), strMessage, ProblemHighlightType.ERROR);
            return;
        }
    }
}
Also used : Parameter(com.jetbrains.php.lang.psi.elements.Parameter)

Example 2 with Parameter

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

the class ParameterDefaultValueIsNotNullInspector method buildVisitor.

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

        @Override
        public void visitPhpMethod(@NotNull Method method) {
            this.analyze(method);
        }

        @Override
        public void visitPhpFunction(@NotNull Function function) {
            this.analyze(function);
        }

        private void analyze(@NotNull Function function) {
            final Parameter[] arguments = function.getParameters();
            if (arguments.length > 0) {
                /* collect violations */
                final List<Parameter> violations = new ArrayList<>();
                for (final Parameter argument : arguments) {
                    final PsiElement defaultValue = argument.getDefaultValue();
                    if (defaultValue != null && !PhpLanguageUtil.isNull(defaultValue)) {
                        /* false-positives: null can not be used due to implicit type hints */
                        final PhpType declared = OpenapiResolveUtil.resolveDeclaredType(argument);
                        if (declared.isEmpty() || declared.getTypes().stream().anyMatch(t -> Types.getType(t).equals(Types.strNull))) {
                            violations.add(argument);
                        }
                    }
                }
                if (!violations.isEmpty()) {
                    /* false-positives: methods overrides, so violation should be addressed in the parent */
                    if (function instanceof Method) {
                        final PhpClass clazz = ((Method) function).getContainingClass();
                        if (clazz != null) {
                            final PhpClass parent = OpenapiResolveUtil.resolveSuperClass(clazz);
                            if (parent != null) {
                                final Method parentMethod = OpenapiResolveUtil.resolveMethod(parent, function.getName());
                                if (parentMethod != null && !parentMethod.getAccess().isPrivate()) {
                                    violations.clear();
                                    return;
                                }
                            }
                        }
                    }
                    /* report violations */
                    violations.forEach(param -> holder.registerProblem(param, MessagesPresentationUtil.prefixWithEa(message)));
                    violations.clear();
                }
            }
        }
    };
}
Also used : PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) BasePhpInspection(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection) Types(com.kalessil.phpStorm.phpInspectionsEA.utils.Types) PhpLanguageUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.PhpLanguageUtil) Function(com.jetbrains.php.lang.psi.elements.Function) Method(com.jetbrains.php.lang.psi.elements.Method) ArrayList(java.util.ArrayList) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) List(java.util.List) OpenapiResolveUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiResolveUtil) MessagesPresentationUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil) PsiElement(com.intellij.psi.PsiElement) Parameter(com.jetbrains.php.lang.psi.elements.Parameter) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) NotNull(org.jetbrains.annotations.NotNull) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Function(com.jetbrains.php.lang.psi.elements.Function) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) ArrayList(java.util.ArrayList) 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) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with Parameter

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

Parameter (com.jetbrains.php.lang.psi.elements.Parameter)3 PsiElement (com.intellij.psi.PsiElement)2 Function (com.jetbrains.php.lang.psi.elements.Function)2 Method (com.jetbrains.php.lang.psi.elements.Method)2 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)2 NotNull (org.jetbrains.annotations.NotNull)2 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)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 PhpClass (com.jetbrains.php.lang.psi.elements.PhpClass)1 PhpUnset (com.jetbrains.php.lang.psi.elements.PhpUnset)1 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)1 BasePhpInspection (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection)1 MessagesPresentationUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil)1 OpenapiResolveUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiResolveUtil)1 PhpLanguageUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.PhpLanguageUtil)1 Types (com.kalessil.phpStorm.phpInspectionsEA.utils.Types)1 ArrayList (java.util.ArrayList)1