pre_rules_layers.py

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 5

Funktionen 6

Code

#!/usr/bin/env python3
"""
Pre-Hook Layer Regeln (BLOCK) - DIP + Architecture Guards.

P6.x Regeln: SOLID + DIP Layer-Violations
H-Regeln: Legacy Architecture Guards
"""

import re
from typing import Optional
from .rule_base import GLOBAL_ALLOWLIST, is_in_allowlist, block


# =============================================================================
# ALLOWLIST
# =============================================================================

FACTORY_ALLOWLIST = [
    "/Factory/",
    "/Bootstrap/",
]


# =============================================================================
# PRÜFUNG 6: SOLID + DIP
# =============================================================================

def p6_1_application_no_infrastructure_import(file_path: str, content: str) -> Optional[dict]:
    """P6.1: UseCases/Application darf Infrastructure nicht importieren."""
    if "/UseCases/" not in file_path and "/Application/" not in file_path:
        return None
    if is_in_allowlist(file_path, GLOBAL_ALLOWLIST):
        return None

    if re.search(r"use\s+Infrastructure\\", content):
        return block("P6.1", "Application layer must not import Infrastructure directly (DIP)")

    return None


def p6_2_domain_no_application_import(file_path: str, content: str) -> Optional[dict]:
    """P6.2: Domain darf Application-Layer nicht importieren."""
    if "/Domain/" not in file_path:
        return None
    if is_in_allowlist(file_path, GLOBAL_ALLOWLIST):
        return None

    if re.search(r"use\s+(UseCases|Application)\\", content):
        return block("P6.2", "Domain must not import Application layer (layer violation)")

    return None


# =============================================================================
# LEGACY H-REGELN (aus architecture_guard.py)
# =============================================================================

def h2_domain_no_infrastructure(file_path: str, content: str) -> Optional[dict]:
    """H2: Domain ohne Infrastructure."""
    if "/Domain/" not in file_path:
        return None
    if is_in_allowlist(file_path, GLOBAL_ALLOWLIST):
        return None

    if re.search(r"use\s+Infrastructure\\", content):
        return block("H2", "Domain must not use Infrastructure")

    return None


def h3_database_factory_only_in_factory(file_path: str, content: str) -> Optional[dict]:
    """H3: DatabaseFactory nur in Factory."""
    if is_in_allowlist(file_path, GLOBAL_ALLOWLIST + FACTORY_ALLOWLIST):
        return None

    if re.search(r"DatabaseFactory::", content):
        return block("H3", "DatabaseFactory only allowed in Factory classes")

    return None


def h4_no_new_repository_in_controller(file_path: str, content: str) -> Optional[dict]:
    """H4: Kein new Repository in Controller."""
    if "/Controller/" not in file_path:
        return None
    if is_in_allowlist(file_path, GLOBAL_ALLOWLIST):
        return None

    if re.search(r"new\s+\w+Repository\s*\(", content):
        return block("H4", "new Repository in Controller not allowed. Use DI.")

    return None


def h5_no_new_infrastructure_in_controller(file_path: str, content: str) -> Optional[dict]:
    """H5: Kein new Infrastructure in Controller."""
    if "/Controller/" not in file_path:
        return None
    if is_in_allowlist(file_path, GLOBAL_ALLOWLIST):
        return None

    if re.search(r"new\s+Infrastructure\\", content):
        return block("H5", "new Infrastructure in Controller not allowed. Use DI.")

    return None


# =============================================================================
# RULE COLLECTION
# =============================================================================

RULES = [
    # Prüfung 6: SOLID + DIP
    p6_1_application_no_infrastructure_import,
    p6_2_domain_no_application_import,

    # Legacy H-Regeln
    h2_domain_no_infrastructure,
    h3_database_factory_only_in_factory,
    h4_no_new_repository_in_controller,
    h5_no_new_infrastructure_in_controller,
]
← Übersicht