Problem/Motivation

String will be a reserved word in PHP 7. We could easily allow support for PHP 7 by simply renaming core/lib/Drupal/Component/Utility/String

Proposed resolution

Split class methods into:
1) move decodeEntities() into \Drupal\Component\Utility\Html class
2) other methods to \Drupal\Component\Utility\SafeMarkup class

Remaining tasks

Decide on change record.

See https://github.com/grom358/d8codetools for a way to easily rename a function across all files.

User interface changes

no

API changes

Deprecate all methods in \Drupal\Component\Utility\String

String::decodeEntities() Html::decodeEntities()
String::checkPlain() SafeMarkup::checkPlain()
String::format() SafeMarkup::format()
String::placeholder() SafeMarkup::placeholder()

Comments

jibran’s picture

How about DrupalString?

jibran’s picture

and for DataType StingData.

andypost’s picture

Maybe just Strings

andypost’s picture

berdir’s picture

The plugin classes are no issue, that shouldn't break anything except if someone subclassed theme.

But Utility\String is going to be painful, String::checkPlain() has 255 calls in core alone.

berdir’s picture

Oh. Maybe we can use http://php.net/manual/en/function.class-alias.php to keep BC for String?

benjy’s picture

Title: Rename all String classes to support PHP 7 » Rename Utility\String class to support PHP 7
Issue summary: View changes

Lets make this issue about Utility\String which is going to be a big enough patch anyway, i'll open some new issues for the views string classes.

benjy’s picture

Issue summary: View changes
benjy’s picture

New issue for the views String classes #2455415: Rename Views plugin classes to support PHP 7

dawehner’s picture

What about naming it simply StringHelper, given that the documentation of the class says, that this is the purpose of it:
Encodes special characters in a plain-text string for display as HTML.

dawehner’s picture

Status: Active » Needs review
StatusFileSize
new665.26 KB

I'm curious how big such a patch can become.

Note: even if we decide on a better name this is much easier with that patch applied, given that StringHelper is unique enough.

Status: Needs review » Needs work

The last submitted patch, 11: 2454447-11.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new672.24 KB
new8.72 KB

Fix stuff.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Looks good!

benjy’s picture

+1 for StringHelper.

benjy’s picture

Do we need a change record here?

webchick’s picture

-    $href = '<' . String::checkPlain($attributes['href']) . '>;';
+    $href = '<' . StringHelper::checkPlain($attributes['href']) . '>;';

Oof. :( That's much more verbose/weird. I guess it has parallels with UrlHelper, which is also verbose/weird, but the difference is that's not called almost 1,000 times in core.

Any chance we can cheat and just call it "Strings"? :P

benjy’s picture

I think there is a lot of personal preference here but I personally feel that StringHelper makes more sense than "Strings" even if it's a touch longer.

berdir’s picture

I think String was always a pretty strange class/name.

What do checkPlain(), decodeEntities() and format() have in common? Exactly, nothing. They operate on strings, but the same is true for Html (which would be a much better place for decodeEntities()), Xss (which would be a much better place for checkPlain()), Unicode, UrlHelper and so on.

Except those didn't exist when we added this, I guess it was one of the first: #1938972: Start moving string functions into a utility class (Xss.php was a month later..)

So the simple truth I guess is that StringHelper is as bad as String is right now, but we won't be able to come up with a good name, because there can't be a good name given that those methods have nothing in common.

Unless we actually decide to fix this problem by moving those methods to different classes*, I guess it doesn't matter that much, StringHelper works for me, and as mentioned, is consistent with UrlHelper.

* Not sure how viable that is, and also not sure where to move format() to, if we would go with this.

PS: Another reminder about the BC layer, do we want to do this or not?

wim leers’s picture

100% agreed with every word in #19. That's exactly what I would've written :)

fabianx’s picture

I would be all for merging this into the SafeMarkup class as that is all related to it, and would help performance quite a bit as then all the filters could avoid function calls to SafeMarkup::isSafe(), but use an isset on the static protected class member.

Especially because we generally have been going into the direction of making all escaping auto-escaping in e.g. String::format.

We probably would need to leave a BC layer, but that should be fine ...

And yes move decodeEntities to Html::

andypost’s picture

Title: Rename Utility\String class to support PHP 7 » Split Utility\String class to support PHP 7
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

Initially I though on Strings, but +1 on #21
Also suppose the title is better

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new672.64 KB
new890 bytes

Its tricky, given that in case we want to move String::format into something else a lot of code would need some form of additional treatment if you update.

Fixed the test file in the meantime

benjy’s picture

If we're going to split the methods out, I guess now would be the time. We don't want to make another API break in the future so I guess we need to make a decision on that.

For what it's worth, I'd be in favour of moving the methods into more relevant classes if that's an option that's likely to get committed.

catch’s picture

Title: Split Utility\String class to support PHP 7 » Split Utility\String class to support PHP 7 (String is a reserved word)
Priority: Normal » Critical

Bumping to critical, https://wiki.php.net/rfc/php7timeline looks like PHP 7 will be out before 8.1.0 even if not before 8.0.0.

I'd personally be OK with an API change here, including splitting the class to make more sense - it's not going to be any more of an API change to split the class up really, and in this case we copied some related functions from common.inc to a class. On the other hand we want to have the new API available asap since this will be widespread even if it's easy to make, so would be good not to get stuck on naming for too long.

Leaving the old class in for ~two betas also seems good - that gives plenty of warning before anything breaks.

Permanent dynamically defined BC-layer, that sounds like more trouble than it's worth.

andypost’s picture

StatusFileSize
new9.89 KB

Suppose the split should be like that.
Also it makes sense to split the issue on parts for example on per module basis

andypost’s picture

StatusFileSize
new12.48 KB
new22.37 KB

And split the tests.

andypost’s picture

StatusFileSize
new3.55 KB
new25.67 KB

remove core usage and clean-up

dawehner’s picture

I'm certainly in favour of making things sane, so here is a suggestion:

  • Have one issue to fix the critical issue: Rename String => StringHelper
  • Have a dedicated discussion how to make things sane, and keep StringHelper as BC layer
  • Convert the instances from StringHelper onto the same API
ianthomas_uk’s picture

Issue summary: View changes

There's a script at https://github.com/grom358/d8codetools which makes it easy to move/rename a function across the codebase, once we've decided where we want them to go.

berdir’s picture

@dawehner: That means we would have two API change. I don't think that makes sense and that we can qualify that.

I think we have two options:

a) Rename to StringHelper.
b) Go with the patch in #28. Maybe already convert the method calls here, maybe not.

