rspec/rules/S1075/swift/rule.adoc

55 lines
1.4 KiB
Plaintext
Raw Normal View History

2020-06-30 12:47:33 +02:00
include::../description.adoc[]
2023-10-18 11:18:09 +02:00
== How to fix it
=== Code examples
==== Noncompliant code example
2020-06-30 12:47:33 +02:00
2023-10-18 11:18:09 +02:00
[source,swift,diff-id=1,diff-type=noncompliant]
2020-06-30 12:47:33 +02:00
----
public class Foo {
public func listUsers() -> [User] {
var users:[User]
let location = "/home/mylogin/Dev/users.txt" // Non-Compliant
let fileContent = NSString(contentsOfFile: location, encoding: NSUTF8StringEncoding, error: nil)
users = parse(fileContent!)
return users
}
}
----
2023-10-18 11:18:09 +02:00
==== Compliant solution
2020-06-30 12:47:33 +02:00
2023-10-18 11:18:09 +02:00
[source,swift,diff-id=1,diff-type=compliant]
2020-06-30 12:47:33 +02:00
----
public class Foo {
// Configuration is a class that returns customizable properties: it can be mocked to be injected during tests.
private var config:Configuration
public init(myConfig:Configuration) {
config = myConfig
}
public func listUsers() -> [User] {
var users:[User]
// Find here the way to get the correct folder, in this case using the Configuration object
let location = config.getProperty("myApplication.listingFile")
// and use this parameter instead of the hard coded path
let fileContent = NSString(contentsOfFile: location, encoding: NSUTF8StringEncoding, error: nil)
users = parse(fileContent!)
return users
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
endif::env-github,rspecator-view[]