Recently, I encountered a bug in my PHP code while I was making use of
iyzico's payment API. The error message was
Fatal error: Uncaught TypeError: Iyzipay\RequestStringBuilder::appendArray(): Argument #2 ($array) must be of type ?array, Iyzipay\Model\BasketItem given. The root cause was a couple of calls away from appendArray which threw the error: I was passing an object of type BasketItem as input to the
CreateCheckoutFormInitializeRequest.setBasketItems($basketItems) function. Since this function did not specify the type of $basketItems, it accepted any input without warnings from the IDE. As PHP is an interpreted language, you need to execute all code paths to ensure there are no bugs. Your best option is to let the IDE help you as much as possible, and one of the ways is to specify types, especially in API functions.
If the API had required an array type for the setBasketItems method, as in setBasketItems(array $basketItems), my IDE would have alerted me, saving me several hours.
Given that PHP has supported type declarations for function parameters, including classes and arrays, since PHP 5.0 (2004, 20 years ago!), iyzico's API could have been better designed, avoiding unnecessary developer time wastage.
No comments:
Post a Comment