-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanti-refractoring.py
80 lines (73 loc) · 2.25 KB
/
anti-refractoring.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# anti_refactor.py
import random
import ast
def introduce_god_object(code):
"""Introduce a God Object anti-pattern"""
tree = ast.parse(code)
class_def = tree.body[0]
for i in range(10): # add 10 random methods
method_name = f"method_{i}"
method_def = ast.FunctionDef(
name=method_name,
args=ast.arguments(
args=[],
vararg=None,
kwonlyargs=[]
),
body=[ast.Pass()],
decorator_list=[]
)
class_def.body.append(method_def)
return ast.unparse(tree)
def add_duplicate_code(code):
"""Add duplicate code blocks"""
tree = ast.parse(code)
func_def = tree.body[0]
duplicate_code = ast.parse("x = 5\ny = 10").body
func_def.body.extend(duplicate_code)
return ast.unparse(tree)
def create_long_method(code):
"""Create a long method"""
tree = ast.parse(code)
func_def = tree.body[0]
long_method_body = [ast.Expr(ast.Num(n)) for n in range(100)]
func_def.body.extend(long_method_body)
return ast.unparse(tree)
def add_switch_statement_with_many_cases(code):
"""Add a switch statement with many cases"""
tree = ast.parse(code)
func_def = tree.body[0]
switch_stmt = ast.parse("x = 5\nif x == 1: pass\nelif x == 2: pass\n...").body[0]
for i in range(10): # add 10 cases
case_clause = ast.IfExp(
test=ast.Compare(
left=ast.Name(id="x", ctx=ast.Load()),
ops=[ast.Eq()],
comparators=[ast.Num(i)]
),
body=ast.Pass(),
orelse=None
)
switch_stmt.orelse = case_clause
func_def.body.append(switch_stmt)
return ast.unparse(tree)
def anti_refactor(code):
"""Apply anti-refactoring techniques to the code"""
techniques = [
introduce_god_object,
add_duplicate_code,
create_long_method,
add_switch_statement_with_many_cases
]
for _ in range(random.randint(1, 3)): # apply 1-3 techniques
technique = random.choice(techniques)
code = technique(code)
return code
# example usage
code = """
def my_function():
x = 5
y = 10
"""
anti_refactored_code = anti_refactor(code)
print(anti_refactored_code)