Search in sources :

Example 26 with PhpClass

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

the class NormallyCallsParentMethodStrategy method apply.

public static void apply(@NotNull Method method, @NotNull ProblemsHolder holder) {
    final PhpClass clazz = method.getContainingClass();
    if (clazz != null) {
        final String methodName = method.getName();
        final PhpClass parentClazz = OpenapiResolveUtil.resolveSuperClass(clazz);
        final Method parentMethod = parentClazz == null ? null : OpenapiResolveUtil.resolveMethod(parentClazz, methodName);
        final PhpClass parentMethodHolder = parentMethod == null ? null : parentMethod.getContainingClass();
        if (parentMethodHolder != null && !parentMethod.isAbstract() && !parentMethod.getAccess().isPrivate()) {
            final boolean isUsed = PsiTreeUtil.findChildrenOfType(method, MethodReference.class).stream().anyMatch(call -> methodName.equals(call.getName()));
            if (!isUsed) {
                final PsiElement nameNode = NamedElementUtil.getNameIdentifier(method);
                if (nameNode != null) {
                    holder.registerProblem(nameNode, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), methodName, parentMethodHolder.getName(), methodName));
                }
            }
        }
    }
}
Also used : PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) Method(com.jetbrains.php.lang.psi.elements.Method) PsiElement(com.intellij.psi.PsiElement)

Example 27 with PhpClass

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

the class AccessModifierPresentedInspector method buildVisitor.

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

        @Override
        public void visitPhpClass(@NotNull PhpClass clazz) {
            /* community request: interfaces have only public methods, what is default access levels */
            if (!ANALYZE_INTERFACES && clazz.isInterface()) {
                return;
            }
            /* inspect methods */
            for (final Method method : clazz.getOwnMethods()) {
                final PsiElement methodName = NamedElementUtil.getNameIdentifier(method);
                if (methodName != null && method.getAccess().isPublic()) {
                    final PhpModifierList modifiers = PsiTreeUtil.findChildOfType(method, PhpModifierList.class);
                    if (modifiers != null && !modifiers.getText().toLowerCase().contains("public")) {
                        final String message = String.format(messagePattern, method.getName());
                        holder.registerProblem(methodName, message, new MemberVisibilityFix(modifiers));
                    }
                }
            }
            /* inspect fields */
            final PhpLanguageLevel phpVersion = PhpProjectConfigurationFacade.getInstance(holder.getProject()).getLanguageLevel();
            final boolean checkConstantVisibility = phpVersion.compareTo(PhpLanguageLevel.PHP710) >= 0;
            for (final Field field : clazz.getOwnFields()) {
                final PsiElement fieldName = NamedElementUtil.getNameIdentifier(field);
                if (fieldName != null && field.getModifier().isPublic()) {
                    if (field.isConstant()) {
                        /* {const}.isPublic() always returns true, even if visibility is not declared */
                        if (ANALYZE_CONSTANTS && checkConstantVisibility && field.getPrevPsiSibling() == null) {
                            final String message = String.format(messagePattern, field.getName());
                            holder.registerProblem(fieldName, message, new ConstantVisibilityFix(field));
                        }
                    } else {
                        final PhpModifierList modifiers = PsiTreeUtil.findChildOfType(field.getParent(), PhpModifierList.class);
                        if (modifiers != null && !modifiers.getText().toLowerCase().contains("public")) {
                            final String message = String.format(messagePattern, field.getName());
                            holder.registerProblem(fieldName, message, new MemberVisibilityFix(modifiers));
                        }
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) Field(com.jetbrains.php.lang.psi.elements.Field) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) Method(com.jetbrains.php.lang.psi.elements.Method) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PhpLanguageLevel(com.jetbrains.php.config.PhpLanguageLevel) PhpModifierList(com.jetbrains.php.lang.psi.elements.PhpModifierList) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with PhpClass

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

the class ProtectedMembersOfFinalClassStrategy method apply.

public static void apply(@NotNull PhpClassMember subject, @NotNull ProblemsHolder holder) {
    final PhpClass clazz = subject.getContainingClass();
    final boolean isTargetContext = clazz != null && clazz.isFinal() && subject.getModifier().isProtected();
    if (isTargetContext && OpenapiResolveUtil.resolveSuperClass(clazz) == null && !isOverride(subject, clazz)) {
        final PsiElement modifier = ModifierExtractionUtil.getProtectedModifier(subject);
        if (modifier != null) {
            holder.registerProblem(modifier, message, new MakePrivateFixer(modifier));
        }
    }
}
Also used : PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) MakePrivateFixer(com.kalessil.phpStorm.phpInspectionsEA.inspectors.semanticalAnalysis.classes.lowerAccessLevel.fixers.MakePrivateFixer) PsiElement(com.intellij.psi.PsiElement)

Example 29 with PhpClass

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

the class HasAlsoMethodStrategy method apply.

public static void apply(@NotNull Method method, @NotNull String companion, @NotNull ProblemsHolder holder) {
    final PhpClass clazz = method.getContainingClass();
    if (clazz != null && OpenapiResolveUtil.resolveMethod(clazz, companion) == null) {
        final PsiElement nameNode = NamedElementUtil.getNameIdentifier(method);
        if (nameNode != null) {
            final String message = String.format(messagePattern, method.getName(), companion);
            holder.registerProblem(nameNode, message, ProblemHighlightType.GENERIC_ERROR);
        }
    }
}
Also used : PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PsiElement(com.intellij.psi.PsiElement)

Example 30 with PhpClass

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

the class MustNotThrowExceptionsStrategy method apply.

public static void apply(final Method method, final ProblemsHolder holder) {
    if (null == method.getNameIdentifier()) {
        return;
    }
    final HashSet<PsiElement> processedRegistry = new HashSet<>();
    final HashMap<PhpClass, HashSet<PsiElement>> throwsExceptions = CollectPossibleThrowsUtil.collectNestedAndWorkflowExceptions(method, processedRegistry, holder);
    processedRegistry.clear();
    if (throwsExceptions.size() > 0) {
        for (Map.Entry<PhpClass, HashSet<PsiElement>> pair : throwsExceptions.entrySet()) {
            /* extract pairs */
            final PhpClass thrown = pair.getKey();
            final HashSet<PsiElement> expressions = pair.getValue();
            final String message = messagePattern.replace("%c%", thrown.getFQN()).replace("%m%", method.getName());
            for (PsiElement blame : expressions) {
                holder.registerProblem(blame, message, ProblemHighlightType.GENERIC_ERROR);
            }
            expressions.clear();
        }
        throwsExceptions.clear();
    }
}
Also used : PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) Map(java.util.Map) HashMap(java.util.HashMap) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet)

Aggregations

PhpClass (com.jetbrains.php.lang.psi.elements.PhpClass)35 PsiElement (com.intellij.psi.PsiElement)26 NotNull (org.jetbrains.annotations.NotNull)19 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)14 Method (com.jetbrains.php.lang.psi.elements.Method)11 PhpIndex (com.jetbrains.php.PhpIndex)8 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)7 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)7 MessagesPresentationUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil)6 OpenapiResolveUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiResolveUtil)6 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)5 Field (com.jetbrains.php.lang.psi.elements.Field)5 BasePhpInspection (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection)5 HashSet (java.util.HashSet)5 Project (com.intellij.openapi.project.Project)4 ClassReference (com.jetbrains.php.lang.psi.elements.ClassReference)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)3 Editor (com.intellij.openapi.editor.Editor)3