I'd prefer b), with a follow-up to remove String in 1-2month.

dawehner’s picture

b) Go with the patch in #28. Maybe already convert the method calls here, maybe not.

Alright, so let's do that. I'm curious whether we should do the conversion of the method calls per message, so its really easy to review?

fabianx’s picture

+1 to #28, could improve performance quite a lot.

aspilicious’s picture

+++ b/core/lib/Drupal/Component/Utility/String.php
@@ -27,45 +24,32 @@ class String {
+   * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
+   *   Use \qDrupal\Component\Utility\Html::decodeEntities() instead.

Me found a typo

andypost’s picture

StatusFileSize
new677 bytes
new25.67 KB

fixed

rteijeiro’s picture

StatusFileSize
new25.69 KB
new1.24 KB

Wow, you were faster than me @andypost. Anyway I fixed a few nitpicks.

aspilicious’s picture

Status: Needs review » Reviewed & tested by the community

Ok everyone agrees, lets get this in asap so everyone can update their code once again ;)

aspilicious’s picture

Status: Reviewed & tested by the community » Needs review

Whoopsie, we need a change record first...

andypost’s picture

Issue summary: View changes
dawehner’s picture

Issue tags: -Needs change record

Created one

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC.

fabianx’s picture

RTBC + 1

alexpott’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs Drupal 8 critical triage

This issue addresses a critical bug and is allowed per https://www.drupal.org/core/beta-changes. Committed 40d6efe and pushed to 8.0.x. Thanks!

  • alexpott committed 40d6efe on 8.0.x
    Issue #2454447 by andypost, dawehner, rteijeiro: Split Utility\String...
benjy’s picture

Should these have been done here? Follow-up?

./core/modules/node/src/Plugin/views/argument/Type.php:use Drupal\Component\Utility\String as UtilityString;
./core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php:use Drupal\Component\Utility\String as UtilityString;
./core/modules/views/src/Plugin/views/argument/FieldList.php:use Drupal\Component\Utility\String as UtilityString;
./core/modules/views/src/Plugin/views/argument/ListString.php:use Drupal\Component\Utility\String as UtilityString;
./core/modules/views/src/Plugin/views/filter/FilterPluginBase.php:use Drupal\Component\Utility\String as UtilityString;
./core/modules/views/src/Plugin/views/filter/InOperator.php:use Drupal\Component\Utility\String as UtilityString;
./core/modules/views/src/Plugin/views/filter/NumericFilter.php:use Drupal\Component\Utility\String as UtilityString;
./core/modules/views/src/Plugin/views/filter/StringFilter.php:use Drupal\Component\Utility\String as UtilityString
benjy’s picture

Ahh Berdir pointed me to the two follow-ups, cheers.

andypost’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.