Saturday, June 8, 2024

Specify types in API functions

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.


Thursday, June 6, 2024

PHP double quotes vs single quotes

Use double quotes (") when you want PHP to interpret special characters like \n as newlines. Use single quotes (') when you want the string to be taken literally without any special character interpretation.
// Using double quotes>
echo "This is a line\nAnd this is another line";
// Using single quotes
echo 'This is a line\nAnd this is another line';
Output:
This is a line
And this is another line
This is a line\nAnd this is another line

Sunday, June 2, 2024

The confusing 508 status code

Recently, UptimeRobot notified me that our site is down with HTTP status code 508 - Loop detected. The site recovered on its own after five minutes. On another occasion, a user reported seeing the message Resource Limit Is Reached when attempting to access our web app via a browser.

The standard definition of 508 says that it may be given in the context of the Web Distributed Authoring and Versioning (WebDAV) protocol and that it indicates that the server terminated an operation because it encountered an infinite loop. This was initially confusing, as I was not using any WebDAV and  the ini_set('max_execution_time', 300) in my PHP code would end hanging processes, which is consistent with the site recovering after 5 minutes.

Further research revealed that the 508 code could also signify that a Resource Limit Is Reached, a usage specific to CloudLinux environments, which deviates from the standard interpretation. In this case, Uptime Robot was applying the standard definition, whereas our hosting environment attributed a different meaning to the same code.

Ultimately, the issue was traced back to exceeding the maximum number of processes (NPROC) allocated to our web app. I am continuing to investigate the root cause.