Update encode method to use type hinting for string|Response object#1150
Draft
joanhey wants to merge 16 commits intowalkor:masterfrom
Draft
Update encode method to use type hinting for string|Response object#1150joanhey wants to merge 16 commits intowalkor:masterfrom
joanhey wants to merge 16 commits intowalkor:masterfrom
Conversation
We can improve this method.
…Contains parameter
We need to check it later
Contributor
Author
|
Interesting journey with this code. Testing With only class str implements stringable {
function __tostring(){
return 'Hola';
}
}
$b = new str();
function test(string $input = '') {
if (is_string($input)) echo "String";
if (is_a($input, 'Stringable')) echo "Object";
var_dump($input);
}
test($b);
// Return:
Stringstring(4) "Hola"With class str implements stringable {
function __tostring(){
return 'Hola';
}
}
$b = new str();
function test(string|Stringable $input = '') {
if (is_string($input)) echo "String";
if (is_a($input, 'Stringable')) echo "Object"; // or is_a($input, 'str')
var_dump($input);
echo $input;
}
test($b);
// Return:
Objectobject(str)#1 (0) {
}
HolaHere only change to string when echo or (string). And seems that phpstan fail with this code. |
Contributor
Author
|
In my humble opinion, we can refactor the Checking the code:
A small discussion about the best way to do it will help. |
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.
Available from PHP 8.0.
EDIT:
string|Response|ServerSentEventsstring|StringableWith only type hint
string, any class that implementsstringablecan be passed, so we are not limiting the classes. And we can simplify thesend()code as always is string. But for now it's OK.PD: later we need to check the
TcpConnection::send()why the$sendBufferreceivenull. This variable now ismixedneed to be limited tostring|Stringable.For now use a fast fix
$sendBuffer ??= '';