diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 26ab37bb4..746dc6bcd 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -273,6 +273,69 @@ enabledByDefault="true" level="WARNING" implementationClass="com.magento.idea.magento2uct.inspections.php.deprecation.ImportingDeprecatedInterface"/> + + + + + + + + + diff --git a/resources/inspectionDescriptions/CallingDeprecatedMethod.html b/resources/inspectionDescriptions/CallingDeprecatedMethod.html new file mode 100644 index 000000000..87f4c56a3 --- /dev/null +++ b/resources/inspectionDescriptions/CallingDeprecatedMethod.html @@ -0,0 +1,7 @@ + + +

[1439] Call Adobe Commerce @deprecated method: The deprecated method will be removed in upcoming versions. Consider relying on methods declared in API interfaces instead.

+ +

Text after this comment will only be shown in the settings of the inspection.

+ + diff --git a/resources/inspectionDescriptions/ImplementedDeprecatedInterface.html b/resources/inspectionDescriptions/ImplementedDeprecatedInterface.html new file mode 100644 index 000000000..6c623a680 --- /dev/null +++ b/resources/inspectionDescriptions/ImplementedDeprecatedInterface.html @@ -0,0 +1,7 @@ + + +

[1338] Implemented Adobe Commerce @deprecated interface: The deprecated interface will be removed in upcoming versions. Consider removing the interface inheritance, using an interface marked as @api or an interface introduced within your implementation instead.

+ +

Text after this comment will only be shown in the settings of the inspection.

+ + diff --git a/resources/inspectionDescriptions/InheritedDeprecatedInterface.html b/resources/inspectionDescriptions/InheritedDeprecatedInterface.html new file mode 100644 index 000000000..58b30f232 --- /dev/null +++ b/resources/inspectionDescriptions/InheritedDeprecatedInterface.html @@ -0,0 +1,7 @@ + + +

[1337] Inherited from Adobe Commerce @deprecated interface: The deprecated interface will be removed in upcoming versions. Consider removing the interface inheritance, using an interface marked as @api or an interface introduced within your implementation instead.

+ +

Text after this comment will only be shown in the settings of the inspection.

+ + diff --git a/resources/inspectionDescriptions/OverridingDeprecatedConstant.html b/resources/inspectionDescriptions/OverridingDeprecatedConstant.html new file mode 100644 index 000000000..ec169b9ee --- /dev/null +++ b/resources/inspectionDescriptions/OverridingDeprecatedConstant.html @@ -0,0 +1,7 @@ + + +

[1235] Overriding Adobe Commerce @deprecated constant: The deprecated constant will be removed in upcoming versions. Consider using a constant marked as @api or a private constant within your implementation instead.

+ +

Text after this comment will only be shown in the settings of the inspection.

+ + diff --git a/resources/inspectionDescriptions/OverridingDeprecatedProperty.html b/resources/inspectionDescriptions/OverridingDeprecatedProperty.html new file mode 100644 index 000000000..d66641df0 --- /dev/null +++ b/resources/inspectionDescriptions/OverridingDeprecatedProperty.html @@ -0,0 +1,7 @@ + + +

[1535] Overriding Adobe Commerce @deprecated property: The deprecated property will be removed in upcoming versions. Consider relying on methods declared in API interfaces or using a private property within your implementation instead.

+ +

Text after this comment will only be shown in the settings of the inspection.

+ + diff --git a/resources/inspectionDescriptions/UsingDeprecatedClass.html b/resources/inspectionDescriptions/UsingDeprecatedClass.html new file mode 100644 index 000000000..d60393dd3 --- /dev/null +++ b/resources/inspectionDescriptions/UsingDeprecatedClass.html @@ -0,0 +1,7 @@ + + +

[1134] Using Adobe Commerce @deprecated class: The extended class will be removed in upcoming versions. Consider using Adobe Commerce class marked as @api instead.

+ +

Text after this comment will only be shown in the settings of the inspection.

+ + diff --git a/resources/inspectionDescriptions/UsingDeprecatedConstant.html b/resources/inspectionDescriptions/UsingDeprecatedConstant.html new file mode 100644 index 000000000..fc7b33df8 --- /dev/null +++ b/resources/inspectionDescriptions/UsingDeprecatedConstant.html @@ -0,0 +1,7 @@ + + +

[1234] Using Adobe Commerce @deprecated constant: The deprecated constant will be removed in upcoming versions. Consider using a constant marked as @api or a private constant within your implementation instead.

+ +

Text after this comment will only be shown in the settings of the inspection.

+ + diff --git a/resources/inspectionDescriptions/UsingDeprecatedInterface.html b/resources/inspectionDescriptions/UsingDeprecatedInterface.html new file mode 100644 index 000000000..9a45dee77 --- /dev/null +++ b/resources/inspectionDescriptions/UsingDeprecatedInterface.html @@ -0,0 +1,7 @@ + + +

[1334] Used Adobe Commerce @deprecated interface: The deprecated interface will be removed in upcoming versions. Consider using an interface or class marked as @api instead.

+ +

Text after this comment will only be shown in the settings of the inspection.

+ + diff --git a/resources/inspectionDescriptions/UsingDeprecatedProperty.html b/resources/inspectionDescriptions/UsingDeprecatedProperty.html new file mode 100644 index 000000000..3b31ff6e1 --- /dev/null +++ b/resources/inspectionDescriptions/UsingDeprecatedProperty.html @@ -0,0 +1,7 @@ + + +

[1534] Using Adobe Commerce @deprecated property: The deprecated method will be removed in upcoming versions. Consider relying on methods declared in API interfaces instead.

+ +

Text after this comment will only be shown in the settings of the inspection.

+ + diff --git a/resources/uct/bundle/inspection.properties b/resources/uct/bundle/inspection.properties index 3f41dfdb3..e54130cfd 100644 --- a/resources/uct/bundle/inspection.properties +++ b/resources/uct/bundle/inspection.properties @@ -1,20 +1,25 @@ inspection.deprecation.group.name=Deprecation Inspections inspection.displayName.ExtendingDeprecatedClass=Extending from Adobe Commerce @deprecated class inspection.displayName.ImportingDeprecatedClass=Importing Adobe Commerce @deprecated class -inspection.displayName.ImportingDeprecatedInterface=Importing Adobe Commerce @deprecated interface +inspection.displayName.ImportingDeprecatedInterface=Imported Adobe Commerce @deprecated interface +inspection.displayName.UsingDeprecatedClass=Using Adobe Commerce @deprecated class +inspection.displayName.UsingDeprecatedInterface=Used Adobe Commerce @deprecated interface +inspection.displayName.UsingDeprecatedConstant=Using Adobe Commerce @deprecated constant +inspection.displayName.OverridingDeprecatedConstant=Overriding Adobe Commerce @deprecated constant +inspection.displayName.OverridingDeprecatedProperty=Overriding Adobe Commerce @deprecated property +inspection.displayName.InheritedDeprecatedInterface=Inherited from Adobe Commerce @deprecated interface +inspection.displayName.ImplementedDeprecatedInterface=Implemented Adobe Commerce @deprecated interface +inspection.displayName.CallingDeprecatedMethod=Call Adobe Commerce @deprecated method +inspection.displayName.UsingDeprecatedProperty=Using Adobe Commerce @deprecated property customCode.warnings.deprecated.1131=[1131] Extending from @deprecated class ''{0}'' customCode.warnings.deprecated.1132=[1132] Importing @deprecated class ''{0}'' -customCode.warnings.deprecated.1133=[1133] Loading Adobe Commerce @deprecated class {0} in {1}. The extended class will be removed in upcoming versions. Consider using Adobe Commerce class marked as @api instead. -customCode.warnings.deprecated.1134=[1134] Using Adobe Commerce @deprecated class {0} in {1}::{2}. The extended class will be removed in upcoming versions. Consider using Adobe Commerce class marked as @api instead. -customCode.warnings.deprecated.1234=[1234] Using Adobe Commerce @deprecated constant {0}::{1} in {2}. The deprecated constant will be removed in upcoming versions. Consider using a constant marked as @api or a private constant within your implementation instead. -customCode.warnings.deprecated.1235=[1235] Overriding Adobe Commerce @deprecated constant {0}::{1} in {2}. The deprecated constant will be removed in upcoming versions. Consider using a constant marked as @api or a private constant within your implementation instead. -customCode.warnings.deprecated.1236=[1236] Assignation of Adobe Commerce @deprecated constant {0}::{1} in {2}. The deprecated constant will be removed in upcoming versions. Consider using a constant marked as @api or a private constant within your implementation instead. +customCode.warnings.deprecated.1134=[1134] Using @deprecated class ''{0}'' +customCode.warnings.deprecated.1234=[1234] Using @deprecated constant ''{0}'' +customCode.warnings.deprecated.1235=[1235] Overriding @deprecated constant ''{0}'' customCode.warnings.deprecated.1332=[1332] Imported @deprecated interface ''{0}'' -customCode.warnings.deprecated.1334=[1334] Used Adobe Commerce @deprecated interface {0} in {1}::{2}. The deprecated interface will be removed in upcoming versions. Consider using an interface or class marked as @api instead. -customCode.warnings.deprecated.1337=[1337] Inherited from Adobe Commerce @deprecated interface {0} in {1}. The deprecated interface will be removed in upcoming versions. Consider removing the interface inheritance, using an interface marked as @api or an interface introduced within your implementation instead. -customCode.warnings.deprecated.1338=[1338] Implemented Adobe Commerce @deprecated interface {0} in {1}. The deprecated interface will be removed in upcoming versions. Consider removing the interface inheritance, using an interface marked as @api or an interface introduced within your implementation instead. -customCode.warnings.deprecated.1430=[1430] Call not declared dataobject method. The magic methods that are not declared may be changed. Consider relying on interface methods instead. -customCode.warnings.deprecated.1439=[1439] Call Adobe Commerce @deprecated method. The deprecated method will be removed in upcoming versions. Consider relying on methods declared in API interfaces instead. -customCode.warnings.deprecated.1534=[1534] Using Adobe Commerce @deprecated property. The deprecated method will be removed in upcoming versions. Consider relying on methods declared in API interfaces instead. -customCode.warnings.deprecated.1535=[1535] Overriding Adobe Commerce @deprecated property {0}::{1} in {2}. The deprecated property will be removed in upcoming versions. Consider relying on methods declared in API interfaces or using a private property within your implementation instead. -customCode.warnings.deprecated.1536=[1536] Assignation of Adobe Commerce @deprecated property. The deprecated method will be removed in upcoming versions. Consider relying on methods declared in API interfaces instead. \ No newline at end of file +customCode.warnings.deprecated.1334=[1334] Used @deprecated interface ''{0}'' +customCode.warnings.deprecated.1337=[1337] Inherited from @deprecated interface ''{0}'' +customCode.warnings.deprecated.1338=[1338] Implemented @deprecated interface ''{0}'' +customCode.warnings.deprecated.1439=[1439] Call @deprecated method ''{0}'' +customCode.warnings.deprecated.1534=[1534] Using @deprecated property ''{0}'' +customCode.warnings.deprecated.1535=[1535] Overriding @deprecated property ''{0}'' diff --git a/src/com/magento/idea/magento2uct/execution/GenerateUctReportCommand.java b/src/com/magento/idea/magento2uct/execution/GenerateUctReportCommand.java index 5b0c3151d..ef7097ec0 100644 --- a/src/com/magento/idea/magento2uct/execution/GenerateUctReportCommand.java +++ b/src/com/magento/idea/magento2uct/execution/GenerateUctReportCommand.java @@ -5,7 +5,6 @@ package com.magento.idea.magento2uct.execution; -import com.intellij.codeInspection.InspectionManager; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VfsUtil; @@ -13,28 +12,16 @@ import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; -import com.intellij.psi.util.PsiTreeUtil; -import com.jetbrains.php.codeInsight.PhpCodeInsightUtil; import com.jetbrains.php.lang.psi.PhpFile; -import com.jetbrains.php.lang.psi.elements.ClassConstantReference; -import com.jetbrains.php.lang.psi.elements.Field; -import com.jetbrains.php.lang.psi.elements.PhpClass; -import com.jetbrains.php.lang.psi.elements.PhpPsiElement; -import com.jetbrains.php.lang.psi.elements.PhpUseList; -import com.jetbrains.php.lang.psi.resolve.types.PhpTypeAnalyserVisitor; -import com.magento.idea.magento2plugin.util.GetFirstClassOfFile; import com.magento.idea.magento2uct.execution.process.OutputWrapper; import com.magento.idea.magento2uct.execution.scanner.ModuleFilesScanner; import com.magento.idea.magento2uct.execution.scanner.ModuleScanner; import com.magento.idea.magento2uct.execution.scanner.data.ComponentData; import com.magento.idea.magento2uct.inspections.UctInspectionManager; import com.magento.idea.magento2uct.inspections.UctProblemsHolder; -import com.magento.idea.magento2uct.inspections.php.DeprecationInspection; import com.magento.idea.magento2uct.packages.SupportedIssue; import java.io.File; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; import org.jetbrains.annotations.NotNull; public class GenerateUctReportCommand { @@ -95,73 +82,6 @@ public void execute() { } } - /** - * Visit directory recursively. - * - * @param directory PsiDirectory - */ - private void visitDirectory(final @NotNull PsiDirectory directory) { - final PsiFile[] files = directory.getFiles(); - - for (final PsiFile file : files) { - if (!(file instanceof PhpFile)) { - continue; - } - final PhpClass phpClass = GetFirstClassOfFile.getInstance().execute((PhpFile) file); - - if (phpClass != null) { - final DeprecationInspection deprecationInspection = - new DeprecationInspection(phpClass); - final UctProblemsHolder problemsHolder = new UctProblemsHolder( - InspectionManager.getInstance(project), - phpClass.getContainingFile(), - false - ); - final PhpTypeAnalyserVisitor visitor = - (PhpTypeAnalyserVisitor) deprecationInspection.buildVisitor( - problemsHolder, - false - ); - visitor.visitPhpClass(phpClass); - - final PhpPsiElement scopeForUseOperator = - PhpCodeInsightUtil.findScopeForUseOperator(phpClass); - - if (scopeForUseOperator != null) { - final List imports = - PhpCodeInsightUtil.collectImports(scopeForUseOperator); - - for (final PhpUseList phpUseList : imports) { - visitor.visitPhpUseList(phpUseList); - } - } - - final List classConstantReferences = - new ArrayList<>( - PsiTreeUtil.findChildrenOfType( - phpClass, - ClassConstantReference.class - ) - ); - for (final ClassConstantReference constantReference : classConstantReferences) { - visitor.visitPhpClassConstantReference(constantReference); - } - - for (final Field field : phpClass.getOwnFields()) { - visitor.visitPhpField(field); - } - - for (final ProblemDescriptor descriptor : problemsHolder.getResults()) { - output.print(descriptor.getDescriptionTemplate()); - } - } - } - - for (final PsiDirectory subDirectory : directory.getSubdirectories()) { - visitDirectory(subDirectory); - } - } - /** * Get target psi directory. * diff --git a/src/com/magento/idea/magento2uct/inspections/UctInspectionManager.java b/src/com/magento/idea/magento2uct/inspections/UctInspectionManager.java index 4fc9b0a84..9d9f8ab4f 100644 --- a/src/com/magento/idea/magento2uct/inspections/UctInspectionManager.java +++ b/src/com/magento/idea/magento2uct/inspections/UctInspectionManager.java @@ -10,12 +10,19 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElementVisitor; import com.intellij.psi.PsiFile; +import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.php.codeInsight.PhpCodeInsightUtil; import com.jetbrains.php.lang.psi.PhpFile; +import com.jetbrains.php.lang.psi.elements.AssignmentExpression; +import com.jetbrains.php.lang.psi.elements.ClassConstantReference; +import com.jetbrains.php.lang.psi.elements.ClassReference; +import com.jetbrains.php.lang.psi.elements.FieldReference; +import com.jetbrains.php.lang.psi.elements.MethodReference; import com.jetbrains.php.lang.psi.elements.PhpClass; import com.jetbrains.php.lang.psi.elements.PhpPsiElement; import com.magento.idea.magento2plugin.util.GetFirstClassOfFile; import com.magento.idea.magento2uct.packages.SupportedIssue; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; import org.jetbrains.annotations.NotNull; @@ -82,6 +89,12 @@ private List collectElements(final @NotNull PhpClass phpClass) { if (scopeForUseOperator != null) { elements.addAll(PhpCodeInsightUtil.collectImports(scopeForUseOperator)); } + elements.addAll(PsiTreeUtil.findChildrenOfType(phpClass, ClassConstantReference.class)); + elements.addAll(Arrays.asList(phpClass.getOwnFields())); + elements.addAll(PsiTreeUtil.findChildrenOfType(phpClass, MethodReference.class)); + elements.addAll(PsiTreeUtil.findChildrenOfType(phpClass, AssignmentExpression.class)); + elements.addAll(PsiTreeUtil.findChildrenOfType(phpClass, ClassReference.class)); + elements.addAll(PsiTreeUtil.findChildrenOfType(phpClass, FieldReference.class)); return elements; } diff --git a/src/com/magento/idea/magento2uct/inspections/php/DeprecationInspection.java b/src/com/magento/idea/magento2uct/inspections/php/DeprecationInspection.java deleted file mode 100644 index 70e7504e6..000000000 --- a/src/com/magento/idea/magento2uct/inspections/php/DeprecationInspection.java +++ /dev/null @@ -1,302 +0,0 @@ -/* - * Copyright © Magento, Inc. All rights reserved. - * See COPYING.txt for license details. - */ - -package com.magento.idea.magento2uct.inspections.php; - -import com.intellij.codeInspection.ProblemHighlightType; -import com.intellij.codeInspection.ProblemsHolder; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiElementVisitor; -import com.jetbrains.php.lang.PhpLangUtil; -import com.jetbrains.php.lang.inspections.PhpInspection; -import com.jetbrains.php.lang.psi.PhpPsiElementFactory; -import com.jetbrains.php.lang.psi.elements.ClassConstantReference; -import com.jetbrains.php.lang.psi.elements.ClassReference; -import com.jetbrains.php.lang.psi.elements.ExtendsList; -import com.jetbrains.php.lang.psi.elements.Field; -import com.jetbrains.php.lang.psi.elements.PhpClass; -import com.jetbrains.php.lang.psi.elements.PhpReference; -import com.jetbrains.php.lang.psi.elements.PhpUse; -import com.jetbrains.php.lang.psi.elements.PhpUseList; -import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl; -import com.jetbrains.php.lang.psi.resolve.types.PhpType; -import com.jetbrains.php.lang.psi.resolve.types.PhpTypeAnalyserVisitor; -import com.magento.idea.magento2uct.bundles.UctInspectionBundle; -import com.magento.idea.magento2uct.versioning.VersionStateManager; -import org.jetbrains.annotations.NotNull; - -public class DeprecationInspection extends PhpInspection { - - private final PhpClass phpClass; - - public DeprecationInspection(final PhpClass phpClass) { - this.phpClass = phpClass; - } - - @Override - public @NotNull PsiElementVisitor buildVisitor( - final @NotNull ProblemsHolder problemsHolder, - boolean isOnTheFly - ) { - return new PhpTypeAnalyserVisitor() { - - private final UctInspectionBundle bundle = new UctInspectionBundle(); - - @Override - public void visitPhpClass(final @NotNull PhpClass clazz) { - PhpClass parentClass = clazz.getSuperClass(); - - for (final ClassReference interfaceReference - : clazz.getImplementsList().getReferenceElements()) { - final String interfaceFqn = interfaceReference.getFQN(); - - if (interfaceFqn == null) { - continue; - } - if (VersionStateManager.getInstance().isDeprecated(interfaceFqn)) { - final String message = bundle.message( - "customCode.warnings.deprecated.1338", - interfaceFqn, - clazz.getFQN() - ); - problemsHolder.registerProblem( - interfaceReference, - message, - ProblemHighlightType.WARNING - ); - } - } - - if (parentClass == null) { - return; - } - final ExtendsList extendsList = clazz.getExtendsList(); - final String parentClassFqn = parentClass.getFQN(); - - while (parentClass != null) { - for (final ClassReference parentClassInterfaceReference - : parentClass.getImplementsList().getReferenceElements()) { - final String parentClassInterfaceFqn = - parentClassInterfaceReference.getFQN(); - - if (parentClassInterfaceFqn == null) { - continue; - } - if (VersionStateManager.getInstance() - .isDeprecated(parentClassInterfaceFqn)) { - final String message = bundle.message( - "customCode.warnings.deprecated.1337", - parentClassInterfaceFqn, - clazz.getFQN() - ); - problemsHolder.registerProblem( - parentClassInterfaceReference, - message, - ProblemHighlightType.WARNING - ); - } - } - if (VersionStateManager.getInstance().isDeprecated(parentClass.getFQN())) { - final String message = bundle.message( - "customCode.warnings.deprecated.1131", - parentClass.getFQN(), - clazz.getFQN() - ); - - for (final ClassReference classReference - : extendsList.getReferenceElements()) { - if (parentClassFqn.equals(classReference.getFQN())) { - problemsHolder.registerProblem( - classReference, - message, - ProblemHighlightType.WARNING - ); - } - } - } - parentClass = parentClass.getSuperClass(); - } - } - - @Override - public void visitPhpUseList(final PhpUseList useList) { - for (final PhpUse use : useList.getDeclarations()) { - if (VersionStateManager.getInstance().isDeprecated(use.getFQN())) { - final PhpReference phpReference = use.getTargetReference(); - boolean isInterface = false; - - if (phpReference != null) { - final PsiElement element = phpReference.resolve(); - - if (element instanceof PhpClass - && ((PhpClass) element).isInterface()) { - isInterface = true; - } - } - String bundleKey = "customCode.warnings.deprecated.1132"; - - if (isInterface) { - bundleKey = "customCode.warnings.deprecated.1332"; - } - final String message = bundle.message( - bundleKey, - use.getFQN(), - phpClass.getFQN() - ); - problemsHolder.registerProblem( - use, - message, - ProblemHighlightType.WARNING - ); - } - } - } - - @Override - public void visitPhpClassConstantReference( - final ClassConstantReference constantReference - ) { - final PsiElement element = constantReference.resolve(); - - if (element instanceof ClassConstImpl - && VersionStateManager - .getInstance().isDeprecated(((ClassConstImpl) element).getFQN()) - ) { - final PhpClass containingClass = - ((ClassConstImpl) element).getContainingClass(); - - if (containingClass == null) { - return; - } - final String message = bundle.message( - "customCode.warnings.deprecated.1234", - containingClass.getFQN(), - ((ClassConstImpl) element).getName(), - phpClass.getFQN() - ); - problemsHolder.registerProblem( - constantReference, - message, - ProblemHighlightType.WARNING - ); - } - } - - /** - * Implemented php class constant visitor. - * - * @param constant ClassConstImpl - */ - public void visitPhpClassConstant(final ClassConstImpl constant) { - PhpClass parentClass = phpClass.getSuperClass(); - boolean isFound = false; - - while (parentClass != null && !isFound) { - for (final Field field : parentClass.getOwnFields()) { - if (field instanceof ClassConstImpl - && field.getName().equals(constant.getName()) - && VersionStateManager.getInstance().isDeprecated(field.getFQN()) - ) { - final String message = bundle.message( - "customCode.warnings.deprecated.1235", - parentClass.getFQN(), - field.getName(), - phpClass.getFQN() - ); - problemsHolder.registerProblem( - constant, - message, - ProblemHighlightType.WARNING - ); - isFound = true; - } - } - parentClass = parentClass.getSuperClass(); - } - } - - @Override - public void visitPhpField(final Field field) { - super.visitPhpField(field); - - if (field instanceof ClassConstImpl) { - visitPhpClassConstant((ClassConstImpl) field); - return; - } - checkIfDeprecatedFieldOverridden(field); - final PhpType type = this.getType().filterScalarPrimitives(); - final String fieldType = type.toString(); - - if (PhpLangUtil.isFqn(fieldType)) { - final ClassReference phpReference = - PhpPsiElementFactory.createClassReference( - phpClass.getProject(), - fieldType - ); - boolean isInterface = false; - final PsiElement element = phpReference.resolve(); - - if (element instanceof PhpClass - && ((PhpClass) element).isInterface()) { - isInterface = true; - } - - if (VersionStateManager.getInstance().isDeprecated(fieldType)) { - String bundleKey = "customCode.warnings.deprecated.1134"; - - if (isInterface) { - bundleKey = "customCode.warnings.deprecated.1334"; - } - final String message = bundle.message( - bundleKey, - fieldType, - phpClass.getFQN(), - field.getNameIdentifier().getText() - ); - problemsHolder.registerProblem( - field, - message, - ProblemHighlightType.WARNING - ); - } - } - } - - /** - * Check if deprecated field is overridden. - * - * @param field PhpClass - */ - private void checkIfDeprecatedFieldOverridden(final Field field) { - PhpClass parentClass = phpClass.getSuperClass(); - boolean isFound = false; - - while (parentClass != null && !isFound) { - for (final Field parentField : parentClass.getOwnFields()) { - if (!(parentField instanceof ClassConstImpl) - && parentField.getName().equals(field.getName()) - && VersionStateManager.getInstance().isDeprecated( - parentField.getFQN()) - ) { - final String message = bundle.message( - "customCode.warnings.deprecated.1535", - parentClass.getFQN(), - parentField.getName(), - phpClass.getFQN() - ); - problemsHolder.registerProblem( - field, - message, - ProblemHighlightType.WARNING - ); - isFound = true; - } - } - parentClass = parentClass.getSuperClass(); - } - } - }; - } -} diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/CallingDeprecatedMethod.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/CallingDeprecatedMethod.java new file mode 100644 index 000000000..f6720e90e --- /dev/null +++ b/src/com/magento/idea/magento2uct/inspections/php/deprecation/CallingDeprecatedMethod.java @@ -0,0 +1,56 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2uct.inspections.php.deprecation; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiElementVisitor; +import com.jetbrains.php.lang.inspections.PhpInspection; +import com.jetbrains.php.lang.psi.elements.Method; +import com.jetbrains.php.lang.psi.elements.MethodReference; +import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor; +import com.magento.idea.magento2uct.inspections.UctProblemsHolder; +import com.magento.idea.magento2uct.packages.SupportedIssue; +import com.magento.idea.magento2uct.versioning.VersionStateManager; +import org.jetbrains.annotations.NotNull; + +public class CallingDeprecatedMethod extends PhpInspection { + + @Override + public @NotNull PsiElementVisitor buildVisitor( + final @NotNull ProblemsHolder problemsHolder, + boolean isOnTheFly + ) { + return new PhpElementVisitor() { + + @Override + public void visitPhpMethodReference(final MethodReference reference) { + final PsiElement resolvedElement = reference.resolve(); + + if (!(resolvedElement instanceof Method)) { + return; + } + final String type = ((Method) resolvedElement).getFQN(); + + if (VersionStateManager.getInstance().isDeprecated(type)) { + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.CALLING_DEPRECATED_METHOD.getCode() + ); + } + problemsHolder.registerProblem( + reference, + SupportedIssue.CALLING_DEPRECATED_METHOD.getMessage( + type.replace(".", "::") + ), + ProblemHighlightType.LIKE_DEPRECATED + ); + } + } + }; + } +} diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/ImplementedDeprecatedInterface.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/ImplementedDeprecatedInterface.java new file mode 100644 index 000000000..01b204347 --- /dev/null +++ b/src/com/magento/idea/magento2uct/inspections/php/deprecation/ImplementedDeprecatedInterface.java @@ -0,0 +1,130 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2uct.inspections.php.deprecation; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.openapi.util.Pair; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiElementVisitor; +import com.jetbrains.php.lang.inspections.PhpInspection; +import com.jetbrains.php.lang.psi.elements.ClassReference; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor; +import com.magento.idea.magento2uct.inspections.UctProblemsHolder; +import com.magento.idea.magento2uct.packages.SupportedIssue; +import com.magento.idea.magento2uct.versioning.VersionStateManager; +import java.util.List; +import org.jetbrains.annotations.NotNull; + +public class ImplementedDeprecatedInterface extends PhpInspection { + + @Override + public @NotNull PsiElementVisitor buildVisitor( + final @NotNull ProblemsHolder problemsHolder, + boolean isOnTheFly + ) { + return new PhpElementVisitor() { + + @Override + public void visitPhpClass(final PhpClass clazz) { + if (clazz.isInterface()) { + return; + } + for (final ClassReference ref : clazz.getImplementsList().getReferenceElements()) { + final String interfaceFqn = ref.getFQN(); + final PsiElement interfaceClass = ref.resolve(); + + if (interfaceFqn == null || !(interfaceClass instanceof PhpClass)) { + continue; + } + final boolean isDeprecated + = VersionStateManager.getInstance().isDeprecated(interfaceFqn); + Pair checkResult = null; + + if (isDeprecated || (checkResult = InheritedDeprecatedInterface + .hasDeprecatedParent((PhpClass) interfaceClass)).getFirst()) { + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.IMPLEMENTED_DEPRECATED_INTERFACE.getCode() + ); + } + problemsHolder.registerProblem( + ref, + SupportedIssue.IMPLEMENTED_DEPRECATED_INTERFACE.getMessage( + checkResult == null + ? interfaceFqn + : checkResult.getSecond() + ), + ProblemHighlightType.LIKE_DEPRECATED + ); + } + } + + PhpClass parentClass = clazz.getSuperClass(); + + while (parentClass != null) { + final Pair checkResult = checkImplements(parentClass); + + if (checkResult.getFirst()) { + final List classExtends = + clazz.getExtendsList().getReferenceElements(); + + if (classExtends.isEmpty()) { + break; + } + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.IMPLEMENTED_DEPRECATED_INTERFACE.getCode() + ); + } + problemsHolder.registerProblem( + classExtends.get(0), + SupportedIssue.IMPLEMENTED_DEPRECATED_INTERFACE.getMessage( + checkResult.getSecond() + ), + ProblemHighlightType.LIKE_DEPRECATED + ); + } + parentClass = parentClass.getSuperClass(); + } + } + + /** + * Check class implements. + * + * @param clazz PhpClass + * + * @return Pair[Boolean, String] + */ + private Pair checkImplements(final PhpClass clazz) { + if (clazz.isInterface()) { + return new Pair<>(false, null); + } + for (final ClassReference ref : clazz.getImplementsList().getReferenceElements()) { + final String interfaceFqn = ref.getFQN(); + final PsiElement interfaceClass = ref.resolve(); + + if (interfaceFqn == null || !(interfaceClass instanceof PhpClass)) { + continue; + } + + if (VersionStateManager.getInstance().isDeprecated(interfaceFqn)) { + return new Pair<>(true, interfaceFqn); + } + final Pair parentCheck = InheritedDeprecatedInterface + .hasDeprecatedParent((PhpClass) interfaceClass); + + if (parentCheck.getFirst()) { + return parentCheck; + } + } + + return new Pair<>(false, null); + } + }; + } +} diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/InheritedDeprecatedInterface.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/InheritedDeprecatedInterface.java new file mode 100644 index 000000000..924c0f636 --- /dev/null +++ b/src/com/magento/idea/magento2uct/inspections/php/deprecation/InheritedDeprecatedInterface.java @@ -0,0 +1,103 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2uct.inspections.php.deprecation; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.openapi.util.Pair; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiElementVisitor; +import com.jetbrains.php.lang.inspections.PhpInspection; +import com.jetbrains.php.lang.psi.elements.ClassReference; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor; +import com.magento.idea.magento2uct.inspections.UctProblemsHolder; +import com.magento.idea.magento2uct.packages.SupportedIssue; +import com.magento.idea.magento2uct.versioning.VersionStateManager; +import org.jetbrains.annotations.NotNull; + +public class InheritedDeprecatedInterface extends PhpInspection { + + @Override + public @NotNull PsiElementVisitor buildVisitor( + final @NotNull ProblemsHolder problemsHolder, + boolean isOnTheFly + ) { + return new PhpElementVisitor() { + + @Override + public void visitPhpClass(final PhpClass clazz) { + if (!clazz.isInterface()) { + return; + } + for (final ClassReference ref : clazz.getExtendsList().getReferenceElements()) { + final String interfaceFqn = ref.getFQN(); + final PsiElement interfaceClass = ref.resolve(); + + if (interfaceFqn == null || !(interfaceClass instanceof PhpClass)) { + continue; + } + Pair parentCheckResult = null; + + if (VersionStateManager.getInstance().isDeprecated(interfaceFqn) + || (parentCheckResult = hasDeprecatedParent( + (PhpClass) interfaceClass + )).getFirst()) { + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.INHERITED_DEPRECATED_INTERFACE.getCode() + ); + } + problemsHolder.registerProblem( + ref, + SupportedIssue.INHERITED_DEPRECATED_INTERFACE.getMessage( + parentCheckResult == null + ? interfaceFqn + : parentCheckResult.getSecond() + ), + ProblemHighlightType.LIKE_DEPRECATED + ); + } + } + } + }; + } + + /** + * Check if specified interface inherited from deprecated parent. + * + * @param interfaceClass PhpClass + * + * @return Pair[Boolean, String] + */ + public static Pair hasDeprecatedParent(final PhpClass interfaceClass) { + if (!interfaceClass.isInterface()) { + return new Pair<>(false, null); + } + for (final ClassReference interfaceRef + : interfaceClass.getExtendsList().getReferenceElements()) { + final String interfaceFqn = interfaceRef.getFQN(); + + if (interfaceFqn == null) { + continue; + } + + if (VersionStateManager.getInstance().isDeprecated(interfaceFqn)) { + return new Pair<>(true, interfaceFqn); + } + } + + for (final PhpClass parent : interfaceClass.getImplementedInterfaces()) { + final Pair checkResult = hasDeprecatedParent(parent); + + if (checkResult.getFirst()) { + return checkResult; + } + } + + return new Pair<>(false, null); + } +} diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/LoadingDeprecatedClass.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/LoadingDeprecatedClass.java deleted file mode 100644 index 6b965172f..000000000 --- a/src/com/magento/idea/magento2uct/inspections/php/deprecation/LoadingDeprecatedClass.java +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Copyright © Magento, Inc. All rights reserved. - * See COPYING.txt for license details. - */ - -package com.magento.idea.magento2uct.inspections.php.deprecation; - -public class LoadingDeprecatedClass { -} diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/OverridingDeprecatedConstant.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/OverridingDeprecatedConstant.java new file mode 100644 index 000000000..7698898d3 --- /dev/null +++ b/src/com/magento/idea/magento2uct/inspections/php/deprecation/OverridingDeprecatedConstant.java @@ -0,0 +1,75 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2uct.inspections.php.deprecation; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.psi.PsiElementVisitor; +import com.jetbrains.php.lang.inspections.PhpInspection; +import com.jetbrains.php.lang.psi.elements.Field; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl; +import com.jetbrains.php.lang.psi.resolve.types.PhpTypeAnalyserVisitor; +import com.magento.idea.magento2uct.inspections.UctProblemsHolder; +import com.magento.idea.magento2uct.packages.SupportedIssue; +import com.magento.idea.magento2uct.versioning.VersionStateManager; +import org.jetbrains.annotations.NotNull; + +public class OverridingDeprecatedConstant extends PhpInspection { + + @Override + public @NotNull PsiElementVisitor buildVisitor( + final @NotNull ProblemsHolder problemsHolder, + boolean isOnTheFly + ) { + return new PhpTypeAnalyserVisitor() { + + @Override + public void visitPhpField(final Field field) { + super.visitPhpField(field); + + if (!(field instanceof ClassConstImpl)) { + return; + } + final ClassConstImpl constant = (ClassConstImpl) field; + final PhpClass phpClass = field.getContainingClass(); + + if (phpClass == null) { + return; + } + PhpClass parentClass = phpClass.getSuperClass(); + boolean isFound = false; + + while (parentClass != null && !isFound) { + for (final Field ownField : parentClass.getOwnFields()) { + if (ownField instanceof ClassConstImpl + && ownField.getName().equals(constant.getName()) + && VersionStateManager.getInstance().isDeprecated(ownField.getFQN()) + ) { + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.OVERRIDING_DEPRECATED_CONSTANT.getCode() + ); + } + problemsHolder.registerProblem( + constant, + SupportedIssue.OVERRIDING_DEPRECATED_CONSTANT.getMessage( + parentClass.getFQN() + .concat("::") + .concat(ownField.getName()) + ), + ProblemHighlightType.LIKE_DEPRECATED + ); + isFound = true; + break; + } + } + parentClass = parentClass.getSuperClass(); + } + } + }; + } +} diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/OverridingDeprecatedProperty.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/OverridingDeprecatedProperty.java new file mode 100644 index 000000000..94ce8f988 --- /dev/null +++ b/src/com/magento/idea/magento2uct/inspections/php/deprecation/OverridingDeprecatedProperty.java @@ -0,0 +1,75 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2uct.inspections.php.deprecation; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.psi.PsiElementVisitor; +import com.jetbrains.php.lang.inspections.PhpInspection; +import com.jetbrains.php.lang.psi.elements.Field; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl; +import com.jetbrains.php.lang.psi.resolve.types.PhpTypeAnalyserVisitor; +import com.magento.idea.magento2uct.inspections.UctProblemsHolder; +import com.magento.idea.magento2uct.packages.SupportedIssue; +import com.magento.idea.magento2uct.versioning.VersionStateManager; +import org.jetbrains.annotations.NotNull; + +public class OverridingDeprecatedProperty extends PhpInspection { + + @Override + public @NotNull PsiElementVisitor buildVisitor( + final @NotNull ProblemsHolder problemsHolder, + boolean isOnTheFly + ) { + return new PhpTypeAnalyserVisitor() { + + @Override + public void visitPhpField(final Field field) { + super.visitPhpField(field); + + if (field instanceof ClassConstImpl) { + return; + } + final PhpClass phpClass = field.getContainingClass(); + + if (phpClass == null) { + return; + } + PhpClass parentClass = phpClass.getSuperClass(); + boolean isFound = false; + + while (parentClass != null && !isFound) { + for (final Field parentField : parentClass.getOwnFields()) { + if (!(parentField instanceof ClassConstImpl) + && parentField.getName().equals(field.getName()) + && VersionStateManager.getInstance().isDeprecated( + parentField.getFQN() + )) { + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.OVERRIDING_DEPRECATED_PROPERTY.getCode() + ); + } + problemsHolder.registerProblem( + field, + SupportedIssue.OVERRIDING_DEPRECATED_PROPERTY.getMessage( + parentClass.getFQN() + .concat("::") + .concat(parentField.getName()) + ), + ProblemHighlightType.LIKE_DEPRECATED + ); + isFound = true; + break; + } + } + parentClass = parentClass.getSuperClass(); + } + } + }; + } +} diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedClass.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedClass.java index 7904cce98..a3766be06 100644 --- a/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedClass.java +++ b/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedClass.java @@ -5,5 +5,57 @@ package com.magento.idea.magento2uct.inspections.php.deprecation; -public class UsingDeprecatedClass { +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.jetbrains.php.lang.psi.elements.ClassReference; +import com.jetbrains.php.lang.psi.elements.Field; +import com.magento.idea.magento2uct.inspections.UctProblemsHolder; +import com.magento.idea.magento2uct.packages.SupportedIssue; +import org.jetbrains.annotations.NotNull; + +public class UsingDeprecatedClass extends UsingDeprecatedType { + + @Override + protected void registerProblem( + final @NotNull ProblemsHolder problemsHolder, + final Field field, + final String fieldClass, + boolean isInterface + ) { + if (isInterface) { + return; + } + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.USING_DEPRECATED_CLASS.getCode() + ); + } + problemsHolder.registerProblem( + field, + SupportedIssue.USING_DEPRECATED_CLASS.getMessage(fieldClass), + ProblemHighlightType.LIKE_DEPRECATED + ); + } + + @Override + protected void registerReferenceProblem( + final @NotNull ProblemsHolder problemsHolder, + final ClassReference reference, + final String deprecatedType, + final boolean isInterface + ) { + if (isInterface) { + return; + } + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.USING_DEPRECATED_CLASS.getCode() + ); + } + problemsHolder.registerProblem( + reference, + SupportedIssue.USING_DEPRECATED_CLASS.getMessage(deprecatedType), + ProblemHighlightType.LIKE_DEPRECATED + ); + } } diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedConstant.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedConstant.java new file mode 100644 index 000000000..a985849a7 --- /dev/null +++ b/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedConstant.java @@ -0,0 +1,67 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2uct.inspections.php.deprecation; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiElementVisitor; +import com.jetbrains.php.lang.inspections.PhpInspection; +import com.jetbrains.php.lang.psi.elements.ClassConstantReference; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl; +import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor; +import com.magento.idea.magento2uct.inspections.UctProblemsHolder; +import com.magento.idea.magento2uct.packages.SupportedIssue; +import com.magento.idea.magento2uct.versioning.VersionStateManager; +import org.jetbrains.annotations.NotNull; + +public class UsingDeprecatedConstant extends PhpInspection { + + @Override + public @NotNull PsiElementVisitor buildVisitor( + final @NotNull ProblemsHolder problemsHolder, + boolean isOnTheFly + ) { + return new PhpElementVisitor() { + + @Override + public void visitPhpClassConstantReference( + final ClassConstantReference constantReference + ) { + final PsiElement element = constantReference.resolve(); + + if (!(element instanceof ClassConstImpl)) { + return; + } + final String constantClass = ((ClassConstImpl) element).getFQN(); + + if (!VersionStateManager.getInstance().isDeprecated(constantClass)) { + return; + } + final PhpClass containingClass = ((ClassConstImpl) element).getContainingClass(); + + if (containingClass == null) { + return; + } + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.USING_DEPRECATED_CONSTANT.getCode() + ); + } + problemsHolder.registerProblem( + constantReference, + SupportedIssue.USING_DEPRECATED_CONSTANT.getMessage( + containingClass.getFQN() + .concat("::") + .concat(((ClassConstImpl) element).getName()) + ), + ProblemHighlightType.LIKE_DEPRECATED + ); + } + }; + } +} diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedInterface.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedInterface.java new file mode 100644 index 000000000..6803f4f06 --- /dev/null +++ b/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedInterface.java @@ -0,0 +1,61 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2uct.inspections.php.deprecation; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.jetbrains.php.lang.psi.elements.ClassReference; +import com.jetbrains.php.lang.psi.elements.Field; +import com.magento.idea.magento2uct.inspections.UctProblemsHolder; +import com.magento.idea.magento2uct.packages.SupportedIssue; +import org.jetbrains.annotations.NotNull; + +public class UsingDeprecatedInterface extends UsingDeprecatedType { + + @Override + protected void registerProblem( + final @NotNull ProblemsHolder problemsHolder, + final Field field, + final String fieldInterface, + boolean isInterface + ) { + if (!isInterface) { + return; + } + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.USING_DEPRECATED_INTERFACE.getCode() + ); + } + problemsHolder.registerProblem( + field, + SupportedIssue.USING_DEPRECATED_INTERFACE.getMessage(fieldInterface), + ProblemHighlightType.LIKE_DEPRECATED + ); + } + + @Override + protected void registerReferenceProblem( + final @NotNull ProblemsHolder problemsHolder, + final ClassReference reference, + final String deprecatedType, + final boolean isInterface + ) { + if (!isInterface) { + return; + } + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.USING_DEPRECATED_INTERFACE.getCode() + ); + } + problemsHolder.registerProblem( + reference, + SupportedIssue.USING_DEPRECATED_INTERFACE.getMessage(deprecatedType), + ProblemHighlightType.LIKE_DEPRECATED + ); + } +} diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedProperty.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedProperty.java new file mode 100644 index 000000000..4081066d5 --- /dev/null +++ b/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedProperty.java @@ -0,0 +1,79 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2uct.inspections.php.deprecation; + +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiElementVisitor; +import com.intellij.psi.PsiFile; +import com.jetbrains.php.lang.inspections.PhpInspection; +import com.jetbrains.php.lang.psi.elements.Field; +import com.jetbrains.php.lang.psi.elements.FieldReference; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor; +import com.magento.idea.magento2uct.inspections.UctProblemsHolder; +import com.magento.idea.magento2uct.packages.SupportedIssue; +import com.magento.idea.magento2uct.versioning.VersionStateManager; +import org.jetbrains.annotations.NotNull; + +public class UsingDeprecatedProperty extends PhpInspection { + + @Override + public @NotNull PsiElementVisitor buildVisitor( + final @NotNull ProblemsHolder problemsHolder, + boolean isOnTheFly + ) { + return new PhpElementVisitor() { + + @Override + public void visitPhpFieldReference(final FieldReference fieldReference) { + final PsiElement element = fieldReference.resolve(); + final PsiFile file = fieldReference.getContainingFile(); + + if (!(element instanceof Field) || element.getContainingFile() == file) { + return; + } + final Field field = (Field) element; + + if (VersionStateManager.getInstance().isDeprecated(field.getFQN())) { + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.USING_DEPRECATED_PROPERTY.getCode() + ); + } + problemsHolder.registerProblem( + fieldReference, + SupportedIssue.USING_DEPRECATED_PROPERTY.getMessage( + field.getFQN().replace(".", "::") + ), + ProblemHighlightType.LIKE_DEPRECATED + ); + } else { + final PhpClass containingClass = field.getContainingClass(); + + if (containingClass == null) { + return; + } + if (VersionStateManager.getInstance().isDeprecated(containingClass.getFQN())) { + if (problemsHolder instanceof UctProblemsHolder) { + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( + SupportedIssue.USING_DEPRECATED_PROPERTY.getCode() + ); + } + problemsHolder.registerProblem( + fieldReference, + SupportedIssue.USING_DEPRECATED_PROPERTY.getMessage( + field.getFQN().replace(".", "::") + ), + ProblemHighlightType.LIKE_DEPRECATED + ); + } + } + } + }; + } +} diff --git a/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedType.java b/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedType.java new file mode 100644 index 000000000..90a2d720e --- /dev/null +++ b/src/com/magento/idea/magento2uct/inspections/php/deprecation/UsingDeprecatedType.java @@ -0,0 +1,113 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2uct.inspections.php.deprecation; + +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiElementVisitor; +import com.jetbrains.php.lang.PhpLangUtil; +import com.jetbrains.php.lang.inspections.PhpInspection; +import com.jetbrains.php.lang.psi.PhpPsiElementFactory; +import com.jetbrains.php.lang.psi.elements.ClassReference; +import com.jetbrains.php.lang.psi.elements.Field; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl; +import com.jetbrains.php.lang.psi.resolve.types.PhpType; +import com.jetbrains.php.lang.psi.resolve.types.PhpTypeAnalyserVisitor; +import com.magento.idea.magento2uct.versioning.VersionStateManager; +import org.jetbrains.annotations.NotNull; + +public abstract class UsingDeprecatedType extends PhpInspection { + + /** + * Register type specific problem. + * + * @param problemsHolder ProblemsHolder + * @param field Field + * @param deprecatedType String + * @param isInterface boolean + */ + protected abstract void registerProblem( + final @NotNull ProblemsHolder problemsHolder, + final Field field, + final String deprecatedType, + final boolean isInterface + ); + + /** + * Register type specific problem. + * + * @param problemsHolder ProblemsHolder + * @param reference ClassReference + * @param deprecatedType String + * @param isInterface boolean + */ + protected abstract void registerReferenceProblem( + final @NotNull ProblemsHolder problemsHolder, + final ClassReference reference, + final String deprecatedType, + final boolean isInterface + ); + + @Override + public @NotNull PsiElementVisitor buildVisitor( + final @NotNull ProblemsHolder problemsHolder, + boolean isOnTheFly + ) { + return new PhpTypeAnalyserVisitor() { + + @Override + public void visitPhpField(final Field field) { + super.visitPhpField(field); + final PhpClass phpClass = field.getContainingClass(); + + if (phpClass == null || field instanceof ClassConstImpl) { + return; + } + final PhpType type = this.getType().filterScalarPrimitives(); + final String fieldType = type.toString(); + + if (!PhpLangUtil.isFqn(fieldType)) { + return; + } + final ClassReference phpReference = PhpPsiElementFactory.createClassReference( + phpClass.getProject(), + fieldType + ); + boolean isInterface = false; + final PsiElement element = phpReference.resolve(); + + if (element instanceof PhpClass && ((PhpClass) element).isInterface()) { + isInterface = true; + } + + if (VersionStateManager.getInstance().isDeprecated(fieldType)) { + registerProblem(problemsHolder, field, fieldType, isInterface); + } + } + + @Override + public void visitPhpClassReference(final ClassReference reference) { + final PsiElement resolved = reference.resolve(); + + if (!(resolved instanceof PhpClass)) { + return; + } + final PhpClass phpClass = (PhpClass) resolved; + final boolean isInterface = phpClass.isInterface(); + + if (VersionStateManager.getInstance().isDeprecated(phpClass.getFQN())) { + registerReferenceProblem( + problemsHolder, + reference, + reference.getFQN(), + isInterface + ); + } + } + }; + } +} diff --git a/src/com/magento/idea/magento2uct/packages/SupportedIssue.java b/src/com/magento/idea/magento2uct/packages/SupportedIssue.java index e5556575c..68738a17c 100644 --- a/src/com/magento/idea/magento2uct/packages/SupportedIssue.java +++ b/src/com/magento/idea/magento2uct/packages/SupportedIssue.java @@ -9,9 +9,18 @@ import com.intellij.psi.PsiElementVisitor; import com.magento.idea.magento2uct.bundles.UctInspectionBundle; import com.magento.idea.magento2uct.inspections.UctProblemsHolder; +import com.magento.idea.magento2uct.inspections.php.deprecation.CallingDeprecatedMethod; import com.magento.idea.magento2uct.inspections.php.deprecation.ExtendingDeprecatedClass; +import com.magento.idea.magento2uct.inspections.php.deprecation.ImplementedDeprecatedInterface; import com.magento.idea.magento2uct.inspections.php.deprecation.ImportingDeprecatedClass; import com.magento.idea.magento2uct.inspections.php.deprecation.ImportingDeprecatedInterface; +import com.magento.idea.magento2uct.inspections.php.deprecation.InheritedDeprecatedInterface; +import com.magento.idea.magento2uct.inspections.php.deprecation.OverridingDeprecatedConstant; +import com.magento.idea.magento2uct.inspections.php.deprecation.OverridingDeprecatedProperty; +import com.magento.idea.magento2uct.inspections.php.deprecation.UsingDeprecatedClass; +import com.magento.idea.magento2uct.inspections.php.deprecation.UsingDeprecatedConstant; +import com.magento.idea.magento2uct.inspections.php.deprecation.UsingDeprecatedInterface; +import com.magento.idea.magento2uct.inspections.php.deprecation.UsingDeprecatedProperty; import java.lang.reflect.InvocationTargetException; import java.util.LinkedList; import java.util.List; @@ -36,6 +45,60 @@ public enum SupportedIssue { IssueSeverityLevel.WARNING, "customCode.warnings.deprecated.1332", ImportingDeprecatedInterface.class + ), + USING_DEPRECATED_CLASS( + 1134, + IssueSeverityLevel.WARNING, + "customCode.warnings.deprecated.1134", + UsingDeprecatedClass.class + ), + USING_DEPRECATED_INTERFACE( + 1334, + IssueSeverityLevel.WARNING, + "customCode.warnings.deprecated.1334", + UsingDeprecatedInterface.class + ), + USING_DEPRECATED_CONSTANT( + 1234, + IssueSeverityLevel.WARNING, + "customCode.warnings.deprecated.1234", + UsingDeprecatedConstant.class + ), + OVERRIDING_DEPRECATED_CONSTANT( + 1235, + IssueSeverityLevel.WARNING, + "customCode.warnings.deprecated.1235", + OverridingDeprecatedConstant.class + ), + OVERRIDING_DEPRECATED_PROPERTY( + 1535, + IssueSeverityLevel.WARNING, + "customCode.warnings.deprecated.1535", + OverridingDeprecatedProperty.class + ), + INHERITED_DEPRECATED_INTERFACE( + 1337, + IssueSeverityLevel.WARNING, + "customCode.warnings.deprecated.1337", + InheritedDeprecatedInterface.class + ), + IMPLEMENTED_DEPRECATED_INTERFACE( + 1338, + IssueSeverityLevel.WARNING, + "customCode.warnings.deprecated.1338", + ImplementedDeprecatedInterface.class + ), + CALLING_DEPRECATED_METHOD( + 1439, + IssueSeverityLevel.WARNING, + "customCode.warnings.deprecated.1439", + CallingDeprecatedMethod.class + ), + USING_DEPRECATED_PROPERTY( + 1534, + IssueSeverityLevel.WARNING, + "customCode.warnings.deprecated.1534", + UsingDeprecatedProperty.class ); private final int code;