-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed string helper encoding when Yii::$app is not available #13263
Conversation
Thank you for putting effort in the improvement of the Yii framework. In order for the framework and your solution to remain stable in the future, we have a unit test requirement in place. Therefore we can only accept your pull request if it is covered by unit tests. Could you add these please? Thanks! P.S. If you have any questions about the creation of unit tests? Don't hesitate to ask for support. More information about unit tests This is an automated comment, triggered by adding the label |
See #13254 (comment) We should either fix all the dependencies on Yii application around the helper classes and validators or leave everything be as it is. |
Sounds good. This would make development more flexible. |
IMO all dependencies on an active Yii application should be minimized in the helpers. For three reasons:
|
@@ -104,12 +104,15 @@ public static function dirname($path) | |||
*/ | |||
public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false) | |||
{ | |||
if ($encoding === null) { | |||
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
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.
shouldnt this be put once in an ApplicationHelper?
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.
or Yii::encoding()
.
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.
what exactly and why?
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.
A helper to replace a simple ternary operator check?
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.
I don't think a new helper is needed but that ternary operator seem to happen a lot. How about a new method?
StringHelper::getEncoding()
or Yii::encoding()
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.
Standalone valdator and helper running will become impossible in case you need default encoding different from UTF-8.
For the helper it is no problem as long as you specify the $encoding
param of the method.
For the validator we could add a property that defaults to Yii::$app->charset
or 'UTF-8' if not set.
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.
For the helper it is no problem as long as you specify the $encoding param of the method.
You can do it now, with current code, and thus avoid coupling with Application
. Following such argument - no change is needed at all.
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.
Yii::$app should ideally be removed out of this helper
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.
I'm for Yii::ENCODING
constant or just leaving UTF-8
string in ternary check. This approach is already used in multiple places in core. Symfony uses UTF-8
as "magic string", and Laravel don't even care about different encodings and have hardcoded UTF-8
in multiple places. It is not worth complicating this only for extremely edge cases.
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.
Leaving a string there sounds OK to me.
making StringHelper and StringValidator independent of Yii::$app fixes #13254
0cdb073
to
74d2d02
Compare
rebased and made sure tests cover it by forcing Yii::$app to be null in test cases. This should be done for more helpers, will prepare a separate PR for that. |
@@ -104,12 +104,15 @@ public static function dirname($path) | |||
*/ | |||
public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false) | |||
{ | |||
if ($encoding === null) { | |||
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8'; |
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.
Usage of ternary operator introduces a "magic string" - UTF-8
.
Keeping default encoding at Application field is a one thing, but put putting same string constant all over the code is a different one.
While in current state UTF-8
is covering most of the needs and is de-facto standard it may change in the future. After several years we may use UTF-16
for example.
We should either introduce a static level for default encoding (e.g. Yii::$encoding
or Yii::encoding()
or Yii::$defaultEncoding
) or abandon the whole idea.
if (mb_strlen($string, $encoding) > $length) { | ||
return rtrim(mb_substr($string, 0, $length, $encoding)) . $suffix; | ||
} else { | ||
return $string; |
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.
What do the above two lines do?
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.
If string length is more than limit defined in $length
it trims string to the length specified, then adding $suffix
.
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.
It's the two new lines
} else {
return $string;
that i think need explanation. The next line after the else {}
is return $string;
When there is no framework "global context" (Yii::$app) where the default or user configuration can go, I would expect the framework to get out of the way rather than force any hardcoded value. Instead, I would expect PHP's INI directive default_charset to apply. |
Could be a good idea. @cebe what do you think? |
default_charset is PHP 5.6.0, so that would be the way to go in 2.1. |
As we discuss a framework helper, I'd like to implement both @tom-- & @klimov-paul ideas: |
$this->mockApplication(); | ||
|
||
// destroy application, Helper must work without Yii::$app | ||
$this->destroyApplication(); |
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.
shouldnt we extend from a PhpUnit base class in which no application is setup?
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.
@dynasource yiiunit\TestCase
dose not mock application by default. destroyApplication()
is used only to make sure that Yii::$app
is not setup as leftovers after previous tests.
I agree. The encoding logic should be taken out. |
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.
I'm for merging this as is. Yii::encoding()
could be added in another PR, there is no sense to block this one.
Merged. Thanks @cebe for working on it and everyone for reviews and suggestions. |
fixes #13254