fix(phpstan): address phpstan identified type errors and refactor code
Refactor several PHP files to fix type-related errors and potential crash scenarios. These include:
- Checking if a method exists before attempting to call it
- Providing alternatives for default values where a procedure may not exist
- Ensuring variables are initialized properly regardless of whether procedures execute successfully or not
- Casting return types to match the return type in function comments
- Other significant changes to improve overall code stability and predictability.
Related: #149 (closed)