Showing 18 of 18 total issues
File MapViewController.swift
has 443 lines of code (exceeds 250 allowed). Consider refactoring. Open
import CoreLocation
import MapKit
import UIKit
@objc(HLFMapViewControllerDelegate)
MapViewController
has 23 methods (exceeds 20 allowed). Consider refactoring. Open
@objc(HLFMapViewController)
open class MapViewController: UIViewController {
open static var bundle: Bundle { return Bundle(for: self) }
Function mapView
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
open func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if isDeferringSelection {
performDeferredSelection(animated: animated)
} else if shouldRedoSearchOnPan {
guard !hasSelectedPin && !isQuerying else { return }
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function searchMapItems
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
@objc fileprivate func searchMapItems() {
let search = MKLocalSearch(request: searchRequest)
search.start { [unowned self] (searchResponse, error) in
guard let mapItems = searchResponse?.mapItems else {
print("MKLocalSearch error: \(String(describing: error))")
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function mapView
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
open func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
guard let button = control as? UIButton else { return }
guard let view = view as? MapPinView else { return }
let mapItem = MKMapItem(placemark: view.placemark)
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Condition should not be enclosed within parentheses Open
if (!isDeferredSelectionEnabled) {
- Read upRead up
- Exclude checks
redundant-parentheses
Control flow constructs (if
, else if
, switch
, for
, while
, repeat-while
, and guard
statements), Exception handling constructs (throw
, and do/catch
statements), and Initializers (array
, dictionary
, initializer patterns
) should not be enclosed in parentheses.
Additionally, method calls with no parameters and a trailing closure should not have empty parentheses following the method name.
Control flow constructs
- if, else if statement
Preferred
if SomeCondition {
} else if SomeOtherCondition {
}
Not Preferred
if (SomeCondition) {
} else if (SomeOtherCondition) {
}
- switch statement
Preferred
switch SomeData {
default:
break
}
Not Preferred
switch (SomeData) {
default:
break
}
- for loop
Preferred
for var i = 0; i < 10; i+=1 {
}
Not Preferred
for (var i = 0; i < 10; i+=1) {
}
- while loop
Preferred
while SomeCondition {
}
Not Preferred
while (SomeCondition) {
}
- repeat-while loop
Preferred
repeat {
} while SomeCondition
Not Preferred
repeat {
} while (SomeCondition)
- guard clause
Preferred
guard true else { }
Not Preferred
guard (true) else { }
Exception handling constructs
- do/catch statement
Preferred
do {
} catch SomeException {
}
Not Preferred
do {
} catch (SomeException) {
}
- throw statement
Preferred
throw SomeException
Not Preferred
throw (SomeException)
Initializers
- array items
Preferred
var shoppingList: [String] = ["Eggs", "Milk"]
Not Preferred
var shoppingList: [String] = [("Eggs"), ("Milk")]
- dictionary items
Preferred
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
Not Preferred
var airports: [String: String] = [("YYZ"): ("Toronto Pearson"), ("DUB"): ("Dublin")]
- initializer patterns
Preferred
var x: Int = 2
var y: String = "Sleekbyte"
var x = 2
Not Preferred
var x: Int = (2)
var y: String = ("Sleekbyte")
var x = (2)
Method calls
Preferred
items.map {
item in item.transform()
}
Not Preferred
items.map() {
item in item.transform()
}
Getter closing brace should be isolated on a separate line Open
get { return mapIndicator }
- Read upRead up
- Exclude checks
brace-style
Definitions of
- class
- struct
- function
- Control flow constructs (if
, else if
, else
, switch
, for
, while
, repeat-while
)
- init
- protocol
- enum
- closure
- Getters and Setters (set
, get
)
- extension
should follow the One True Brace Style (1TBS): each construct has its opening brace one the same line along with the same indentation level as its header, the statements within the braces are indented, and the closing brace at the end is on the same indentation level as the header of the function at a line of its own. Braces are not omitted for a control statement with only a single statement in its scope. Every opening brace must also have one space preceding it.
Classes
Preferred
class SomeClass {
}
class SomeClass: SomeSuperClass {
}
Not Preferred
class SomeClass
{
}
class SomeClass: SomeSuperClass{
}
Structs
Preferred
struct SomeStruct {
}
struct SomeStruct : SomeParentStruct {
}
Not Preferred
struct SomeStruct
{
}
struct SomeStruct : SomeParentStruct {
}
Functions
Preferred
func someMethod() {
}
func someOtherFunction () -> () {
}
Not Preferred
func someMethod()
{
}
func someOtherFunction () -> ()
{
}
Control flow constructs
- if, else if, and else statement
Preferred
if SomeCondition {
} else if someOtherCondition {
} else {
}
Not Preferred
if SomeCondition
{
}
else if someOtherCondition
{
}
else
{
}
- switch statement
Preferred
switch SomeData {
default:
break
}
Not Preferred
switch SomeData
{
default:
break
}
- for loop
Preferred
for var i = 0; i < 10; i+=1 {
}
Not Preferred
for var i = 0; i < 10; i+=1
{
}
- while loop
Preferred
while SomeCondition {
}
Not Preferred
while SomeCondition
{
}
- repeat-while loop
Preferred
repeat {
} while SomeCondition
Not Preferred
repeat
{
} while SomeCondition
Initializers
Preferred
init(someParameter:Double, someOtherParameter:Double) {
self.someMember = someParameter
self.someOtherMember = someOtherParameter
}
Not Preferred
init(someParameter:Double, someOtherParameter:Double)
{
self.someMember = someParameter
self.someOtherMember = someOtherParameter
}
Protocols
Preferred
protocol SomeProtocol {
}
protocol SomeOtherProtocol : X {
}
Not Preferred
protocol SomeProtocol
{
}
protocol SomeOtherProtocol : X
{
}
Enums
Preferred
enum SomeEnum {
case A, B, C, D
}
enum SomeEnum {
case A
case B
case C
case D
}
enum SomeEnum: Int {
case A, B, C = 5, D
}
Not Preferred
enum SomeEnum
{
case A, B, C, D
}
enum SomeEnum
{
case A
case B
case C
case D
}
enum SomeEnum: Int
{
case A, B, C = 5, D
}
Closures
Preferred
func someFunction () -> () {
// closure
}
Not Preferred
func someFunction () -> ()
{
// closure
}
Setters and Getters
- set
Preferred
set {
oldValue = newValue / 2
}
Not Preferred
set
{
oldValue = newValue / 2
}
- get
Preferred
get {
return value * 2
}
Not Preferred
get
{
return value * 2
}
Extensions
Preferred
extension someExtension {
}
Not Preferred
extension someExtension
{
}
Class names should be UpperCamelCase Open
class HLFMapViewController_Example_UITests: XCTestCase {
- Read upRead up
- Exclude checks
upper-camel-case
class
, enum
, enum value, struct
, and protocol
names should follow UpperCamelCase naming convention : several words are joined together, and the first letter of every word is capitalized.
Class names
Preferred
class SomeClass {
// class definition goes here
}
class SomeClass : SomeSuperClass {
// class definition goes here
}
Not Preferred
class invalidClassName {
// class definition goes here
}
class inval1dCla$$Name : SomeSuperClass {
// class definition goes here
}
Enumeration types
Preferred
enum SomeEnumeration {
// enumeration definition goes here
}
Not Preferred
enum S0me_Enumer4t!on {
// enumeration definition goes here
}
Enumeration values
Preferred
enum CompassPoint {
case North
case South
case East
case West
}
Not Preferred
enum CompassPoint {
case N0rth
case Sou-th
case EAST
case We_$t
}
Struct names
Preferred
struct SomeStructure {
// structure definition goes here
}
Not Preferred
struct Some-Structure {
// structure definition goes here
}
Protocol names
Preferred
protocol SomeProtocol {
// protocol definition goes here
}
Not Preferred
protocol someprotocol {
// protocol definition goes here
}
Force casts should be avoided Open
var placemark: MKPlacemark { return annotation as! MKPlacemark }
- Read upRead up
- Exclude checks
forced-type-cast
Avoid using the forced form of the type cast operator (as!
) because Swift is not able to determine at compile time if the type conversion will succeed. In the event of an unsuccessful conversion, a runtime error will be triggered. The conditional form of the type cast operator (as?
) is safer and should be used when possible.
Preferred
if let movie = item as? Movie {
print("Movie: '\(movie.name)', dir. \(movie.director)")
}
Not Preferred
let movie = item as! Movie
print("Movie: '\(movie.name)', dir. \(movie.director)")
Setter closing brace should be isolated on a separate line Open
set { mapIndicator = newValue }
- Read upRead up
- Exclude checks
brace-style
Definitions of
- class
- struct
- function
- Control flow constructs (if
, else if
, else
, switch
, for
, while
, repeat-while
)
- init
- protocol
- enum
- closure
- Getters and Setters (set
, get
)
- extension
should follow the One True Brace Style (1TBS): each construct has its opening brace one the same line along with the same indentation level as its header, the statements within the braces are indented, and the closing brace at the end is on the same indentation level as the header of the function at a line of its own. Braces are not omitted for a control statement with only a single statement in its scope. Every opening brace must also have one space preceding it.
Classes
Preferred
class SomeClass {
}
class SomeClass: SomeSuperClass {
}
Not Preferred
class SomeClass
{
}
class SomeClass: SomeSuperClass{
}
Structs
Preferred
struct SomeStruct {
}
struct SomeStruct : SomeParentStruct {
}
Not Preferred
struct SomeStruct
{
}
struct SomeStruct : SomeParentStruct {
}
Functions
Preferred
func someMethod() {
}
func someOtherFunction () -> () {
}
Not Preferred
func someMethod()
{
}
func someOtherFunction () -> ()
{
}
Control flow constructs
- if, else if, and else statement
Preferred
if SomeCondition {
} else if someOtherCondition {
} else {
}
Not Preferred
if SomeCondition
{
}
else if someOtherCondition
{
}
else
{
}
- switch statement
Preferred
switch SomeData {
default:
break
}
Not Preferred
switch SomeData
{
default:
break
}
- for loop
Preferred
for var i = 0; i < 10; i+=1 {
}
Not Preferred
for var i = 0; i < 10; i+=1
{
}
- while loop
Preferred
while SomeCondition {
}
Not Preferred
while SomeCondition
{
}
- repeat-while loop
Preferred
repeat {
} while SomeCondition
Not Preferred
repeat
{
} while SomeCondition
Initializers
Preferred
init(someParameter:Double, someOtherParameter:Double) {
self.someMember = someParameter
self.someOtherMember = someOtherParameter
}
Not Preferred
init(someParameter:Double, someOtherParameter:Double)
{
self.someMember = someParameter
self.someOtherMember = someOtherParameter
}
Protocols
Preferred
protocol SomeProtocol {
}
protocol SomeOtherProtocol : X {
}
Not Preferred
protocol SomeProtocol
{
}
protocol SomeOtherProtocol : X
{
}
Enums
Preferred
enum SomeEnum {
case A, B, C, D
}
enum SomeEnum {
case A
case B
case C
case D
}
enum SomeEnum: Int {
case A, B, C = 5, D
}
Not Preferred
enum SomeEnum
{
case A, B, C, D
}
enum SomeEnum
{
case A
case B
case C
case D
}
enum SomeEnum: Int
{
case A, B, C = 5, D
}
Closures
Preferred
func someFunction () -> () {
// closure
}
Not Preferred
func someFunction () -> ()
{
// closure
}
Setters and Getters
- set
Preferred
set {
oldValue = newValue / 2
}
Not Preferred
set
{
oldValue = newValue / 2
}
- get
Preferred
get {
return value * 2
}
Not Preferred
get
{
return value * 2
}
Extensions
Preferred
extension someExtension {
}
Not Preferred
extension someExtension
{
}
Line should not have any trailing whitespace Open
})
- Read upRead up
- Exclude checks
trailing-whitespace
Flag whitespace after the last non-whitespace character on each line until the newline.
Preferred
let number = 42¬
Not Preferred
let number = 42••¬
Line should not have any trailing whitespace Open
- Read upRead up
- Exclude checks
trailing-whitespace
Flag whitespace after the last non-whitespace character on each line until the newline.
Preferred
let number = 42¬
Not Preferred
let number = 42••¬
If statement closing brace should be isolated on a separate line Open
if debug { print("Reloading with \(mapItems.count) items") }
- Read upRead up
- Exclude checks
brace-style
Definitions of
- class
- struct
- function
- Control flow constructs (if
, else if
, else
, switch
, for
, while
, repeat-while
)
- init
- protocol
- enum
- closure
- Getters and Setters (set
, get
)
- extension
should follow the One True Brace Style (1TBS): each construct has its opening brace one the same line along with the same indentation level as its header, the statements within the braces are indented, and the closing brace at the end is on the same indentation level as the header of the function at a line of its own. Braces are not omitted for a control statement with only a single statement in its scope. Every opening brace must also have one space preceding it.
Classes
Preferred
class SomeClass {
}
class SomeClass: SomeSuperClass {
}
Not Preferred
class SomeClass
{
}
class SomeClass: SomeSuperClass{
}
Structs
Preferred
struct SomeStruct {
}
struct SomeStruct : SomeParentStruct {
}
Not Preferred
struct SomeStruct
{
}
struct SomeStruct : SomeParentStruct {
}
Functions
Preferred
func someMethod() {
}
func someOtherFunction () -> () {
}
Not Preferred
func someMethod()
{
}
func someOtherFunction () -> ()
{
}
Control flow constructs
- if, else if, and else statement
Preferred
if SomeCondition {
} else if someOtherCondition {
} else {
}
Not Preferred
if SomeCondition
{
}
else if someOtherCondition
{
}
else
{
}
- switch statement
Preferred
switch SomeData {
default:
break
}
Not Preferred
switch SomeData
{
default:
break
}
- for loop
Preferred
for var i = 0; i < 10; i+=1 {
}
Not Preferred
for var i = 0; i < 10; i+=1
{
}
- while loop
Preferred
while SomeCondition {
}
Not Preferred
while SomeCondition
{
}
- repeat-while loop
Preferred
repeat {
} while SomeCondition
Not Preferred
repeat
{
} while SomeCondition
Initializers
Preferred
init(someParameter:Double, someOtherParameter:Double) {
self.someMember = someParameter
self.someOtherMember = someOtherParameter
}
Not Preferred
init(someParameter:Double, someOtherParameter:Double)
{
self.someMember = someParameter
self.someOtherMember = someOtherParameter
}
Protocols
Preferred
protocol SomeProtocol {
}
protocol SomeOtherProtocol : X {
}
Not Preferred
protocol SomeProtocol
{
}
protocol SomeOtherProtocol : X
{
}
Enums
Preferred
enum SomeEnum {
case A, B, C, D
}
enum SomeEnum {
case A
case B
case C
case D
}
enum SomeEnum: Int {
case A, B, C = 5, D
}
Not Preferred
enum SomeEnum
{
case A, B, C, D
}
enum SomeEnum
{
case A
case B
case C
case D
}
enum SomeEnum: Int
{
case A, B, C = 5, D
}
Closures
Preferred
func someFunction () -> () {
// closure
}
Not Preferred
func someFunction () -> ()
{
// closure
}
Setters and Getters
- set
Preferred
set {
oldValue = newValue / 2
}
Not Preferred
set
{
oldValue = newValue / 2
}
- get
Preferred
get {
return value * 2
}
Not Preferred
get
{
return value * 2
}
Extensions
Preferred
extension someExtension {
}
Not Preferred
extension someExtension
{
}
Force casts should be avoided Open
return mapView.annotations.filter(isNonSelectedPlacemark) as! [MKPlacemark]
- Read upRead up
- Exclude checks
forced-type-cast
Avoid using the forced form of the type cast operator (as!
) because Swift is not able to determine at compile time if the type conversion will succeed. In the event of an unsuccessful conversion, a runtime error will be triggered. The conditional form of the type cast operator (as?
) is safer and should be used when possible.
Preferred
if let movie = item as? Movie {
print("Movie: '\(movie.name)', dir. \(movie.director)")
}
Not Preferred
let movie = item as! Movie
print("Movie: '\(movie.name)', dir. \(movie.director)")
Force casts should be avoided Open
let existing = mapView.annotations.filter { $0 is MKPlacemark } as! [MKPlacemark]
- Read upRead up
- Exclude checks
forced-type-cast
Avoid using the forced form of the type cast operator (as!
) because Swift is not able to determine at compile time if the type conversion will succeed. In the event of an unsuccessful conversion, a runtime error will be triggered. The conditional form of the type cast operator (as?
) is safer and should be used when possible.
Preferred
if let movie = item as? Movie {
print("Movie: '\(movie.name)', dir. \(movie.director)")
}
Not Preferred
let movie = item as! Movie
print("Movie: '\(movie.name)', dir. \(movie.director)")
TODO found Open
// TODO: Handle location loading timeout.
- Exclude checks
TODO found Open
// TODO: Restore searchController state by implementing UIStateRestoring.
- Exclude checks
TODO found Open
// TODO: Test usability of search results in this state.
- Exclude checks