Skip to content

feature/detect-magic-numbers#72

Open
aliyahnurdafika wants to merge 5 commits intodevfrom
feature/detect-magic-numbers
Open

feature/detect-magic-numbers#72
aliyahnurdafika wants to merge 5 commits intodevfrom
feature/detect-magic-numbers

Conversation

@aliyahnurdafika
Copy link
Copy Markdown
Collaborator

@aliyahnurdafika aliyahnurdafika commented Feb 15, 2026

Update

The current implementation has been refactored since the original PR description.

The Magic Number detector is now implemented under:
src/magic_number/

The previous implementation under:
src/violations/
has been removed.

For review purposes, please focus on the following modules:

src/magic_number/
    detect_magic_number.py      → main detection engine
    magic_number_filter.py      → filtering rules
    magic_number_patterns.py    → configurations
    magic_number_runner.py      → repository test runner

test/test_magic_number_unit.py  → unit test

Summary

This PR introduces a magic Number detector integrated with the full-file PR analysis pipeline.

Instead of analyzing patch snippets only, the system retrieves the full file content
at the PR head commit and performs numeric literal detection across the entire file.

The detector identifies numeric literals that are not part of:
• Constant definitions
• Known safe literals
• Styling / presentation contexts
• Regex / string / comment content

and reports them as Magic Number violations for code quality analysis.

Files Added/Updated

  1. src/extractors/pull_request_extractor.py: Updates PR data extraction to support full-file analysis.
  • Scans the PR branch.
  • Retrieves PR metadata (PR id, head SHA, changed files).
  • For each modified file, calls extract_file_content_at_ref(...).
  • Fetches full file content at the PR's head commit.
  • Returns file content as a UTF-8 string.
  1. scripts/enrich_full_files.py: Implements full-file enrichment for PR-level data.
  • Uses the extractor to retrieve complete file contents at PR head.
  • Attaches full file content to each PR file record.
  • Produces enriched CSV data for analysis.
  1. src/magic_number/detect_magic_number.py
    Main detection engine that scans source code line-by-line and identifies numeric literals that may represent Magic Numbers.

  2. src/magic_number/magic_number_filter.py
    Context filtering utilities to eliminate false positives including:
    • Comments
    • Strings
    • Regex literals
    • Styling / presentation contexts
    • HTTP status codes
    • Object key numbers
    • CSS unit values

  3. src/magic_number/magic_number_patterns.py
    Defines numeric literal patterns, safe literals, and language mappings.

  4. src/magic_number/magic_number_runner.py
    A repository runner that reads enriched PR data and applies the detector
    to generate violation reports.

  5. test/test_magic_number_unit.py
    Unit tests validating detection correctness and filtering behavior.

Unit test results:
35 tests passed.

@aliyahnurdafika aliyahnurdafika changed the title Add Magic Number Detection with rule registry, unit tests, team test, and full-file contents integration feature/detect-magic-numbers Mar 8, 2026
@d2r3v d2r3v force-pushed the dev branch 2 times, most recently from a8e6d00 to d14e5b5 Compare March 16, 2026 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant