Search in sources :

Example 1 with PhpModifierList

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

the class ModifierExtractionUtil method getProtectedModifier.

@Nullable
public static PsiElement getProtectedModifier(final PhpClassMember subject) {
    final PsiElement expression = (subject instanceof Field) ? subject.getParent() : subject;
    final PhpModifierList list = PsiTreeUtil.findChildOfType(expression, PhpModifierList.class);
    final LeafPsiElement[] modifiers = PsiTreeUtil.getChildrenOfType(list, LeafPsiElement.class);
    PsiElement result = null;
    if (modifiers != null) {
        for (final LeafPsiElement modifier : modifiers) {
            if (modifier.getText().equalsIgnoreCase("protected")) {
                result = modifier;
                break;
            }
        }
    }
    return result;
}
Also used : Field(com.jetbrains.php.lang.psi.elements.Field) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) PhpModifierList(com.jetbrains.php.lang.psi.elements.PhpModifierList) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PhpModifierList

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

the class MisorderedModifiersInspector method buildVisitor.

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

        @Override
        public void visitPhpMethod(@NotNull Method method) {
            if (method.isStatic() || method.isAbstract() || method.isFinal()) {
                final PhpModifierList modifiersNode = PsiTreeUtil.findChildOfType(method, PhpModifierList.class);
                final List<LeafPsiElement> modifiers = PsiTreeUtil.findChildrenOfType(modifiersNode, LeafPsiElement.class).stream().filter(element -> !(element instanceof PsiWhiteSpace)).collect(Collectors.toList());
                if (modifiersNode != null && modifiers.size() >= 2) {
                    final String original = this.getOriginalOrder(modifiers);
                    final String expected = this.getExpectedOrder(original, standardOrder);
                    if (!original.equals(expected)) {
                        problemsHolder.registerProblem(modifiersNode, MessagesPresentationUtil.prefixWithEa(message), new TheLocalFix(expected));
                    }
                }
            }
        }

        @NotNull
        private String getOriginalOrder(@NotNull Collection<LeafPsiElement> original) {
            return original.stream().map(LeafElement::getText).collect(Collectors.joining(" ")).toLowerCase();
        }

        @NotNull
        private String getExpectedOrder(@NotNull String original, @NotNull Collection<String> expected) {
            return expected.stream().filter(original::contains).collect(Collectors.joining(" "));
        }
    };
}
Also used : PhpPsiElementFactory(com.jetbrains.php.lang.psi.PhpPsiElementFactory) BasePhpInspection(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection) Collection(java.util.Collection) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Method(com.jetbrains.php.lang.psi.elements.Method) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) List(java.util.List) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) MessagesPresentationUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Project(com.intellij.openapi.project.Project) NotNull(org.jetbrains.annotations.NotNull) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PhpModifierList(com.jetbrains.php.lang.psi.elements.PhpModifierList) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Collection(java.util.Collection) Method(com.jetbrains.php.lang.psi.elements.Method) NotNull(org.jetbrains.annotations.NotNull) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) PhpModifierList(com.jetbrains.php.lang.psi.elements.PhpModifierList) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PhpModifierList

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

Aggregations

PsiElement (com.intellij.psi.PsiElement)3 PhpModifierList (com.jetbrains.php.lang.psi.elements.PhpModifierList)3 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 Field (com.jetbrains.php.lang.psi.elements.Field)2 Method (com.jetbrains.php.lang.psi.elements.Method)2 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)2 NotNull (org.jetbrains.annotations.NotNull)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 Project (com.intellij.openapi.project.Project)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 PhpLanguageLevel (com.jetbrains.php.config.PhpLanguageLevel)1 PhpPsiElementFactory (com.jetbrains.php.lang.psi.PhpPsiElementFactory)1 PhpClass (com.jetbrains.php.lang.psi.elements.PhpClass)1 BasePhpInspection (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection)1 MessagesPresentationUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil)1