integrated-application-development / integrated-application-development/sonar-delphi

Interface methods should be public

Open
#214 2 comments 0 reactions 0 assignees View on GitHub
feature rule
Dominant language
Java
Stars
159
Forks
31
Avg merge
5d 4h
Merged PRs (30d)
4

Description

### Prerequisites

- [X] This rule has not already been suggested.
- [X] This should be a new rule, not an improvement to an existing rule.
- [X] This rule would be generally useful, not specific to my code or setup.

### Suggested rule title

Interface methods should be public

### Rule description

When implementing a method for an interface, Delphi doesn't require that the method be as visible as the interface would suggest.

The following code compiles
```delphi
type
I = interface
procedure Foo;
end;

T = class(TInterfacedObject, I)
private
procedure Foo;
end;

procedure T.Foo; begin end;
```

This is an issue because one might look at `private procedure Foo` and assume that the method can not be accessed from outside the declaring file. However, the method is accessible via the interface:
```delphi
var
tobj: T;
begin
tobj.Foo; // doesn't compile (outside of the file `T` is declared in)
(tobj as I).Foo; // no issues
end;
```

This rule would require that all interface methods are implemented with the visibility of `public` or `published`, and raise an issue whenever the visibility is lower.

### Rationale

Most developers will read the visibility of a method and assume that alone determines how accessible it is. This escape-hatch for interface methods is entirely unexpected and subverts the entire point of access control.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.