Skip to content

Commit

Permalink
Add check 102 to detect SY-SYSID usage (#1124)
Browse files Browse the repository at this point in the history
Co-authored-by: Lars Hvam <[email protected]>
  • Loading branch information
ConjuringCoffee and larshp authored Jan 5, 2024
1 parent 410c25b commit 67e6abc
Show file tree
Hide file tree
Showing 7 changed files with 821 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/_checks/102.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Use of system ID
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.
84 changes: 84 additions & 0 deletions src/checks/zcl_aoc_check_102.clas.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"! <p class="shorttext synchronized">102 - Use of system ID</p>
CLASS zcl_aoc_check_102 DEFINITION
PUBLIC
INHERITING FROM zcl_aoc_super
FINAL.

PUBLIC SECTION.
METHODS constructor.

METHODS check REDEFINITION.
ENDCLASS.


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

version = '001'.
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 ).
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 ).

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*'.

DATA(lv_index_token) = sy-tabix.

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

IF lv_error_code IS INITIAL.
" No error
CONTINUE.
ENDIF.

DATA(lv_include) = io_scan->get_include( <ls_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 ).
ENDLOOP.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
45 changes: 45 additions & 0 deletions src/checks/zcl_aoc_check_102.clas.locals_def.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
CONSTANTS:
BEGIN OF gc_code,
usage_uncategorized TYPE sci_errc VALUE '001',
in_condition TYPE sci_errc VALUE '002',
first_letter_used TYPE sci_errc VALUE '003',
as_default_value TYPE sci_errc VALUE '004',
in_concatenate TYPE sci_errc VALUE '005',
overridden TYPE sci_errc VALUE '006',
assigned_to_variable TYPE sci_errc VALUE '007',
in_database_select TYPE sci_errc VALUE '008',
in_write TYPE sci_errc VALUE '009',
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: 82 additions & 0 deletions src/checks/zcl_aoc_check_102.clas.locals_imp.abap
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
CLASS lcl_check_helper IMPLEMENTATION.
METHOD constructor.
mo_scan = io_scan.
ENDMETHOD.

METHOD determine_error_code.
IF is_using_only_first_letter( is_token ) = abap_true.
rv_error_code = gc_code-first_letter_used.
RETURN.
ENDIF.

IF is_used_in_macro( is_statement ) = abap_true.
rv_error_code = gc_code-within_macro.
RETURN.
ENDIF.

ASSIGN mo_scan->tokens[ is_statement-from ] TO FIELD-SYMBOL(<ls_first_token_of_statement>).

CASE <ls_first_token_of_statement>-str.
WHEN zcl_aoc_scan=>gc_keyword-types
OR zcl_aoc_scan=>gc_keyword-ranges.
" Ignore
RETURN.
WHEN zcl_aoc_scan=>gc_keyword-concatenate.
rv_error_code = gc_code-in_concatenate.
WHEN is_token-str.
rv_error_code = gc_code-overridden.
WHEN zcl_aoc_scan=>gc_keyword-select.
rv_error_code = gc_code-in_database_select.
WHEN zcl_aoc_scan=>gc_keyword-write.
rv_error_code = gc_code-in_write.
WHEN zcl_aoc_scan=>gc_keyword-message.
rv_error_code = gc_code-in_message.
WHEN zcl_aoc_scan=>gc_keyword-if
OR zcl_aoc_scan=>gc_keyword-elseif
OR zcl_aoc_scan=>gc_keyword-case
OR zcl_aoc_scan=>gc_keyword-when
OR zcl_aoc_scan=>gc_keyword-check
OR zcl_aoc_scan=>gc_keyword-assert.
rv_error_code = gc_code-in_condition.
WHEN zcl_aoc_scan=>gc_keyword-methods
OR zcl_aoc_scan=>gc_keyword-class_methods
OR zcl_aoc_scan=>gc_keyword-data
OR zcl_aoc_scan=>gc_keyword-class_data.
ASSIGN mo_scan->tokens[ iv_index_token - 1 ] TO FIELD-SYMBOL(<ls_token_before_sysid>).

CASE <ls_token_before_sysid>-str.
WHEN zcl_aoc_scan=>gc_keyword-default.
rv_error_code = gc_code-as_default_value.
WHEN zcl_aoc_scan=>gc_keyword-like
OR zcl_aoc_scan=>gc_keyword-type.
" Ignore
RETURN.
ENDCASE.
WHEN OTHERS.
READ TABLE mo_scan->tokens ASSIGNING <ls_token_before_sysid> INDEX iv_index_token - 1.

IF <ls_token_before_sysid>-str = '='
AND iv_index_token - is_statement-from = 2.
rv_error_code = gc_code-assigned_to_variable.
ELSE.
rv_error_code = gc_code-usage_uncategorized.
ENDIF.
ENDCASE.
ENDMETHOD.

METHOD is_using_only_first_letter.
CASE is_token-str.
WHEN 'SY-SYSID+0(1)' OR 'SY-SYSID(1)'.
rv_result = abap_true.
ENDCASE.
ENDMETHOD.

METHOD is_used_in_macro.
ASSIGN mo_scan->tokens[ is_statement-to + 1 ] TO FIELD-SYMBOL(<ls_token_after_statement>).

IF sy-subrc = 0
AND <ls_token_after_statement>-str = zcl_aoc_scan=>gc_keyword-end_of_definition.
rv_result = abap_true.
ENDIF.
ENDMETHOD.
ENDCLASS.
Loading

0 comments on commit 67e6abc

Please sign in to comment.