fix: issue with GetHeader called with non-existing header#1019
fix: issue with GetHeader called with non-existing header#1019JohnVillalovos merged 1 commit intodevelopfrom
Conversation
This would cause errors like: PHP Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /home/runner/work/librebooking/librebooking/lib/Server/Server.php on line 272 PHP Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /home/runner/work/librebooking/librebooking/lib/Server/Server.php on line 260
There was a problem hiding this comment.
Pull request overview
This PR fixes PHP warnings that occur when GetHeader is called with a non-existing header key. The warnings include an "Undefined array key" error and a deprecated null parameter warning for strlen(). The fix adds a null coalescing operator to return an empty string when the header doesn't exist.
Changes:
- Modified
GetHeadermethod inlib/Server/Server.phpto return an empty string instead of undefined when a header key doesn't exist in$_SERVER
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public function GetHeader($headerCode) | ||
| { | ||
| return $_SERVER[$headerCode]; | ||
| return $_SERVER[$headerCode] ?? ''; |
There was a problem hiding this comment.
This change returns an empty string for missing headers, which is inconsistent with other similar methods in the Server class. Methods like GetCookie, GetQuerystring, GetRawForm, and GetSession all return null for missing values.
Additionally, this is inconsistent with the IRestServer interface (lib/WebService/IRestServer.php:43), which defines GetHeader as returning string|null. While Server.php doesn't implement IRestServer, the inconsistency could cause confusion.
Consider using return $_SERVER[$headerCode] ?? null; instead to maintain consistency with the codebase pattern and match the interface definition.
Note: The docblock on line 268 also claims "@return string" but should be "@return string|null" to reflect the actual behavior.
This would cause errors like:
PHP Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in
/home/runner/work/librebooking/librebooking/lib/Server/Server.php on
line 272
PHP Deprecated: strlen(): Passing null to parameter #1 ($string) of
type string is deprecated in
/home/runner/work/librebooking/librebooking/lib/Server/Server.php on
line 260