rules_style.py
- Pfad:
/var/www/tools/ki-protokoll/claude-hook/quality/rules_style.py - Namespace: claude-hook.quality
- Zeilen: 64 | Größe: 2,003 Bytes
- Geändert: 2025-12-25 16:56:44 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 98
- Dependencies: 90 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 7
- extends Rule
- use re
- use collections.Counter
- use typing.List
- use rule_base.Rule
- use rule_base.COMMON_NUMBERS
- use rule_base.MIGRATION_ALLOWLIST
Klassen 2
-
W5_1_RepeatedStringsclass Zeile 18 -
W5_2_MagicNumbersclass Zeile 38
Code
#!/usr/bin/env python3
"""
Style & DRY Rules (W5) - Don't Repeat Yourself.
Regeln gegen Code-Duplikation: wiederholte Strings und Magic Numbers.
"""
import re
from collections import Counter
from typing import List
from .rule_base import Rule, COMMON_NUMBERS, MIGRATION_ALLOWLIST
# =============================================================================
# W5: DRY - Don't Repeat Yourself
# =============================================================================
class W5_1_RepeatedStrings(Rule):
"""W5.1: Wiederholte String-Literale (> 3x)."""
def __init__(self):
super().__init__(allowlist=MIGRATION_ALLOWLIST)
def check(self, file_path: str, content: str) -> List[str]:
# Finde Strings > 20 Zeichen
string_literals = re.findall(r'"([^"]{20,})"', content)
counts = Counter(string_literals)
warnings = []
for s, count in counts.items():
if count > 3:
truncated = s[:40] + "..." if len(s) > 40 else s
warnings.append(f"W5.1: String repeated {count}x: '{truncated}' - consider constant.")
return warnings
class W5_2_MagicNumbers(Rule):
"""W5.2: Wiederholte Magic Numbers (> 2x)."""
def __init__(self):
super().__init__(allowlist=MIGRATION_ALLOWLIST)
def check(self, file_path: str, content: str) -> List[str]:
# Finde Zahlen (2+ Ziffern, nicht in Konstanten)
numbers = re.findall(r'(?<![A-Z_])(\d{2,})(?![A-Z_])', content)
warnings = []
for num in set(numbers):
if num not in COMMON_NUMBERS and numbers.count(num) > 2:
warnings.append(f"W5.2: Magic number {num} repeated {numbers.count(num)}x - consider constant.")
return warnings
# =============================================================================
# RULE COLLECTION
# =============================================================================
RULES = [
W5_1_RepeatedStrings(),
W5_2_MagicNumbers(),
]