@@ -7,21 +7,21 @@ CLASS y_check_prefer_case_to_elseif DEFINITION PUBLIC INHERITING FROM y_check_ba
7
7
METHODS inspect_tokens REDEFINITION .
8
8
9
9
PRIVATE SECTION .
10
- TYPES : BEGIN OF counter ,
10
+ TYPES : BEGIN OF ty_counter ,
11
11
if_structure TYPE sstruc,
12
12
if_statement TYPE sstmnt,
13
13
condition TYPE string ,
14
14
count TYPE i ,
15
- END OF counter.
16
- TYPES counters TYPE TABLE OF counter.
15
+ END OF ty_counter.
16
+
17
+ TYPES ty_counters TYPE TABLE OF ty_counter.
18
+
19
+ DATA counters TYPE ty_counters.
17
20
18
21
METHODS has_multiple_conditions IMPORTING statement TYPE sstmnt
19
22
RETURNING VALUE (result ) TYPE abap_bool .
20
23
21
- METHODS should_skip_test_code IMPORTING structure TYPE sstruc
22
- RETURNING VALUE (result ) TYPE abap_bool .
23
-
24
- METHODS handle_result IMPORTING counters TYPE counters.
24
+ METHODS handle_result.
25
25
26
26
ENDCLASS .
27
27
@@ -37,69 +37,54 @@ CLASS y_check_prefer_case_to_elseif IMPLEMENTATION.
37
37
settings-threshold = 5 .
38
38
settings-documentation = | { c_docs_path-checks } prefer-case-to-elseif.md| .
39
39
40
+ relevant_statement_types = VALUE #( ).
41
+
42
+ relevant_structure_types = VALUE #( ( scan_struc_type-condition )
43
+ ( scan_struc_type-alternation ) ).
44
+
40
45
set_check_message( 'Prefer CASE to ELSE IF for multiple alternative conditions!' ).
41
46
ENDMETHOD .
42
47
43
48
44
49
METHOD execute_check .
45
- DATA counters TYPE counters.
46
-
47
- DATA (structures ) = ref_scan_manager->get_structures( ).
48
- DATA (statements ) = ref_scan_manager->get_statements( ).
49
-
50
- LOOP AT structures ASSIGNING FIELD-SYMBOL (<structure> )
51
- WHERE type = scan_struc_type-condition
52
- OR type = scan_struc_type-alternation.
53
-
54
- IF should_skip_test_code( <structure> ) = abap_true .
55
- CONTINUE .
56
- ENDIF .
57
-
58
- DATA (statement ) = statements[ <structure> -stmnt_from ].
59
-
60
- IF has_multiple_conditions( statement ) = abap_true .
61
- CONTINUE .
62
- ENDIF .
63
-
64
- DATA (token ) = get_token_abs( statement-from ).
65
-
66
- DATA (if_structure ) = COND #( WHEN token = 'IF' THEN <structure>
67
- WHEN token = 'ELSEIF' THEN structures[ <structure> -back ] ).
68
-
69
- IF if_structure IS INITIAL .
70
- CONTINUE .
71
- ENDIF .
50
+ super ->execute_check( ).
51
+ handle_result( ).
52
+ ENDMETHOD .
72
53
73
- DATA (condition ) = get_token_abs( statement-from + 1 ).
74
54
75
- TRY .
76
- counters[ if_structure = if_structure
77
- condition = condition ]-count = counters[ if_structure = if_structure
78
- condition = condition ]-count + 1 .
79
- CATCH cx_sy_itab_line_not_found.
80
- counters = VALUE #( BASE counters
81
- ( if_structure = if_structure
82
- if_statement = statements[ if_structure-stmnt_from ]
83
- condition = condition
84
- count = 1 ) ).
85
- ENDTRY .
55
+ METHOD inspect_tokens .
86
56
87
- ENDLOOP .
57
+ " Only the first statement is relevant
58
+ CHECK structure-stmnt_from = index .
88
59
89
- handle_result( counters ).
90
-
91
- ENDMETHOD .
60
+ DATA (if_statement ) = ref_scan_manager->statements[ structure-stmnt_from ].
92
61
62
+ IF has_multiple_conditions( if_statement ) = abap_true .
63
+ RETURN .
64
+ ENDIF .
93
65
94
- METHOD inspect_tokens .
95
- RETURN .
96
- ENDMETHOD .
66
+ DATA (if_token ) = get_token_abs( if_statement-from ).
97
67
68
+ DATA (if_structure ) = COND #( WHEN if_token = 'IF' THEN structure
69
+ WHEN if_token = 'ELSEIF' THEN ref_scan_manager->structures[ structure-back ] ).
98
70
99
- METHOD should_skip_test_code .
100
- CHECK test_code_detector->is_testcode( structure ) = abap_true .
101
- CHECK line_exists ( check_configurations[ apply_on_testcode = abap_false ] ).
102
- result = abap_true .
71
+ IF if_structure IS INITIAL .
72
+ RETURN .
73
+ ENDIF .
74
+
75
+ DATA (condition ) = get_token_abs( if_statement-from + 1 ).
76
+
77
+ TRY .
78
+ counters[ if_structure = if_structure
79
+ condition = condition ]-count = counters[ if_structure = if_structure
80
+ condition = condition ]-count + 1 .
81
+ CATCH cx_sy_itab_line_not_found.
82
+ counters = VALUE #( BASE counters
83
+ ( if_structure = if_structure
84
+ if_statement = if_statement
85
+ condition = condition
86
+ count = 1 ) ).
87
+ ENDTRY .
103
88
ENDMETHOD .
104
89
105
90
@@ -120,13 +105,15 @@ CLASS y_check_prefer_case_to_elseif IMPLEMENTATION.
120
105
ENDLOOP .
121
106
ENDMETHOD .
122
107
108
+
123
109
METHOD has_multiple_conditions .
124
- LOOP AT ref_scan_manager->get_tokens( ) ASSIGNING FIELD-SYMBOL (<token> )
110
+ LOOP AT ref_scan_manager->tokens ASSIGNING FIELD-SYMBOL (<token> )
125
111
FROM statement-from TO statement-to
126
112
WHERE str = 'AND' OR str = 'OR' .
127
113
result = abap_true .
128
114
RETURN .
129
115
ENDLOOP .
130
116
ENDMETHOD .
131
117
118
+
132
119
ENDCLASS .
0 commit comments