Search in sources :

Example 1 with PhpClass

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

the class OverridingDeprecatedMethodInspector method buildVisitor.

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

        public void visitPhpMethod(Method method) {
            /* do not process un-reportable classes and interfaces - we are searching real tech. debt here */
            final PhpClass clazz = method.getContainingClass();
            final PsiElement methodName = NamedElementUtil.getNameIdentifier(method);
            if (null == methodName || null == clazz) {
                return;
            }
            final String searchMethodName = method.getName();
            /* search for deprecated parent methods */
            final PhpClass parent = OpenapiResolveUtil.resolveSuperClass(clazz);
            final Method parentMethod = null == parent ? null : OpenapiResolveUtil.resolveMethod(parent, searchMethodName);
            if (null != parentMethod) {
                if (!method.isDeprecated() && parentMethod.isDeprecated()) {
                    final String message = patternNeedsDeprecation.replace("%m%", searchMethodName);
                    holder.registerProblem(methodName, message, ProblemHighlightType.LIKE_DEPRECATED);
                    return;
                }
                if (method.isDeprecated() && !parentMethod.isDeprecated()) {
                    final String message = patternDeprecateParent.replace("%m%", searchMethodName);
                    holder.registerProblem(methodName, message, ProblemHighlightType.WEAK_WARNING);
                    return;
                }
            }
            /* search for deprecated interface methods */
            if (!method.isDeprecated()) {
                for (final PhpClass iface : OpenapiResolveUtil.resolveImplementedInterfaces(clazz)) {
                    final Method ifaceMethod = OpenapiResolveUtil.resolveMethod(iface, searchMethodName);
                    if (ifaceMethod != null && ifaceMethod.isDeprecated()) {
                        final String message = patternNeedsDeprecation.replace("%m%", searchMethodName);
                        holder.registerProblem(methodName, message, ProblemHighlightType.LIKE_DEPRECATED);
                        return;
                    }
                }
            }
        }
    };
}
Also used : BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) Method(com.jetbrains.php.lang.psi.elements.Method) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PhpClass

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

the class ProtectedMembersOfFinalClassStrategy method isOverride.

private static boolean isOverride(@NotNull PhpClassMember member, @NotNull PhpClass clazz) {
    boolean result = false;
    final PhpClass parent = OpenapiResolveUtil.resolveSuperClass(clazz);
    if (null != parent) {
        final String memberName = member.getName();
        final PhpClassMember parentMember = member instanceof Field ? parent.findFieldByName(memberName, ((Field) member).isConstant()) : OpenapiResolveUtil.resolveMethod(parent, memberName);
        result = parentMember != null;
    }
    return result;
}
Also used : Field(com.jetbrains.php.lang.psi.elements.Field) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PhpClassMember(com.jetbrains.php.lang.psi.elements.PhpClassMember)

Example 3 with PhpClass

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

the class ComparableCoreClassesStrategy method isComparableObject.

private static boolean isComparableObject(@NotNull PsiElement operand, @NotNull PhpIndex index) {
    /* extract types of operand, check if classes are/inherited from \DateTime */
    final Set<String> operandTypes = new HashSet<>();
    if (operand instanceof PhpTypedElement) {
        final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) operand, operand.getProject());
        if (resolved != null) {
            resolved.filterUnknown().getTypes().forEach(t -> operandTypes.add(Types.getType(t)));
        }
    }
    if (!TypesSemanticsUtil.isNullableObjectInterface(operandTypes)) {
        operandTypes.clear();
        return false;
    }
    /* collect classes to check for \DateTime relationship */
    final List<PhpClass> operandClasses = new ArrayList<>();
    operandTypes.stream().filter(fqn -> fqn.charAt(0) == '\\').forEach(fqn -> operandClasses.addAll(OpenapiResolveUtil.resolveClassesAndInterfacesByFQN(fqn, index)));
    operandTypes.clear();
    /* inspect classes for being a/child of special once */
    for (final PhpClass clazz : operandClasses) {
        final HashSet<PhpClass> hierarchy = InterfacesExtractUtil.getCrawlInheritanceTree(clazz, true);
        for (final PhpClass oneClass : hierarchy) {
            if (comparableObjects.contains(oneClass.getFQN())) {
                return true;
            }
        }
        hierarchy.clear();
    }
    operandClasses.clear();
    return false;
}
Also used : PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) ExpressionSemanticUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.ExpressionSemanticUtil) Types(com.kalessil.phpStorm.phpInspectionsEA.utils.Types) TypesSemanticsUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.TypesSemanticsUtil) InterfacesExtractUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.hierarhy.InterfacesExtractUtil) Function(com.jetbrains.php.lang.psi.elements.Function) Set(java.util.Set) PhpIndex(com.jetbrains.php.PhpIndex) PhpTypedElement(com.jetbrains.php.lang.psi.elements.PhpTypedElement) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) OpenapiResolveUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiResolveUtil) PsiElement(com.intellij.psi.PsiElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) ArrayList(java.util.ArrayList) PhpTypedElement(com.jetbrains.php.lang.psi.elements.PhpTypedElement) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) HashSet(java.util.HashSet)

Example 4 with PhpClass

use of com.jetbrains.php.lang.psi.elements.PhpClass in project yii2support by nvlad.

the class ObjectFactoryReference method resolve.

@Nullable
@Override
public PsiElement resolve() {
    PsiElement possibleArrayCreation = myElement.getParent().getParent().getParent();
    if (possibleArrayCreation instanceof ArrayCreationExpression) {
        ArrayCreationExpression arrayCreation = (ArrayCreationExpression) possibleArrayCreation;
        PsiDirectory dir = myElement.getContainingFile().getContainingDirectory();
        PhpClass phpClass = ObjectFactoryUtils.findClassByArrayCreation(arrayCreation, dir);
        if (phpClass != null) {
            PsiElement field = ClassUtils.findWritableField(phpClass, myElement.getText());
            return field;
        }
    }
    return null;
}
Also used : ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) PsiDirectory(com.intellij.psi.PsiDirectory) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PhpClass

use of com.jetbrains.php.lang.psi.elements.PhpClass in project yii2support by nvlad.

the class MissingActiveRecordInActiveQueryInspection method buildVisitor.

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

        @Override
        public void visitPhpClass(PhpClass clazz) {
            if (clazz.getSuperClass() != null && clazz.getSuperClass().getFQN().equals("\\yii\\db\\ActiveQuery")) {
                PhpIndex index = PhpIndex.getInstance(clazz.getProject());
                PhpClass activeRecordClass = ClassUtils.findClassInSeeTags(index, clazz, "\\yii\\db\\BaseActiveRecord");
                if (activeRecordClass == null) {
                    problemsHolder.registerProblem(clazz.getFirstChild(), "Can not find connected ActiveRecord class.\nYou should add @see tag with linked ActiveRecord", ProblemHighlightType.WEAK_WARNING);
                }
            }
            super.visitPhpClass(clazz);
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) PhpIndex(com.jetbrains.php.PhpIndex) NotNull(org.jetbrains.annotations.NotNull)

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