integrated-application-development / integrated-application-development/sonar-delphi
Interface methods should be public
- 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
Assessment
This issue has not been assessed yet.