Skip to content

Commit

Permalink
Improve check for SY-SYSID usage (#1126)
Browse files Browse the repository at this point in the history
Co-authored-by: abaplint[bot] <24845621+abaplint[bot]@users.noreply.github.com>
  • Loading branch information
ConjuringCoffee and abaplint[bot] authored Jan 10, 2024
1 parent 6c00e5d commit 1b13452
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 160 deletions.
4 changes: 2 additions & 2 deletions docs/_checks/102.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: Use of system ID
title: Use of SY-SYSID
cNumber: CHECK_102
rfc: true
index: 102
---

Coupling your logic to the name of the SAP system (`SY-SYSID`) is generally a bad idea. Use this check to find all uses of the system ID.
Coupling your logic to the ID of the SAP system (`SY-SYSID`) is generally a bad idea. Use this check to find all uses of the system ID.
129 changes: 86 additions & 43 deletions src/checks/zcl_aoc_check_102.clas.abap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"! <p class="shorttext synchronized">102 - Use of system ID</p>
"! <p class="shorttext synchronized">102 - Use of SY-SYSID</p>
CLASS zcl_aoc_check_102 DEFINITION
PUBLIC
INHERITING FROM zcl_aoc_super
Expand All @@ -8,77 +8,120 @@ CLASS zcl_aoc_check_102 DEFINITION
METHODS constructor.

METHODS check REDEFINITION.

PRIVATE SECTION.
TYPES:
BEGIN OF ty_s_error_code_map,
usage_kind TYPE zcl_aoc_sy_variable_analyzer=>ty_v_usage_kind,
error_code TYPE sci_errc,
text TYPE ty_scimessage_text,
END OF ty_s_error_code_map.

DATA mt_error_code_map TYPE TABLE OF ty_s_error_code_map.

METHODS is_using_only_first_letter
IMPORTING
is_token TYPE stokesx
RETURNING
VALUE(rv_result) TYPE abap_bool.
ENDCLASS.


CLASS zcl_aoc_check_102 IMPLEMENTATION.
METHOD constructor.
super->constructor( ).

version = '001'.
version = '002'.
position = '102'.

has_attributes = abap_true.
attributes_ok = abap_true.

enable_rfc( ).

insert_scimessage( iv_code = gc_code-usage_uncategorized
iv_text = TEXT-001 ).
insert_scimessage( iv_code = gc_code-in_condition
iv_text = TEXT-002 ).
mt_error_code_map = VALUE #( ( usage_kind = zcl_aoc_sy_variable_analyzer=>gc_usage_kind-usage_uncategorized
error_code = gc_code-usage_uncategorized
text = TEXT-001 )
( usage_kind = zcl_aoc_sy_variable_analyzer=>gc_usage_kind-in_condition
error_code = gc_code-in_condition
text = TEXT-002 )
( usage_kind = zcl_aoc_sy_variable_analyzer=>gc_usage_kind-as_default_value
error_code = gc_code-as_default_value
text = TEXT-004 )
( usage_kind = zcl_aoc_sy_variable_analyzer=>gc_usage_kind-in_concatenate
error_code = gc_code-in_concatenate
text = TEXT-005 )
( usage_kind = zcl_aoc_sy_variable_analyzer=>gc_usage_kind-overridden
error_code = gc_code-overridden
text = TEXT-006 )
( usage_kind = zcl_aoc_sy_variable_analyzer=>gc_usage_kind-assigned_to_variable
error_code = gc_code-assigned_to_variable
text = TEXT-007 )
( usage_kind = zcl_aoc_sy_variable_analyzer=>gc_usage_kind-in_database_select
error_code = gc_code-in_database_select
text = TEXT-008 )
( usage_kind = zcl_aoc_sy_variable_analyzer=>gc_usage_kind-in_write
error_code = gc_code-in_write
text = TEXT-009 )
( usage_kind = zcl_aoc_sy_variable_analyzer=>gc_usage_kind-in_message
error_code = gc_code-in_message
text = TEXT-010 )
( usage_kind = zcl_aoc_sy_variable_analyzer=>gc_usage_kind-within_macro
error_code = gc_code-within_macro
text = TEXT-011 ) ).

LOOP AT mt_error_code_map ASSIGNING FIELD-SYMBOL(<ls_error_code_map>).
insert_scimessage( iv_code = <ls_error_code_map>-error_code
iv_text = <ls_error_code_map>-text ).
ENDLOOP.

insert_scimessage( iv_code = gc_code-first_letter_used
iv_text = TEXT-003 ).
insert_scimessage( iv_code = gc_code-as_default_value
iv_text = TEXT-004 ).
insert_scimessage( iv_code = gc_code-in_concatenate
iv_text = TEXT-005 ).
insert_scimessage( iv_code = gc_code-overridden
iv_text = TEXT-006 ).
insert_scimessage( iv_code = gc_code-assigned_to_variable
iv_text = TEXT-007 ).
insert_scimessage( iv_code = gc_code-in_database_select
iv_text = TEXT-008 ).
insert_scimessage( iv_code = gc_code-in_write
iv_text = TEXT-009 ).
insert_scimessage( iv_code = gc_code-in_message
iv_text = TEXT-010 ).
insert_scimessage( iv_code = gc_code-within_macro
iv_text = TEXT-011 ).
ENDMETHOD.

METHOD check.
" abapOpenChecks
" https://github.com/larshp/abapOpenChecks
" MIT License

DATA(lo_helper) = NEW lcl_check_helper( io_scan ).
DATA(lo_variable_analyzer) = NEW zcl_aoc_sy_variable_analyzer( io_scan ).

DATA(lt_variable_usage) = lo_variable_analyzer->analyze_variable_usage( 'SYSID' ).

