Skip to content

Commit

Permalink
Validate interface method access type
Browse files Browse the repository at this point in the history
  • Loading branch information
zulus committed Oct 13, 2023
1 parent 955484f commit c91b7ae
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class Messages extends NLS {
public static String EnumCaseInClass;
public static String EnumCaseWithType;
public static String EnumCaseWithoutType;
public static String IntefaceMethodAccessType;

static {
// initialize resource bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public enum PHPProblemIdentifier implements IProblemIdentifier, IProblemIdentifierExtension {

SYNTAX, USE_STATEMENTS /* deprecated */, AbstractMethodInAbstractClass, BodyForAbstractMethod, MethodRequiresBody, AbstractMethodsInConcreteClass, UndefinedType, ClassExtendFinalClass, CannotInstantiateType, ImportNotFound, DuplicateImport, UnusedImport, UnnecessaryImport, DuplicateDeclaration, DuplicateMethodDeclaration, DuplicateConstantDeclaration, DuplicateFieldDeclaration, AbstractMethodMustBeImplemented, SuperclassMustBeAClass, SuperInterfaceMustBeAnInterface, CannotUseTypeAsTrait, NestedNamespaceDeclarations, InvalidConstantExpression, ReassignAutoGlobalVariable, CannotUseReservedWord, UndefinedVariable, UnusedVariable, UnexpectedNamespaceDeclaration, FirstClassMustMatchFileName, InvalidClassBodyStatement
SYNTAX, USE_STATEMENTS /* deprecated */, AbstractMethodInAbstractClass, BodyForAbstractMethod, MethodRequiresBody, AbstractMethodsInConcreteClass, UndefinedType, ClassExtendFinalClass, CannotInstantiateType, ImportNotFound, DuplicateImport, UnusedImport, UnnecessaryImport, DuplicateDeclaration, DuplicateMethodDeclaration, DuplicateConstantDeclaration, DuplicateFieldDeclaration, AbstractMethodMustBeImplemented, SuperclassMustBeAClass, SuperInterfaceMustBeAnInterface, CannotUseTypeAsTrait, NestedNamespaceDeclarations, InvalidConstantExpression, ReassignAutoGlobalVariable, CannotUseReservedWord, UndefinedVariable, UnusedVariable, UnexpectedNamespaceDeclaration, FirstClassMustMatchFileName, InvalidClassBodyStatement, InterfaceAccessTypeMustBeOmmited

;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ FirstTypeMustMatchFileName=The declared {0} "{1}" must be named to "{2}"
PropertyInEnum=Enum may not include properties
EnumCaseInClass=Enum case can only be used in enums
EnumCaseWithType=Case {0} of non-backed enum {1} must not have a value
EnumCaseWithoutType=Case {0} of backed enum {1} must have a value
EnumCaseWithoutType=Case {0} of backed enum {1} must have a value
IntefaceMethodAccessType=Access type for interface method {0} must be omitted
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.core.runtime.Platform;
import org.eclipse.dltk.annotations.NonNull;
import org.eclipse.dltk.ast.ASTNode;
import org.eclipse.dltk.ast.declarations.Declaration;
import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
import org.eclipse.dltk.ast.declarations.TypeDeclaration;
import org.eclipse.dltk.ast.expressions.Expression;
Expand Down Expand Up @@ -66,10 +67,15 @@ public class ValidatorVisitor extends PHPASTVisitor implements IValidatorVisitor

private static final String EMPTY = ""; //$NON-NLS-1$
private static final String PAAMAYIM_NEKUDOTAIM = "::"; //$NON-NLS-1$
private static final String CALL = "()"; //$NON-NLS-1$
private static final String NAMESPACE_RESOLVER = "NAMESPACE_RESOLVER"; //$NON-NLS-1$
private static final List<String> TYPE_SKIP = new ArrayList<>();
private static final List<String> COMMENT_TYPE_SKIP = new ArrayList<>();

private static final String ACCESS_ABSTRACT = "abstract"; //$NON-NLS-1$
private static final String ACCESS_PRIVATE = "private"; //$NON-NLS-1$
private static final String ACCESS_PROTECTED = "protected"; //$NON-NLS-1$

private static final int ALLOW_ARRAY = 1;
private static final int ALLOW_NEW = 2;

Expand Down Expand Up @@ -244,11 +250,39 @@ public boolean visit(PHPMethodDeclaration s) throws Exception {
PHPProblemIdentifier.DuplicateMethodDeclaration, new String[] { s.getName() },
ProblemSeverities.Error);
} else {
if (parentType instanceof InterfaceDeclaration) {
checkInterfaceMethodMofiers((InterfaceDeclaration) parentType, s);

}
childs.put(id, s);
}
return super.visit(s);
}

private void checkInterfaceMethodMofiers(InterfaceDeclaration parentType, PHPMethodDeclaration s) {
if (PHPFlags.isAbstract(s.getModifiers()) && s.getBody().end() - s.getBody().start() == 1) {
reportAccessProblem(parentType, s, ACCESS_ABSTRACT);
}
if (PHPFlags.isPrivate(s.getModifiers())) {
reportAccessProblem(parentType, s, ACCESS_PRIVATE);
}
if (PHPFlags.isProtected(s.getModifiers())) {
reportAccessProblem(parentType, s, ACCESS_PROTECTED);
}

}

private void reportAccessProblem(TypeDeclaration parentType, Declaration s, String key) {
int indexOf = context.getSourceContents().substring(s.start(), s.getNameStart()).toLowerCase().indexOf(key);
if (indexOf >= 0) {
reportProblem(s.start() + indexOf, s.start() + indexOf + 8, Messages.IntefaceMethodAccessType,
PHPProblemIdentifier.InterfaceAccessTypeMustBeOmmited,
new String[] { parentType.getName() + PAAMAYIM_NEKUDOTAIM + s.getName() + CALL },
ProblemSeverities.Error);
}

}

@Override
public boolean visit(PHPFieldDeclaration s) throws Exception {
currentDeclaration = s;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Test interface problems
--FILE--
<?php
interface TestInterfaceAccess {
abstract function f1();
protected function f2();
private function f3();
}
?>
--EXPECT--
[line=3, start=39, end=47] Access type for interface method TestInterfaceAccess::f1() must be omitted
[line=4, start=64, end=72] Access type for interface method TestInterfaceAccess::f2() must be omitted
[line=5, start=90, end=98] Access type for interface method TestInterfaceAccess::f3() must be omitted

0 comments on commit c91b7ae

Please sign in to comment.