peripheryapp / periphery

A tool to identify unused code in Swift projects.
MIT License
5.02k stars 178 forks source link

Feature request: Ignore only Encodable properties #736

Closed Brokkoli11 closed 1 month ago

Brokkoli11 commented 1 month ago

Hi, we have recently started using periphery for dead code analysis on our project and we find it very useful!

One thing which would make it even better for us to have an option similar to retain-codable-properties, but only retain Encodable and not Decodable. The reason we need this is that we avoid adding unused properties in the network response models, but for Encodable types (for request bodies), we get a lot of false positives because we convert these Encodable types to dictionary through an extension on Encodable, something like:

extension Encodable {
  var dictionary: [String: Any]? {
    guard let data = try? JSONEncoder().encode(self) else { return nil }
    return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] }
  }
}

It seems to confuse the tool when the JSONEncoder is invoked from a generic method like this and not directly on the type. This means that the explanation in the README.md (Encodable types) does not apply to this method.

Also this extension is in a remote swift package which I believe Periphery doesnt see. In our case it would be fine to just ignore the Encodables as there are not so many violations anyways.