Static Swift Code Analysis
The Background Story
HelsinkiOS March 2017
Jouni Miettunen @jomtwi
Zalando Helsinki
Jouni Miettunen at HelsinkiOS March, 2017 1
What
• Oriented graph of
dependencies between classes
• Objective-C has objc_dep
"Class A depends on B if class
A imports class B header"
Jouni Miettunen at HelsinkiOS March, 2017 2
Why
Discover real (sub) architecture
• New features with existing APIs
• Maintenance & bug fixes
• Refactor technical dept
• Curious or new people
Jouni Miettunen at HelsinkiOS March, 2017 3
What about swift?
Jouni Miettunen at HelsinkiOS March, 2017 4
Compiled object files
• App / framework sources
• Fix project settings
• Update to latest swift
• Build project
• Parse compiled object files
• Open the graph
• Profit !!!
Jouni Miettunen at HelsinkiOS March, 2017 5
Want
• Fast
• Easy
• Configure
• Parse whatever
• Profit !!!
Jouni Miettunen at HelsinkiOS March, 2017 6
Work in progress
...
Hackweek #5
Zalando Open Source
How to Open Source at Zalando
Jouni Miettunen at HelsinkiOS March, 2017 7
Phase 0: basics
• python
• One file or folder tree
• One line, regex
• Ignore
• swift keywords
• swift vs. Objective-C frameworks (71)
Jouni Miettunen at HelsinkiOS March, 2017 8
Phase 1: import framework
import-kind → typealias struct class enum protocol
var func
The Swift Programming Language (Swift 3.0.1)
// Easy
import Foundation
@testable import MyFramework
// What to do
import func MyFramework.debugLog
import class MyFramework.DebugLogger
import enum MyFramework.DebugLevel
import class MyFramework.UIButton
Jouni Miettunen at HelsinkiOS March, 2017 9
Phase 2: class inheritance
• class, protocol, struct, enum
• generics, extensions, typealias
internal final class MyClass {}
class SomeClass: Superclass, FirstProtocol {}
open class Event <ElementA, ElementB>: EventType {}
@objc protocol MyDelegate: OtherDelegate {}
public protocol OwnProtocol: class {}
"Extensions can add new nested types to existing
classes, structures, and enumerations"
Jouni Miettunen at HelsinkiOS March, 2017 10
Phase 3: class usage
1.Definitions
2.Namespace {} and strings
3.Ignore and match
Jouni Miettunen at HelsinkiOS March, 2017 11
Phase 4: decoration
• UI via Graphviz
• Type shapes and colors
• Point of view
• Lonely
• Circular dependencies
• Risk(ish) analysis
Jouni Miettunen at HelsinkiOS March, 2017 12
Phase 5: future
• Continuous integration
• More circular dependencies
• Unused code
• Internal modules
• Any language
• Profit !!!
Jouni Miettunen at HelsinkiOS March, 2017 13
Thank You!! We're hiring, too
backend
web
mobile
Profit !!!
Jouni Miettunen at HelsinkiOS March, 2017 14
The Others
• objc_dep
• Refactorator II, The App
• Objective-C Class Dependencies Visualizer
• Swift relationship graph
Jouni Miettunen at HelsinkiOS March, 2017 15

Static Swift Code Analysis - The Background Story

  • 1.
    Static Swift CodeAnalysis The Background Story HelsinkiOS March 2017 Jouni Miettunen @jomtwi Zalando Helsinki Jouni Miettunen at HelsinkiOS March, 2017 1
  • 2.
    What • Oriented graphof dependencies between classes • Objective-C has objc_dep "Class A depends on B if class A imports class B header" Jouni Miettunen at HelsinkiOS March, 2017 2
  • 3.
    Why Discover real (sub)architecture • New features with existing APIs • Maintenance & bug fixes • Refactor technical dept • Curious or new people Jouni Miettunen at HelsinkiOS March, 2017 3
  • 4.
    What about swift? JouniMiettunen at HelsinkiOS March, 2017 4
  • 5.
    Compiled object files •App / framework sources • Fix project settings • Update to latest swift • Build project • Parse compiled object files • Open the graph • Profit !!! Jouni Miettunen at HelsinkiOS March, 2017 5
  • 6.
    Want • Fast • Easy •Configure • Parse whatever • Profit !!! Jouni Miettunen at HelsinkiOS March, 2017 6
  • 7.
    Work in progress ... Hackweek#5 Zalando Open Source How to Open Source at Zalando Jouni Miettunen at HelsinkiOS March, 2017 7
  • 8.
    Phase 0: basics •python • One file or folder tree • One line, regex • Ignore • swift keywords • swift vs. Objective-C frameworks (71) Jouni Miettunen at HelsinkiOS March, 2017 8
  • 9.
    Phase 1: importframework import-kind → typealias struct class enum protocol var func The Swift Programming Language (Swift 3.0.1) // Easy import Foundation @testable import MyFramework // What to do import func MyFramework.debugLog import class MyFramework.DebugLogger import enum MyFramework.DebugLevel import class MyFramework.UIButton Jouni Miettunen at HelsinkiOS March, 2017 9
  • 10.
    Phase 2: classinheritance • class, protocol, struct, enum • generics, extensions, typealias internal final class MyClass {} class SomeClass: Superclass, FirstProtocol {} open class Event <ElementA, ElementB>: EventType {} @objc protocol MyDelegate: OtherDelegate {} public protocol OwnProtocol: class {} "Extensions can add new nested types to existing classes, structures, and enumerations" Jouni Miettunen at HelsinkiOS March, 2017 10
  • 11.
    Phase 3: classusage 1.Definitions 2.Namespace {} and strings 3.Ignore and match Jouni Miettunen at HelsinkiOS March, 2017 11
  • 12.
    Phase 4: decoration •UI via Graphviz • Type shapes and colors • Point of view • Lonely • Circular dependencies • Risk(ish) analysis Jouni Miettunen at HelsinkiOS March, 2017 12
  • 13.
    Phase 5: future •Continuous integration • More circular dependencies • Unused code • Internal modules • Any language • Profit !!! Jouni Miettunen at HelsinkiOS March, 2017 13
  • 14.
    Thank You!! We'rehiring, too backend web mobile Profit !!! Jouni Miettunen at HelsinkiOS March, 2017 14
  • 15.
    The Others • objc_dep •Refactorator II, The App • Objective-C Class Dependencies Visualizer • Swift relationship graph Jouni Miettunen at HelsinkiOS March, 2017 15