Troubleshooting of Sonar issue
Jump to navigation
Jump to search
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 "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
- PHP: Exceptions - Manual
- PHP Exceptions 種類與使用情境說明 | Asika Lab 飛鳥實驗室
- Single-responsibility principle - Wikipedia
References
Troubleshooting of ...
- PHP, cUrl, Python, selenium, HTTP status code errors
- Database: SQL syntax debug, MySQL errors, MySQLTuner errors or PostgreSQL errors
- HTML/Javascript: Troubleshooting of javascript, XPath
- Software: Mediawiki, Docker, FTP problems, online conference software
- Test connectivity for the web service, Web Ping, Network problem, Web user behavior, Web scrape troubleshooting
Template