-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimized.py
58 lines (53 loc) · 1.56 KB
/
optimized.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import ast
class AntiPatternDetector(ast.NodeVisitor):
def __init__(self, threshold: int = 10):
self.threshold = threshold
self.god_objects = []
def visit_ClassDef(self, node: ast.ClassDef) -> None:
method_count = sum(isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)) for child in node.body)
if method_count > self.threshold:
self.god_objects.append({
'name': node.name,
'lineno': node.lineno,
'col_offset': node.col_offset
})
def detect_and_report_anti_patterns(code: str, threshold: int = 10) -> None:
try:
tree = ast.parse(code)
detector = AntiPatternDetector(threshold)
detector.visit(tree)
if detector.god_objects:
print("Detected anti-patterns:")
for pattern in detector.god_objects:
print(f"- God Object: {pattern['name']} (line {pattern['lineno']}, column {pattern['col_offset']})")
else:
print("No anti-patterns detected.")
except Exception as e:
print(f"Error: {e}")
# example usage
code = """
class GodObject:
def method1(self):
pass
def method2(self):
pass
def method3(self):
pass
def method4(self):
pass
def method5(self):
pass
def method6(self):
pass
def method7(self):
pass
def method8(self):
pass
def method9(self):
pass
def method10(self):
pass
def method11(self):
pass
"""
detect_and_report_anti_patterns(code)