pre_rules_layers.py
- Pfad:
/var/www/tools/ki-protokoll/claude-hook/quality/pre_rules_layers.py - Namespace: claude-hook.quality
- Zeilen: 123 | Größe: 3,983 Bytes
- Geändert: 2025-12-25 16:59:36 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 100
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 5
- use re
- use typing.Optional
- use rule_base.GLOBAL_ALLOWLIST
- use rule_base.is_in_allowlist
- use rule_base.block
Funktionen 6
-
p6_1_application_no_infrastructure_import()Zeile 28 -
p6_2_domain_no_application_import()Zeile 41 -
h2_domain_no_infrastructure()Zeile 58 -
h3_database_factory_only_in_factory()Zeile 71 -
h4_no_new_repository_in_controller()Zeile 82 -
h5_no_new_infrastructure_in_controller()Zeile 95
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,
]