{"id":32150650,"url":"https://github.com/fummicc1/swift-complexity","last_synced_at":"2026-02-20T00:31:19.386Z","repository":{"id":310278580,"uuid":"1020980441","full_name":"fummicc1/swift-complexity","owner":"fummicc1","description":"Code Complexity Analyzer for Swift","archived":false,"fork":false,"pushed_at":"2026-02-11T05:48:12.000Z","size":1234,"stargazers_count":23,"open_issues_count":1,"forks_count":2,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-02-18T17:57:24.569Z","etag":null,"topics":["analysis","complexity-analysis","metrics","swift"],"latest_commit_sha":null,"homepage":"https://swift-complexity.fummicc1.dev/","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fummicc1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-07-16T17:30:21.000Z","updated_at":"2026-02-02T14:57:34.000Z","dependencies_parsed_at":"2025-09-12T17:45:07.589Z","dependency_job_id":null,"html_url":"https://github.com/fummicc1/swift-complexity","commit_stats":null,"previous_names":["fummicc1/swift-complexity"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/fummicc1/swift-complexity","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fummicc1%2Fswift-complexity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fummicc1%2Fswift-complexity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fummicc1%2Fswift-complexity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fummicc1%2Fswift-complexity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fummicc1","download_url":"https://codeload.github.com/fummicc1/swift-complexity/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fummicc1%2Fswift-complexity/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29637408,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T22:32:43.237Z","status":"ssl_error","status_checked_at":"2026-02-19T22:32:38.330Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analysis","complexity-analysis","metrics","swift"],"created_at":"2025-10-21T10:14:34.717Z","updated_at":"2026-02-20T00:31:19.381Z","avatar_url":"https://github.com/fummicc1.png","language":"Swift","readme":"# swift-complexity\n\nA command-line tool to analyze Swift code complexity and quality metrics using swift-syntax.\n\n## Features\n\n- **Multiple Complexity Metrics**: Supports cyclomatic complexity, cognitive complexity, and LCOM4 cohesion analysis\n- **LCOM4 Class Cohesion**: High-precision (90-95%) class cohesion measurement using IndexStore-DB semantic analysis\n- **Web-based Debug Interface**: Interactive browser-based complexity analyzer ([Try it online](https://swift-complexity.fummicc1.dev))\n- **Xcode Integration**: Seamless integration with Xcode via Build Tool Plugin for complexity feedback during build phase\n- **Xcode Diagnostics**: Display complexity warnings and errors directly in Xcode editor with accurate line numbers\n- **Configurable Thresholds**: Set custom complexity thresholds via Xcode Build Settings or environment variables\n- **Exit Code Integration**: Returns exit code 1 when complexity thresholds are exceeded, perfect for CI/CD pipelines\n- **Multiple Output Formats**: Text, JSON, XML, and Xcode diagnostics output for different use cases\n- **Flexible Analysis**: Single files, directories, or recursive directory analysis\n- **Swift Syntax Based**: Uses `swift-syntax` for accurate Swift code parsing\n- **Cross-Platform Support**: CLI works on macOS and Linux, library works on iOS 13+.\n- **Extensible Architecture**: Designed to support additional quality metrics in the future\n\n## Quick Start\n\n### Web Interface (Try Online)\n\nVisit [swift-complexity.fummicc1.dev](https://swift-complexity.fummicc1.dev) to analyze Swift code instantly in your browser.\n\n![Web Interface](docs/imgs/website.png)\n\n### Installation\n\n```bash\ngit clone https://github.com/fummicc1/swift-complexity\ncd swift-complexity\nswift build -c release\n```\n\n### Basic Usage\n\n```bash\n# Analyze a single file\nswift run SwiftComplexityCLI path/to/file.swift\n\n# Analyze a directory with threshold enforcement\nswift run SwiftComplexityCLI Sources --threshold 10\n\n# JSON output for tooling integration\nswift run SwiftComplexityCLI Sources --format json --recursive\n\n# Xcode diagnostics format (for IDE integration)\nswift run SwiftComplexityCLI Sources --format xcode --threshold 15\n\n# LCOM4 class cohesion analysis (requires swift build first)\nswift build  # Generate index\nswift run SwiftComplexityCLI Sources --lcom4 --index-store-path .build/debug/index/store\n```\n\n## CLI Integration\n\nThe tool returns exit code 1 when any function exceeds the specified complexity threshold, making it ideal for:\n\n- **CI/CD Pipelines**: Fail builds when complexity thresholds are exceeded\n- **Git Hooks**: Prevent commits with overly complex code\n- **Code Quality Gates**: Enforce complexity standards across teams\n\n```bash\n# Example: Fail if any function has complexity \u003e 15\nswift run SwiftComplexityCLI Sources --threshold 15 --recursive\n# Exit code 0: All functions below threshold\n# Exit code 1: One or more functions exceed threshold\n```\n\n## Supported Complexity Metrics\n\n### Function-level Metrics\n\n- **Cyclomatic Complexity**: Measures the number of linearly independent paths through code\n- **Cognitive Complexity**: Measures how difficult code is for humans to understand\n\n### Class-level Metrics\n\n- **LCOM4 (Lack of Cohesion of Methods)**: Measures class cohesion by analyzing method-property relationships\n  - **Connected Components**: Counts independent groups of related methods\n  - **High Precision**: 90-95% accuracy using IndexStore-DB semantic analysis\n  - **Implicit self Detection**: Automatically detects both `self.property` and `property` accesses\n  - **Requirements**: Requires `swift build` to generate index data\n\n## Documentation\n\n- **[User Guide](docs/user-guide/)**: Installation, usage, and examples\n- **[Complexity Metrics](docs/user-guide/complexity-metrics.md)**: Detailed metric explanations and examples\n- **[Output Formats](docs/user-guide/output-formats.md)**: JSON, XML, and text format specifications\n- **[Development Guide](docs/development/DEVELOPMENT.md)**: Setup for contributors\n- **[Debug Website](debug-website/)**: Web-based interactive analyzer documentation\n\n## Package Structure\n\nUnified package with multiple components:\n\n### Core Package\n\n- **SwiftComplexityCore**: Core analysis library (supports macOS 14+, iOS 13+)\n- **SwiftComplexityCLI**: Command-line interface\n- **SwiftComplexityPlugin**: Xcode Build Tool Plugin\n\n### Debug Website\n\n- **Frontend**: Next.js application deployed on Cloudflare Workers\n- **Backend**: Vapor 4 API containerized on Cloudflare Containers.\n- **Live Demo**: [swift-complexity.fummicc1.dev](https://swift-complexity.fummicc1.dev)\n\n## Usage Examples\n\n```bash\n# Analyze with verbose output\nswift run SwiftComplexityCLI Sources --verbose --recursive\n\n# Exclude test files with pattern matching\nswift run SwiftComplexityCLI Sources --recursive --exclude \"*Test*.swift\"\n\n# Show only cognitive complexity above threshold\nswift run SwiftComplexityCLI Sources --cognitive-only --threshold 5\n\n# Analyze class cohesion with LCOM4\nswift build  # Generate index first\nswift run SwiftComplexityCLI Sources --lcom4 --index-store-path .build/debug/index/store --format json\n```\n\n## Xcode Build Tool Plugin\n\nIntegrates with both Swift Package Manager and Xcode projects for automatic complexity analysis during builds.\n\n### Swift Package Manager Integration\n\n```swift\n// swift-tools-version: 6.2\nimport PackageDescription\n\nlet package = Package(\n    name: \"YourProject\",\n    dependencies: [\n        .package(url: \"https://github.com/fummicc1/swift-complexity.git\", from: \"1.0.0\")\n    ],\n    targets: [\n        .target(\n            name: \"YourTarget\",\n            plugins: [\n                .plugin(name: \"SwiftComplexityPlugin\", package: \"swift-complexity\")\n            ]\n        )\n    ]\n)\n```\n\n### Xcode Project Integration\n\n1. Add swift-complexity package to your Xcode project\n2. In Build Phases, add \"SwiftComplexityPlugin\" to Run Build Tool Plug-ins\n3. Configure threshold in Build Settings (optional)\n\n### Configuration\n\n**Xcode Build Settings:**\n\n- Key: `SWIFT_COMPLEXITY_THRESHOLD`\n- Value: `15` (or any number, defaults to 10)\n\n**Environment Variable (SPM):**\n\n- `SWIFT_COMPLEXITY_THRESHOLD=15`\n\n### Features\n\n- **Real-time feedback**: Complexity warnings appear directly in Xcode editor\n- **Accurate positioning**: Errors show at exact function locations\n- **Build integration**: Builds fail when thresholds are exceeded\n- **Configurable per target**: Different thresholds for different modules\n\n![Xcode Output](docs/imgs/xcode-output.png)\n\n## Output Examples\n\n### CLI Text Output\n\n```text\nFile: Sources/ComplexityAnalyzer.swift\n+------------------+----------+----------+\n| Function/Method  | Cyclo.   | Cogn.    |\n+------------------+----------+----------+\n| analyzeFunction  |    3     |    2     |\n| calculateTotal   |    5     |    7     |\n+------------------+----------+----------+\n\nTotal: 2 functions, Average Cyclomatic: 4.0, Average Cognitive: 4.5\n\nClass Cohesion (LCOM4):\n+------------------+----------+----------+----------+----------+\n| Class/Struct     | LCOM4    | Methods  | Props    | Level    |\n+------------------+----------+----------+----------+----------+\n| ComplexityAnalyzer|    1     |    5     |    3     | High     |\n| FileProcessor    |    2     |    8     |    4     | Moderate |\n+------------------+----------+----------+----------+----------+\n```\n\n### Xcode Diagnostics Output\n\n```text\n/path/to/Sources/MyFile.swift:45:1: error: Function 'complexFunction' has high complexity (Cyclomatic: 15, Cognitive: 23, Threshold: 10)\n/path/to/Sources/MyFile.swift:89:1: warning: Function 'anotherFunction' has high complexity (Cyclomatic: 12, Cognitive: 18, Threshold: 10)\n```\n\n## Requirements\n\n### CLI Tool\n\n- Swift 6.2+\n- macOS 14+ or Linux\n\n### Core Library\n\n- Swift 6.2+\n- macOS 14+, iOS 13+, or Linux\n\n### LCOM4 Feature (Optional)\n\n- **macOS 14+**: Xcode toolchain is auto-detected\n- **Linux**: Requires `--toolchain-path` option pointing to Swift toolchain\n- Project must be buildable with `swift build`\n- Index data at `.build/debug/index/store` (generated by build)\n\n**Linux Example:**\n\n```bash\n# Using Swiftly-installed toolchain\nTOOLCHAIN=~/.local/share/swiftly/toolchains/swift-6.2.2-RELEASE\nswift build \\\n  -Xcxx -I${TOOLCHAIN}/usr/lib/swift \\\n  -Xcxx -I${TOOLCHAIN}/usr/lib/swift/Block\n.build/debug/SwiftComplexityCLI Sources --lcom4 \\\n  --index-store-path .build/debug/index/store \\\n  --toolchain-path ${TOOLCHAIN}\n```\n\n## License\n\nMIT License\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffummicc1%2Fswift-complexity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffummicc1%2Fswift-complexity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffummicc1%2Fswift-complexity/lists"}