Troubleshooting of Sonar issue

From LemonWiki共筆
Revision as of 17:59, 7 September 2023 by Unknown user (talk)
Jump to navigation Jump to search


icon_scale_pencil.png This article "Troubleshooting of Sonar issue" is still being written. If there are any incomplete parts, you are welcome to directly edit them. 這篇文章「Troubleshooting of Sonar issue」內容還在撰寫中,如果有不完整的部分,歡迎你直接動手修改


How to resolve "Define and throw a dedicated exception instead of using a generic one"

Error condition which met "Define and throw a dedicated exception instead of using a generic one" [1]

if(is_null($some_variable)){
            $error = 'The variable $some_variable is not defined.';
            throw new Exception($error);
        }

Possible solution

if(!is_null($some_variable)){
            $error = 'The variable $some_variable is not defined.';
            throw new InvalidArgumentException($error);
        }

How to resolve "replace all tab characters in this file by sequences of white-spaces (Tabulation characters should not be used)"

Solution: Using the editor which supports regular expression[2]

  • Replace \t
  • with (four whitespaces)

How to resolve "Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed."

Possible solution

  • Created smaller, more specific functions (following Single Responsibility Principle) to handle specific parts of the logic.
  • Use these functions to make the main function easier to read and understand.

Further reading

References


Troubleshooting of ...

Template