-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Supports the encoding of the console and encodes the text to display if needed #4606
Conversation
@@ -65,7 +68,16 @@ public function testCanDetachListenersFromEventManager() | |||
|
|||
public function testIgnoresNonConsoleModelNotContainingResultKeyWhenObtainingResult() | |||
{ | |||
$event = new MvcEvent(); | |||
//Register console service |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use 4 spaces here, not tabs
This also applies to master; I will cherry-pick when merging so that we can get it in both branches. |
* @param string $text | ||
* @return string the encoding text | ||
*/ | ||
public function encodeText($text); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a no-go, as changing an interface creates a backwards compatibility break. Considering that it's an implementation detail, we can omit it from the interface.
In looking at this, I have reservations about it as implemented:
Make the above changes, push them to your branch, and I'll review again. |
I'm ok with your first and third points. For the second point, do you think that the code below is better ? /**
* Encode a text to match console encoding
*
* @param string $text
* @return string the encoding text
*/
public function encodeText($text)
{
if ($this->isUtf8()) {
$textIsUtf8 = StringUtils::isValidUtf8($text);
if ($textIsUtf8) {
return $text;
} else {
return utf8_encode($text);
}
}
$textIsUtf8 = StringUtils::isValidUtf8($text);
if ($textIsUtf8) {
return utf8_decode($text);
} else {
return $text;
}
} |
Yes and No. :) I missed that you were using the value within the first condition. As such, caching the value kind of makes sense. However, it's just as readable, if not more, to call it when needed. Try this: /**
* Encode a text to match console encoding
*
* @param string $text
* @return string the encoding text
*/
public function encodeText($text)
{
if ($this->isUtf8()) {
if (StringUtils::isValidUtf8($text)) {
return $text;
}
return utf8_encode($text);
}
if (StringUtils::isValidUtf8($text)) {
return utf8_decode($text);
}
return $text;
} Note the removal of the else/elseif blocks -- you're returning from the previous condition, so why define them for code that will never consider them? That's the main bit I was getting at. With regards to caching the value -- that means that when I hit the condition, I have to backtrack to where the variable was declared to see what it means and how it was calculated. Using the method call directly in the condition makes this more clear -- and combined with the way the conditionals are written, it will only be executed when that conditional is tested. |
Ok for the function I've got a question about your note "getContent() should call encodeText(), not vice versa.". Does it mean that you want that
Am I Wrong ? |
I backpedaled on that one in a later response. :) Use either a new interface or ducktyping to determine if you can call encodeText(), and only call it if the implementation allows it. Otherwise, use the original routine. |
); | ||
if (is_callable(array($console,'encodeText'))) { | ||
$response->setContent( | ||
call_user_func(array($console,'encodeText'), $response->getContent() . $responseText) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify this to $console->encodeText($response->getContent() . $responseText)
.
Supports the encoding of the console and encodes the text to display if needed
- `s/\t/ /g` - Reflow logic in Console\DefaultRenderingStrategy to make it easier to read, and to re-use common values
I incorporated feedback when merging, including replacing all tabs with spaces. |
Supports the encoding of the console and encodes the text to display if needed
- `s/\t/ /g` - Reflow logic in Console\DefaultRenderingStrategy to make it easier to read, and to re-use common values
No description provided.