sonar-delphi
sonar-delphi copied to clipboard
Interface methods should be public
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
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:
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.