Initialize Client\Response::$reasonPhrase - #19577
Merged
Merged
Conversation
`$reasonPhrase` is only assigned in `_parseHeaders()`, when a header line matches an `HTTP/x.y NNN` status line, and in `withStatus()`. A response constructed without a status line, such as `new Response([], 'body')` in a test double, therefore left the property uninitialized and `getReasonPhrase()` raised "Typed property must not be accessed before initialization" despite declaring a `string` return type. PSR-7 requires the method to return a string. This is not new in 5.x. Before the property gained a native type the untyped default was null, so the same call raised a TypeError instead. Default the property to an empty string, which is what PSR-7 allows when no reason phrase is known.
ADmad
approved these changes
Aug 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #19576.
Cake\Http\Client\Response::$reasonPhraseis assigned in only two places:_parseHeaders(), when a header line matches anHTTP/x.y NNNstatus line, andwithStatus(). Construct a response without a status line and neither runs, so the property stays uninitialized:A method declared
: stringshould not fatal, and PSR-7 requiresgetReasonPhrase()to return a string.Defaulting the property to
''is what PSR-7 allows when no reason phrase is known, and matches the reporter's suggestion.Not a 5.x regression
Worth noting for triage: this is not new.
52104f3("Add native type hints for Http properties") changedprotected $reasonPhrase;toprotected string $reasonPhrase;, butgetReasonPhrase(): stringalready carried the string return type before that commit. So the untyped default wasnulland the same call raisedTypeError: Return value must be of type string, null returned. The typed property only changed the error message.Left out on purpose
The related inconsistency in the issue thread is not addressed here, since it is a behavior change rather than a fix:
withStatus(404)phraseHttp\Response200'OK''Not Found'Http\Client\Response0''(after this PR)''Http\Response::_setStatus()fills an empty reason phrase from its$_statusCodesmap; the client class has no such map and stores whatever it is given. Making the two consistent would change output for existingwithStatus()callers, so it seems better suited to 5.next if wanted at all. Happy to follow up.