LOOP AT lt_variable_usage ASSIGNING FIELD-SYMBOL(<ls_variable_usage>).
DATA(lv_error_code) = VALUE sci_errc( ).

LOOP AT io_scan->statements ASSIGNING FIELD-SYMBOL(<ls_statement>).
LOOP AT io_scan->tokens ASSIGNING FIELD-SYMBOL(<ls_token>)
FROM <ls_statement>-from TO <ls_statement>-to
WHERE str CP 'SY-SYSID*'
OR str CP '@SY-SYSID*'.
ASSIGN mt_error_code_map[ usage_kind = <ls_variable_usage>-usage_kind ] TO FIELD-SYMBOL(<ls_error_code_map>).

DATA(lv_index_token) = sy-tabix.
IF sy-subrc <> 0.
" Ignore this usage kind
CONTINUE.
ENDIF.

DATA(lv_error_code) = lo_helper->determine_error_code( is_token = <ls_token>
iv_index_token = lv_index_token
is_statement = <ls_statement> ).
lv_error_code = <ls_error_code_map>-error_code.

IF lv_error_code IS INITIAL.
" No error
CONTINUE.
ENDIF.
ASSIGN io_scan->tokens[ <ls_variable_usage>-token_index ] TO FIELD-SYMBOL(<ls_token>).

DATA(lv_include) = io_scan->get_include( <ls_statement>-level ).
IF is_using_only_first_letter( <ls_token> ) = abap_true.
" This is more interesting than where it is used
lv_error_code = gc_code-first_letter_used.
ENDIF.

DATA(lv_include) = io_scan->get_include( <ls_variable_usage>-statement_level ).

inform( p_sub_obj_name = lv_include
p_line = <ls_token>-row
p_kind = mv_errty
p_test = myname
p_code = lv_error_code ).

inform( p_sub_obj_name = lv_include
p_line = <ls_token>-row
p_kind = mv_errty
p_test = myname
p_code = lv_error_code ).
ENDLOOP.
ENDLOOP.
ENDMETHOD.

METHOD is_using_only_first_letter.
rv_result = SWITCH #( is_token-str
WHEN `SY-SYSID+0(1)` OR `SY-SYSID(1)` OR `@SY-SYSID+0(1)` OR `@SY-SYSID(1)`
THEN abap_true
ELSE abap_false ).
ENDMETHOD.
ENDCLASS.
31 changes: 0 additions & 31 deletions src/checks/zcl_aoc_check_102.clas.locals_def.abap
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,3 @@ CONSTANTS:
in_message TYPE sci_errc VALUE '010',
within_macro TYPE sci_errc VALUE '011',
END OF gc_code.

CLASS lcl_check_helper DEFINITION.

PUBLIC SECTION.
METHODS constructor
IMPORTING
io_scan TYPE REF TO zcl_aoc_scan.

METHODS determine_error_code
IMPORTING
is_token TYPE stokesx
iv_index_token TYPE sy-tabix
is_statement TYPE sstmnt
RETURNING
VALUE(rv_error_code) TYPE sci_errc.

PRIVATE SECTION.
DATA mo_scan TYPE REF TO zcl_aoc_scan.

METHODS is_using_only_first_letter
IMPORTING
is_token TYPE stokesx
RETURNING
VALUE(rv_result) TYPE abap_bool.

METHODS is_used_in_macro
IMPORTING
is_statement TYPE sstmnt
RETURNING
VALUE(rv_result) TYPE abap_bool.
ENDCLASS.
82 changes: 0 additions & 82 deletions src/checks/zcl_aoc_check_102.clas.locals_imp.abap

This file was deleted.

19 changes: 18 additions & 1 deletion src/checks/zcl_aoc_check_102.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CLASS ltcl_test DEFINITION
METHODS if_condition_01 FOR TESTING.
METHODS first_letter_01 FOR TESTING.
METHODS first_letter_02 FOR TESTING.
METHODS first_letter_03 FOR TESTING.
METHODS if_condition_02 FOR TESTING.
METHODS if_condition_03 FOR TESTING.
METHODS case_condition_01 FOR TESTING.
Expand Down Expand Up @@ -52,7 +53,6 @@ CLASS ltcl_test DEFINITION
METHODS message FOR TESTING.
METHODS check_condition FOR TESTING.
METHODS assert_condition FOR TESTING.

ENDCLASS.


Expand Down Expand Up @@ -119,6 +119,23 @@ CLASS ltcl_test IMPLEMENTATION.
assert_error_code( gc_code-first_letter_used ).
ENDMETHOD.

METHOD first_letter_03.
" Given
_code `SELECT host`.
_code ` FROM ztable`.
_code ` UP TO 1 ROWS`.
_code ` INTO @DATA(lv_host)`.
_code ` WHERE sysname = @sy-sysid+0(1)`.
_code ` ORDER BY PRIMARY KEY.`.
_code `ENDSELECT.`.

" When
execute_check( ).

" Then
assert_error_code( gc_code-first_letter_used ).
ENDMETHOD.

METHOD if_condition_02.
" Given
_code `IF 1 = 2 OR sy-sysid = 'PRD'.`.
Expand Down
2 changes: 1 addition & 1 deletion src/checks/zcl_aoc_check_102.clas.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<VSEOCLASS>
<CLSNAME>ZCL_AOC_CHECK_102</CLSNAME>
<LANGU>E</LANGU>
<DESCRIPT>102 - Use of system ID</DESCRIPT>
<DESCRIPT>102 - Use of SY-SYSID</DESCRIPT>
<STATE>1</STATE>
<CLSCCINCL>X</CLSCCINCL>
<FIXPT>X</FIXPT>
Expand Down
Loading

0 comments on commit 1b13452

Please sign in to comment.