Skip to content

Commit 182c560

Browse files
committed
Merge pull request #7 from S2dentik/fixViolations
Fix violations
2 parents 48580b8 + 9ece84e commit 182c560

16 files changed

+258
-268
lines changed

TaylorFramework/Modules/caprices/ProccessingMessage/OptionsProcessor.swift

+41-49
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ import Cocoa
1010

1111
private let EmptyResultDictionary = Options()
1212

13+
private let optionReporterType: [String: Option.Type] = [
14+
PathLong: PathOption.self, PathShort: PathOption.self,
15+
TypeLong: TypeOption.self, TypeShort: TypeOption.self,
16+
FileLong: FileOption.self, FileShort: FileOption.self,
17+
ExcludeLong: ExcludeOption.self, ExcludeShort: ExcludeOption.self,
18+
ExcludesFileLong: ExcludesFileOption.self, ExcludesFileShort: ExcludesFileOption.self,
19+
ReporterLong: ReporterOption.self, ReporterShort: ReporterOption.self,
20+
RuleCustomizationLong: RuleCustomizationOption.self,
21+
RuleCustomizationShort: RuleCustomizationOption.self,
22+
VerbosityLong: VerbosityOption.self, VerbosityShort: VerbosityOption.self
23+
]
24+
1325
/*
1426
If you change this class don't forget to fix his mock for actual right tests (if is case)
1527
*/
@@ -23,11 +35,12 @@ class OptionsProcessor {
2335
var executableOptions = [ExecutableOption]()
2436

2537
func processOptions(arguments: [String]) -> Options {
26-
let options = optionsFromArguments(arguments)
27-
if options.isEmpty {
28-
return EmptyResultDictionary
38+
let options = try! Array(arguments[1..<arguments.count]).reduceTwoElements([Option]()) {
39+
if let optionObject = optionObjectFromOption($1, argument: $2) { return $0 + optionObject }
40+
throw CommandLineError.InvalidArguments("Error committed on option `\($1)`.")
2941
}
30-
analyzePath = currentAnalyzedPath(options)
42+
if options.isEmpty { return EmptyResultDictionary }
43+
analyzePath = options.filter{ $0 is PathOption }.first?.optionArgument.absolutePath() ?? analyzePath
3144
if !OptionsValidator().validateForSingleOptions(options) { return EmptyResultDictionary }
3245
let resultDictionary = buildResultDictionaryFromOptions(executableOptions)
3346
guard processInformationalOptions() else { return EmptyResultDictionary }
@@ -46,11 +59,6 @@ class OptionsProcessor {
4659
return true
4760
}
4861

49-
func currentAnalyzedPath(options:[Option]) -> String {
50-
return options.filter{ $0 is PathOption }.first?.optionArgument.absolutePath() ?? analyzePath
51-
}
52-
53-
5462
private func buildResultDictionaryFromOptions(options: [ExecutableOption]) -> Options {
5563
var resultDictionary = EmptyResultDictionary
5664
if executeOptionsOnDictionary(&resultDictionary, options: options) {
@@ -111,52 +119,36 @@ class OptionsProcessor {
111119
}
112120

113121

114-
private func optionsFromArguments(arguments: [String]) -> [Option] {
115-
var options = [Option]()
116-
for var index = 1; index < arguments.count; index+=2 {
117-
let option = arguments[index]
118-
let optionArgument = arguments[index + 1]
119-
let optionObject = optionObjectFromOption(option, argument: optionArgument)
120-
if optionObject == nil {
121-
errorPrinter.printError("Error committed on option `\(option)`.")
122-
return []
123-
}
124-
options.append(optionObject!)
122+
private func optionObjectFromOption(option: String, argument: String) -> Option? {
123+
if option == ExcludesFileLong || option == ExcludesFileShort { isExcludesFileIndicated = true }
124+
if let optionType = optionReporterType[option] {
125+
return configureOption(optionType, argument: argument)
125126
}
126-
127-
return options
127+
return nil
128128
}
129129

130-
131-
private func optionObjectFromOption(option:String, argument:String) -> Option? {
132-
switch option {
133-
case PathLong, PathShort:
134-
return configureOption(PathOption.self, argument: argument)
135-
case TypeLong, TypeShort:
136-
return configureOption(TypeOption.self, argument: argument)
137-
case FileLong, FileShort:
138-
return configureOption(FileOption.self, argument: argument)
139-
case ExcludeLong, ExcludeShort:
140-
return configureOption(ExcludeOption.self, argument: argument)
141-
case ExcludesFileLong, ExcludesFileShort:
142-
isExcludesFileIndicated = true
143-
return configureOption(ExcludesFileOption.self, argument: argument)
144-
case ReporterLong, ReporterShort:
145-
return configureOption(ReporterOption.self, argument: argument)
146-
case RuleCustomizationLong, RuleCustomizationShort:
147-
return configureOption(RuleCustomizationOption.self, argument: argument)
148-
case VerbosityLong, VerbosityShort:
149-
return configureOption(VerbosityOption.self, argument: argument)
150-
default:
151-
return nil
152-
}
153-
}
154-
155-
func configureOption<T: Option>(option: T.Type, argument: String) -> T {
156-
let option = T(argument: argument)
130+
func configureOption(optionType: Option.Type, argument: String) -> Option {
131+
let option = optionType.init(argument: argument)
157132
if let infoOption = option as? InformationalOption { infoOptions.append(infoOption) }
158133
else { executableOptions.append(option as! ExecutableOption) } // Safe to force unwrap
159134

160135
return option
161136
}
162137
}
138+
139+
extension Array {
140+
func reduceTwoElements<T>(initial: T, @noescape combine: (T, Array.Generator.Element, Array.Generator.Element) throws -> T) rethrows -> T {
141+
var result = initial
142+
for (first, second) in Zip2Sequence(self.enumerate().filter { $0.0.isEven }.map { $0.1 },
143+
self.enumerate().filter {$0.0.isOdd }.map {$0.1}) {
144+
do {
145+
result = try combine(result, first, second)
146+
} catch let error as NSError {
147+
errorPrinter.printError(error.localizedDescription)
148+
return initial
149+
}
150+
151+
}
152+
return result
153+
}
154+
}

TaylorFramework/Modules/finder/Finder.swift

+13-30
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,16 @@ final class Finder {
2828
excludes = Excludes(paths: parameters!.excludes, rootPath: parameters!.rootPath)
2929
let pathsFromDirectory = findPathsInDirectory(parameters!.rootPath)
3030

31-
return removeDuplicatedPaths(pathsFromDirectory + parameters!.files)
31+
return (pathsFromDirectory + parameters!.files).unique
3232
}
3333

3434
private func validateParameters(parameters: Parameters) -> Bool {
3535
return validatePath(parameters.rootPath) && validateFiles(parameters.files)
3636
}
3737

3838
private func findPathsInDirectory(path: String) -> [String] {
39+
guard parameters != nil && excludes != nil else { return [] }
3940
do {
40-
guard parameters != nil && excludes != nil else {
41-
return []
42-
}
4341
let pathsInDirectory = try fileManager.subpathsOfDirectoryAtPath(path)
4442
let paths = exclude(excludes!, fromPaths: pathsInDirectory)
4543
return paths.map { absolutePath(parameters!.rootPath, fileName: $0) }
@@ -50,18 +48,14 @@ final class Finder {
5048
}
5149

5250
private func exclude(excludes: Excludes, fromPaths files: [FilePath]) -> [FilePath] {
53-
guard parameters != nil else {
54-
return []
55-
}
51+
guard parameters != nil else { return [] }
5652
return files.keepPathsMatchingType(parameters!.type)
5753
.excludePathsContainingSubpathsInArray(excludes.absolutePaths)
5854
.excludePathsContainingDirectories(excludes.relativePaths)
5955
}
6056

6157
private func validateFiles(files:[FilePath]) -> Bool {
62-
guard parameters != nil else {
63-
return true
64-
}
58+
guard parameters != nil else { return true }
6559
for file in files {
6660
if !existsFileOfTypeAtPath(file, type: parameters!.type) {
6761
printer.printWrongFilePath(file)
@@ -72,11 +66,7 @@ final class Finder {
7266
}
7367

7468
private func existsFileOfTypeAtPath(path: FilePath, type: String) -> Bool {
75-
return fileExistsAtPath(path) && path.isKindOfType(type)
76-
}
77-
78-
private func removeDuplicatedPaths(paths: [FilePath]) -> [FilePath] {
79-
return Array(Set(paths))
69+
return fileManager.fileExistsAtPath(path) && path.isKindOfType(type)
8070
}
8171

8272
private func validatePath(path: FilePath) -> Bool {
@@ -88,24 +78,17 @@ final class Finder {
8878
}
8979

9080
private func directoryExistsAtPath(path: FilePath) -> Bool {
91-
guard parameters != nil else {
92-
return false
93-
}
94-
return fileExistsAtPath(path) && isDirectoryAtPath(parameters!.rootPath)
95-
}
96-
97-
private func fileExistsAtPath(path: FilePath) -> Bool {
98-
return fileManager.fileExistsAtPath(path)
99-
}
100-
101-
private func isDirectoryAtPath(path: FilePath) -> Bool {
102-
var isDirectory = ObjCBool(false)
103-
fileManager.fileExistsAtPath(path, isDirectory: &isDirectory)
104-
105-
return isDirectory.boolValue
81+
guard let rootPath = parameters?.rootPath else { return false }
82+
return fileManager.fileExistsAtPath(path) && fileManager.isDirectory(rootPath)
10683
}
10784

10885
private func absolutePath(path: FilePath, fileName: String) -> FilePath {
10986
return path + DirectorySuffix.Slash + fileName
11087
}
11188
}
89+
90+
extension Array where Element: Equatable {
91+
var unique: [Element] {
92+
return self.reduce([Element]()) { !$0.contains($1) ? $0 + $1 : $0 }
93+
}
94+
}

TaylorFramework/Modules/scissors/ComponentFinder.swift

+12-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct ComponentFinder {
2323
do {
2424
let regex = try NSRegularExpression(pattern: pattern, options: [.DotMatchesLineSeparators])
2525
return regex.matchesInString(text, options: [], range: NSMakeRange(0, text.characters.count)).map {
26-
$0.range
26+
$0.range
2727
}.reduce([OffsetRange]()) {
2828
$0 + OffsetRange(start: $1.location, end: $1.location + $1.length)
2929
}
@@ -52,7 +52,7 @@ struct ComponentFinder {
5252

5353
func findComments() -> [ExtendedComponent] {
5454
return syntaxMap.tokens.filter {
55-
ComponentType(type: $0.type) == ComponentType.Comment
55+
ComponentType(type: $0.type) == ComponentType.Comment
5656
}.reduce([ExtendedComponent]()) {
5757
$0 + ExtendedComponent(dict: $1.dictionaryValue)
5858
}
@@ -83,9 +83,9 @@ struct ComponentFinder {
8383
let settersRanges = findRanges("(set($|[ \\t\\n{}]))", text: text)
8484
if gettersRanges.isEmpty { return findObserverGetters(text) }
8585

86-
accessors.append(ExtendedComponent(type: .Function, range: gettersRanges.first!, name: "get", parent: nil))
86+
accessors.append(ExtendedComponent(type: .Function, range: gettersRanges.first!, name: "get"))
8787
if !settersRanges.isEmpty {
88-
accessors.append(ExtendedComponent(type: .Function, range: settersRanges.first!, name: "set", parent: nil))
88+
accessors.append(ExtendedComponent(type: .Function, range: settersRanges.first!, name: "set"))
8989
}
9090
accessors.sortInPlace( { $0.offsetRange.start < $1.offsetRange.start } )
9191
if accessors.count == 1 {
@@ -103,18 +103,17 @@ struct ComponentFinder {
103103
var didSetRanges = findRanges("(didSet($|[ \\t\\n{}]))", text: text)
104104
var observers = [ExtendedComponent]()
105105
if willSetRanges.count > 0 {
106-
observers.append(ExtendedComponent(type: .Function, range: willSetRanges[0], name: "willSet", parent: nil))
106+
observers.append(ExtendedComponent(type: .Function, range: willSetRanges[0], name: "willSet"))
107107
}
108108
if didSetRanges.count > 0 {
109-
observers.append(ExtendedComponent(type: .Function, range: didSetRanges[0], name: "didSet", parent: nil))
109+
observers.append(ExtendedComponent(type: .Function, range: didSetRanges[0], name: "didSet"))
110110
}
111-
observers.sortInPlace( { $0.offsetRange.start < $1.offsetRange.start } )
112-
switch observers.count {
113-
case 0: return []
114-
case 1: observers[0].offsetRange.end = text.characters.count - 1
115-
case 2: observers[0].offsetRange.end = observers[1].offsetRange.start - 1
116-
observers[1].offsetRange.end = text.characters.count - 1
117-
default: break
111+
observers.sortInPlace { $0.offsetRange.start < $1.offsetRange.start }
112+
if observers.count == 1 {
113+
observers[0].offsetRange.end = text.characters.count - 1
114+
} else if observers.count == 2 {
115+
observers[0].offsetRange.end = observers[1].offsetRange.start - 1
116+
observers[1].offsetRange.end = text.characters.count - 1
118117
}
119118

120119
return observers

TaylorFramework/Modules/scissors/ExtendedComponent.swift

+14-51
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ final class ExtendedComponent {
1515
var offsetRange: OffsetRange
1616
var type: ComponentType
1717
var name: String?
18-
var parent: ExtendedComponent?
18+
var parent: ExtendedComponent? = nil
1919
var components: [ExtendedComponent]
2020

21-
init(type: ComponentType, range: OffsetRange, name: String? = nil, parent: ExtendedComponent? = nil) {
21+
init(type: ComponentType, range: OffsetRange, name: String? = nil) {
2222
self.type = type
2323
self.offsetRange = range
24-
self.parent = parent
2524
self.components = []
2625
self.name = name
2726
}
@@ -43,63 +42,27 @@ final class ExtendedComponent {
4342

4443
func appendComponents(var components: [ExtendedComponent], array: XPCArray) -> [ExtendedComponent] {
4544
var braces = 0
46-
for (var childNumber = 0; childNumber < array.count; childNumber++) {
47-
let structure = array[childNumber]
48-
let typeString = structure.asDictionary.type
49-
45+
array.enumerate().forEach { (childNumber, structure) in
5046
var offsetRange = structure.asDictionary.offsetRange
51-
var componentType = ComponentType(type: typeString)
52-
if isElseIf(componentType) { componentType = .ElseIf }
53-
else if isElse(componentType) && childNumber == array.count-1 && braces > 0 { componentType = .Else }
54-
else if componentType.isVariable {
47+
let type = getComponentType(structure.asDictionary.type, bracesCount: braces, isLast: childNumber == array.count - 1)
48+
if type.isVariable {
5549
let bodyOffsetEnd = structure.asDictionary.bodyLength + structure.asDictionary.bodyOffset
56-
if bodyOffsetEnd != 0 {
57-
offsetRange.end = bodyOffsetEnd
58-
}
59-
}
60-
else if componentType.isOther { continue }
61-
if componentType.isBrace { braces++ }
62-
63-
let child = ExtendedComponent(type: componentType, range: offsetRange, name: structure.asDictionary.name)
50+
if bodyOffsetEnd != 0 { offsetRange.end = bodyOffsetEnd }
51+
} else if type.isOther { return }
52+
if type.isBrace { braces++ }
53+
let child = ExtendedComponent(type: type, range: offsetRange, name: structure.asDictionary.name)
6454
components.append(child)
6555
components = child.appendComponents(components, array: structure.asDictionary.substructure)
6656
}
6757

6858
return components
6959
}
7060

71-
func addChild(child: ExtendedComponent) -> ExtendedComponent {
72-
self.components.append(child)
73-
child.parent = self
74-
return child
75-
}
76-
77-
func addChild(type: ComponentType, range: OffsetRange, name: String? = nil) -> ExtendedComponent {
78-
let child = ExtendedComponent(type: type, range: range, name: name, parent: self)
79-
self.components.append(child)
80-
return child
81-
}
82-
83-
func contains(node: ExtendedComponent) -> Bool {
84-
return (node.offsetRange.start >= self.offsetRange.start)
85-
&& (node.offsetRange.end <= self.offsetRange.end)
86-
}
87-
88-
func insert(node: ExtendedComponent) {
89-
for child in self.components {
90-
if child.contains(node) {
91-
child.insert(node)
92-
return
93-
} else if node.contains(child) {
94-
remove(child)
95-
node.addChild(child)
96-
}
97-
}
98-
self.addChild(node)
99-
}
100-
101-
func remove(component: ExtendedComponent) {
102-
components = components.filter() { $0 != component }
61+
func getComponentType(type: String, bracesCount: Int, isLast: Bool) -> ComponentType {
62+
let type = ComponentType(type: type)
63+
if isElseIf(type) { return .ElseIf }
64+
else if isElse(type) && isLast && bracesCount > 0 { return .Else }
65+
else { return type }
10366
}
10467

10568
func insert(components: [ExtendedComponent]) {

0 commit comments

Comments
 (0)