Section 5/181 menit
5. Dead Code Detection: Periphery
5. Dead Code Detection: Periphery
Periphery menemukan deklarasi Swift yang tidak pernah digunakan — function, class, struct, property, enum case yang bisa dihapus.
swift
# Install
brew install peripheryapp/periphery/periphery
# Atau SPM
.package(url: "https://github.com/peripheryapp/periphery", from: "2.21.0")
swift
# Setup interaktif (pertama kali)
cd MyApp
periphery setup
# → Pilih targets yang akan di-scan
# → Generate .periphery.yml
# Scan
periphery scan
# Contoh output:
# warning: /src/MyApp/OldNetworkManager.swift:12:1:
# OldNetworkManager is unused
# warning: /src/MyApp/UserModel.swift:34:5:
# internalHelperFunction() is unused
swift
# .periphery.yml
project: MyApp.xcodeproj
schemes:
- MyApp
targets:
- MyApp
# Jangan flag sebagai unused jika:
retain_public: true # public API (untuk library)
retain_objc_accessible: true # ObjC interop
retain_swift_ui_previews: true
# Flag sebagai unused:
disable_redundant_public_accessibility_analysis: false
# Custom hints (mark sebagai "digunakan oleh sistem")
# Tambahkan komentar: // periphery:ignore
swift
// Menandai deklarasi yang memang "digunakan" (oleh Objective-C runtime, reflection, dll)
// periphery:ignore
class LegacyBridgeClass: NSObject {
// Digunakan dari ObjC storyboard — Periphery tidak bisa detect
@objc func handleTap(_ sender: UIButton) { }
}