refactoring_engine module

מדיניות וקונפיגורציה

המנוע מיישם קיבוץ לפי קוהזיה כדי למנוע Oversplitting ולהימנע מ-God Class:

  • קיבוץ לפי דומיין (io, helpers, compute) + תתי-קבוצות לפי prefix

  • מיזוג קבוצות קטנות לפי תלות (Affinity)

  • יעד מספר קבוצות: 3–5, עם סף מינימלי של 2 פונקציות בקבוצה

  • בסנריו של קובץ קטן עם פונקציה אחת בלבד אבל עם כותרות/מחלקות בסקשנים שונים, המנוע יוצר קבוצות סקאפולדינג לפי הסקשן כדי לשמר לפחות שני מודולי דומיין וכך לאפשר מנגנון מיזוג מעגלי הייבוא

שדות קונפיגורציה זמינים במחלקה RefactoringEngine:

class RefactoringEngine:
    preferred_min_groups: int = 3
    preferred_max_groups: int = 5
    absolute_max_groups: int = 8
    min_functions_per_group: int = 2

מקרה מיוחד: Safe Decomposition ל‑models.py

כאשר קובץ הקלט הוא models.py והוא כולל מחלקות בלבד (אין פונקציות טופ‑לבל), המנוע מבצע פיצול בטוח לתת‑מודולים דומייניים תחת models/:

  • סיווג מחלקות לדומיינים: core, billing, inventory (ולפי צורך network/workflows), על בסיס יוריסטיקות שם/סקשן.

  • יצירת קבצים: models/core.py, models/billing.py, models/inventory.py.

  • יצירת models/__init__.py עם re‑exports לשמירת תאימות (ייבוא מ‑models ימשיך לעבוד).

  • הזרקת יבוא בין‑מודולי למחלקות נדרשות (למשל: from .core import User בתוך billing.py).

  • Dry‑Run Tarjan SCC לזיהוי מעגליות בתוך models/ בלבד ומיזוג נקודתי במקרה של מעגל.

ממשק פנימי רלוונטי:

  • _split_models_monolith() – בונה את חבילת models/ והקבצים הדומייניים.

  • _inject_cross_module_class_imports() – הזרקת imports למחלקות בין מודולים פנימיים.

  • _resolve_circular_imports() – DRY‑Run לזיהוי ומיזוג נקודתי של מעגליות בייבוא.

מנוע רפקטורינג אוטומטי מבצע שינויי מבנה בקוד בצורה בטוחה

class refactoring_engine.RefactorType(value)[מקור]

Bases: Enum

סוגי רפקטורינג נתמכים

SPLIT_FUNCTIONS = 'split_functions'
EXTRACT_FUNCTIONS = 'extract_functions'
MERGE_SIMILAR = 'merge_similar'
CONVERT_TO_CLASSES = 'convert_to_classes'
DEPENDENCY_INJECTION = 'dependency_injection'
class refactoring_engine.FunctionInfo(name, start_line, end_line, args, returns, decorators, docstring, calls, called_by=<factory>, code='', complexity=0, section=None)[מקור]

Bases: object

מידע על פונקציה

פרמטרים:
name: str
start_line: int
end_line: int
args: List[str]
returns: Optional[str]
decorators: List[str]
docstring: Optional[str]
calls: Set[str]
called_by: Set[str]
code: str = ''
complexity: int = 0
section: Optional[str] = None
__init__(name, start_line, end_line, args, returns, decorators, docstring, calls, called_by=<factory>, code='', complexity=0, section=None)
פרמטרים:
Return type:

None

class refactoring_engine.ClassInfo(name, start_line, end_line, methods, attributes, base_classes, decorators, docstring, code='', section=None)[מקור]

Bases: object

מידע על מחלקה

פרמטרים:
name: str
start_line: int
end_line: int
methods: List[FunctionInfo]
attributes: List[str]
base_classes: List[str]
decorators: List[str]
docstring: Optional[str]
code: str = ''
section: Optional[str] = None
__init__(name, start_line, end_line, methods, attributes, base_classes, decorators, docstring, code='', section=None)
פרמטרים:
Return type:

None

class refactoring_engine.RefactorProposal(refactor_type, original_file, new_files, description, changes_summary, warnings=<factory>, imports_needed=<factory>)[מקור]

Bases: object

הצעת רפקטורינג

פרמטרים:
refactor_type: RefactorType
original_file: str
new_files: Dict[str, str]
description: str
changes_summary: List[str]
warnings: List[str]
imports_needed: Dict[str, List[str]]
__init__(refactor_type, original_file, new_files, description, changes_summary, warnings=<factory>, imports_needed=<factory>)
פרמטרים:
Return type:

None

class refactoring_engine.RefactorResult(success, proposal, error=None, validation_passed=False)[מקור]

Bases: object

תוצאת רפקטורינג

פרמטרים:
success: bool
proposal: Optional[RefactorProposal]
error: Optional[str] = None
validation_passed: bool = False
__init__(success, proposal, error=None, validation_passed=False)
פרמטרים:
Return type:

None

class refactoring_engine.CodeAnalyzer(code, filename='unknown.py')[מקור]

Bases: object

מנתח קוד Python

פרמטרים:
__init__(code, filename='unknown.py')[מקור]
פרמטרים:
analyze()[מקור]

ניתוח הקוד

Return type:

bool

find_large_functions(min_lines=50)[מקור]
Return type:

List[FunctionInfo]

פרמטרים:

min_lines (int)

find_large_classes(min_methods=10)[מקור]
Return type:

List[ClassInfo]

פרמטרים:

min_methods (int)

class refactoring_engine.RefactoringEngine[מקור]

Bases: object

מנוע רפקטורינג

CANONICAL_DOMAIN_PRIORITY: Dict[str, int] = {'finance': 1, 'inventory': 2, 'network': 3, 'users': 0, 'workflows': 4}
STICKY_SUPPORT_DOMAINS: Set[str] = {'helpers', 'utils'}
__init__()[מקור]
Return type:

None

propose_refactoring(code, filename, refactor_type, layered_mode=None)[מקור]

הצעת רפקטורינג

Return type:

RefactorResult

פרמטרים:
post_refactor_cleanup(files)[מקור]

שלב ניקוי לאחר רפקטורינג: נקיון imports לא בשימוש ברמת קובץ תוך שימור קבועים/הקצאות גלובליות. הערה: נמנעים מהרצת כלים חיצוניים (ruff/black) מסיבות תאימות סביבה.

Return type:

Dict[str, str]

פרמטרים:

files (Dict[str, str])