Skip to content

UCT-731: Added inspection call non-interface method #796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,13 @@
enabledByDefault="false"
level="WARNING"
implementationClass="com.magento.idea.magento2uct.inspections.php.api.PossibleDependencyOnImplDetails"/>
<localInspection language="PHP" groupPath="UCT"
shortName="CalledNonInterfaceMethod"
bundle="uct.bundle.inspection" key="inspection.displayName.CalledNonInterfaceMethod"
groupBundle="uct.bundle.inspection" groupKey="inspection.api.group.name"
enabledByDefault="false"
level="WARNING"
implementationClass="com.magento.idea.magento2uct.inspections.php.api.CalledNonInterfaceMethod"/>
<!-- \UCT inspection -->

<internalFileTemplate name="Magento Composer JSON"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<p>[1449] Called non-interface method (that is present in implementation).</p>
<!-- tooltip end -->
<p>The purpose of this inspection is to find places where there is called a method that is not declared in the interface.</p>
</body>
</html>
2 changes: 2 additions & 0 deletions resources/uct/bundle/inspection.properties
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ inspection.displayName.ImplementedNonApiInterface=Implemented non Adobe Commerce
inspection.displayName.ExtendedNonApiClass=Extended non Adobe Commerce API class
inspection.displayName.InheritedNonApiInterface=Inherited non Adobe Commerce API interface
inspection.displayName.PossibleDependencyOnImplDetails=Possible dependency on implementation details
inspection.displayName.CalledNonInterfaceMethod=Called non-interface method
customCode.warnings.deprecated.1131=[1131] Extended class ''{0}'' that is @deprecated in the ''{1}''
customCode.warnings.deprecated.1132=[1132] Imported class ''{0}'' that is @deprecated in the ''{1}''
customCode.warnings.deprecated.1134=[1134] Used class ''{0}'' that is @deprecated in the ''{1}''
Expand Down Expand Up @@ -72,3 +73,4 @@ customCode.errors.api.1328=[1328] Implemented interface ''{0}'' is not marked as
customCode.errors.api.1121=[1121] Extended class ''{0}'' is not marked as an API
customCode.errors.api.1327=[1327] Inherited interface ''{0}'' is not marked as an API
customCode.errors.api.1428=[1428] Possible dependency on implementation details. Consider using the interface ''{0}''
customCode.errors.api.1449=[1449] Called non-interface method. Consider using only methods declared in ''{0}''
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2uct.inspections.php.api;

import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.project.Project;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.actions.PhpExpressionTypeProvider;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.inspections.php.util.PhpClassImplementsInterfaceUtil;
import com.magento.idea.magento2uct.inspections.UctProblemsHolder;
import com.magento.idea.magento2uct.inspections.php.CallMethodInspection;
import com.magento.idea.magento2uct.packages.IssueSeverityLevel;
import com.magento.idea.magento2uct.packages.SupportedIssue;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;

public class CalledNonInterfaceMethod extends CallMethodInspection {

private final PhpExpressionTypeProvider typeProvider = new PhpExpressionTypeProvider();

@Override
protected void execute(
final Project project,
final @NotNull ProblemsHolder problemsHolder,
final MethodReference methodReference,
final Method method
) {
final PhpClass parentClass = method.getContainingClass();

if (parentClass == null) {
return;
}
final String informationHint = typeProvider.getInformationHint(methodReference);

if (informationHint.isEmpty()) {
return;
}
final Collection<PhpClass> searchResult = PhpIndex.getInstance(project)
.getInterfacesByFQN(informationHint);

if (searchResult.isEmpty()) {
return;
}
final PhpClass interfaceCandidate = searchResult.stream().iterator().next();

if (interfaceCandidate == null
|| !interfaceCandidate.isInterface()
|| !PhpClassImplementsInterfaceUtil.execute(interfaceCandidate, parentClass)
) {
return;
}
final Collection<Method> methodList = interfaceCandidate.getMethods();
final List<String> methodNames = methodList
.stream()
.map(Method::getName)
.collect(Collectors.toList());

if (methodNames.contains(method.getName())) {
return;
}

if (problemsHolder instanceof UctProblemsHolder) {
((UctProblemsHolder) problemsHolder).setReservedErrorCode(
SupportedIssue.CALLED_NON_INTERFACE_METHOD.getCode()
);
}
problemsHolder.registerProblem(
methodReference,
SupportedIssue.CALLED_NON_INTERFACE_METHOD.getMessage(
interfaceCandidate.getFQN()
),
ProblemHighlightType.WARNING
);
}

@Override
protected IssueSeverityLevel getSeverityLevel() {
return SupportedIssue.CALLED_NON_INTERFACE_METHOD.getLevel();
}
}
7 changes: 7 additions & 0 deletions src/com/magento/idea/magento2uct/packages/SupportedIssue.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.magento.idea.magento2uct.bundles.UctInspectionBundle;
import com.magento.idea.magento2uct.inspections.UctProblemsHolder;
import com.magento.idea.magento2uct.inspections.php.api.CalledNonApiMethod;
import com.magento.idea.magento2uct.inspections.php.api.CalledNonInterfaceMethod;
import com.magento.idea.magento2uct.inspections.php.api.ExtendedNonApiClass;
import com.magento.idea.magento2uct.inspections.php.api.ImplementedNonApiInterface;
import com.magento.idea.magento2uct.inspections.php.api.ImportedNonApiClass;
Expand Down Expand Up @@ -261,6 +262,12 @@ public enum SupportedIssue {
IssueSeverityLevel.ERROR,
"customCode.errors.api.1428",
PossibleDependencyOnImplDetails.class
),
CALLED_NON_INTERFACE_METHOD(
1449,
IssueSeverityLevel.ERROR,
"customCode.errors.api.1449",
CalledNonInterfaceMethod.class
);

private final int code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public synchronized boolean has(final @NotNull String key) {
public String getVersion(final @NotNull String fqn) {
final String version = changelog.get(fqn);

return version == null ? "some" : version;
return version == null ? "2.3.0 or before" : version;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public synchronized boolean has(final @NotNull String fqn) {
public String getVersion(final @NotNull String fqn) {
final String version = changelog.get(fqn);

return version == null ? "some" : version;
return version == null ? "2.3.0 or before" : version;
}

/**
Expand Down