api-php-lib icon indicating copy to clipboard operation
api-php-lib copied to clipboard

TypeError when calling get() with no results

Open SteenSchutt opened this issue 2 years ago • 0 comments

The Site::get() method is type hinted to always return a GeneralInfo struct, but it just mindlessly returns the result from reset($items), which returns false when the array is empty.

Furthermore, it seems quite unhelpful to get multiple results back from the API and then just returning the first one. Considering this is already published as a public interface, I would suggest making the return type nullable and then changing return reset($items); to return reset($items) ?: null. Then add some kind of method that would return all results and call it find, search or something like that.

That way it should be possible to remedy the issue without breaking compatibility (Beyond people working around the issue by catching the TypeError that arises when there are no results).

PHP Fatal error:  Uncaught TypeError: PleskX\Api\Operator\Site::get(): Return value must be of type PleskX\Api\Struct\Site\GeneralInfo, bool returned in ~/Git/project/vendor/plesk/api-php-lib/src/Api/Operator/Site.php:61
Stack trace:
#0 ~/Git/project/init.php(11): PleskX\Api\Operator\Site->get()
#1 {main}
  thrown in ~/Git/project/vendor/plesk/api-php-lib/src/Api/Operator/Site.php on line 61

SteenSchutt avatar Nov 22 '22 10:11 SteenSchutt