Problem/Motivation

\Drupal\comment\CommentStorage::getMaxThread() can cause deprecations on PHP 8.1 because it returns a NULL and passes it to a string function.
\Drupal\comment\Entity\Comment::getThread() can cause deprecations on PHP 8.1 because it can return a NULL.

Steps to reproduce

Run core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php on PHP 8.1

Proposed resolution

Maybe we need to adjust \Drupal\comment\CommentStorage::getMaxThreadPerThread() too... or work out a better fix.

Remaining tasks

None

User interface changes

None

API changes

The method Drupal\comment\CommentInterface::getThread() will return the value NULL when the method is called before the comment is saved.

The methods Drupal\comment\CommentStorageInterface::getMaxThread() and Drupal\comment\CommentStorageInterface::getMaxThreadPerThread() will return the value NULL when the commented entity has no comments.

Data model changes

None

Release notes snippet

TBD

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new683 bytes

Here's the fix from the meta.

andypost’s picture

andypost’s picture

Issue summary: View changes
StatusFileSize
new1.33 KB
new1.2 KB

Btw it happens only on first comment when query returns no results select max(tread) from comment_field_data where cid=0 because https://www.php.net/manual/en/pdostatement.fetchcolumn could return FALSE when results are missing

Also there's usage http://grep.xnddx.ru/search?text=getMaxThreadPerThread&filename=

Patch - in case of FALSE will return "" (empty string)

andypost’s picture

StatusFileSize
new1.26 KB
new1.2 KB

fix typo

daffie’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/comment/src/CommentStorage.php
    @@ -86,8 +86,10 @@ public function getMaxThread(CommentInterface $comment) {
    +    // Result could be FALSE when no comments found. For details see
    +    // https://www.php.net/manual/en/pdostatement.fetchcolumn page.
    
    @@ -101,8 +103,10 @@ public function getMaxThreadPerThread(CommentInterface $comment) {
    +    // Result could be FALSE when no comments found. For details see
    +    // https://www.php.net/manual/en/pdostatement.fetchcolumn page.
    

    These are for me in the wrong place. To me, they belong in the CommentStorageInterface.

  2. The added testing as suggested by @andypost as is done in #3240172: \Drupal\comment\Entity\Comment::getThread() can cause deprecations on PHP 8.1 is to me as great idea.
alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new535 bytes
new1.72 KB

Here's the related work from #3240172: \Drupal\comment\Entity\Comment::getThread() can cause deprecations on PHP 8.1

@daffie I disagree that testing the empty string return is actually worth it. We have no testing of the NULL return and the moment we have PHP 8.1 testing turned on we have coverage.

alexpott’s picture

Title: \Drupal\comment\CommentStorage::getMaxThread() causes deprecations on PHP 8.1 » \Drupal\comment\CommentStorage::getMaxThread() and \Drupal\comment\Entity\Comment::getThread() cause deprecations on PHP 8.1
Issue summary: View changes

Fixing the title and issue summary.

alexpott’s picture

+++ b/core/modules/comment/src/Entity/Comment.php
@@ -484,10 +484,7 @@ public function setCreatedTime($created) {
   public function getThread() {
-    $thread = $this->get('thread');
-    if (!empty($thread->value)) {
-      return $thread->value;
-    }
+    return $this->get('thread')->value ?? '';
   }

This is a subtle and probably not important behaviour change but a string that evals to empty has different behaviour now... ie...

php > var_dump(empty('0'));
bool(true)
alexpott’s picture

Re #6.1 I think the comments are not necessary. Maybe the interface should document what an empty string means but it certainly should not document this implementation detail. The interface already claims to only return strings - although this is not currently true.

daffie’s picture

Maybe the interface should document what an empty string means

Could we do this then?

andypost’s picture

StatusFileSize
new1.65 KB
new3.37 KB

I suppose both docs needs update to explain the only case when there's an empty string could happen
- Comment entity is new - thread is not calculated yet
- Storage could return empty when calculating thread for first comment in the entity's comment field thread

larowlan’s picture

I think this is fine
However, I think we should open a follow-up to move Comment::preSave to the storage handler.

That way we can deprecate ::getMaxThread and ::getMaxThreadPerThread and make them protected.

They really don't belong in an API in my opinion - and Comment::preSave is the only existing usages.

Probably the same story for ::getChildCids and Comment::postDelete

larowlan’s picture

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

@andypost: Thank you for updating the interfaces.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/comment/src/CommentInterface.php
    @@ -212,7 +212,8 @@ public function setCreatedTime($created);
    -   *   The alphadecimal representation of the comment's place in a thread.
    +   *   The alphadecimal representation of the comment's place in a thread. Or
    +   *   empty string for new comment.
    

    I'm not sure that we should be starting sentences with "Or". I think this should be an unsaved comment as new comment feels ambiguous.

  2. +++ b/core/modules/comment/src/CommentStorage.php
    @@ -86,8 +86,10 @@ public function getMaxThread(CommentInterface $comment) {
    +    // Result could be FALSE when no comments found. For details see
    +    // https://www.php.net/manual/en/pdostatement.fetchcolumn page.
    

    I think the docs on the interface are enough. Also I think we might want to see how the FALSE is used and document that and not change the return type... i.e. fix the callers.

alexpott’s picture

I've investigated the deprecations a little further...

The \Drupal\comment\CommentStorage::getMaxThread() is due to an rtrim() in \Drupal\comment\Entity\Comment::preSave()...

          // This is a comment with no parent comment (depth 0): we start
          // by retrieving the maximum thread level.
          $max = $storage->getMaxThread($this);
          // Strip the "/" from the end of the thread.
          $max = rtrim((string) $max, '/');

Also FWIW the document that was being added by previous patches was incorrect. This was never returning FALSE for entities without comments. It is returning NULL. - here's evidence for that https://3v4l.org/F7tDb

And the \Drupal\comment\Entity\Comment::getThread() deprecation is due to an explode()\Drupal\comment\CommentViewBuilder::buildComponents() where it does:

    foreach ($entities as $id => $entity) {
      if ($build[$id]['#comment_threaded']) {
        $comment_indent = count(explode('.', $entity->getThread())) - 1;

getThread() is NULL here because a comment is being previewed and is get to be saved.

I think we should fix these where they are called and update the documentation.

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new4.28 KB
new3.43 KB

Here's the latest from the meta...

andypost’s picture

++ to change the interface, then it needs CR

+++ b/core/modules/comment/src/CommentViewBuilder.php
@@ -124,7 +124,7 @@ public function buildComponents(array &$build, array $entities, array $displays,
+        $comment_indent = count(explode('.', (string) $entity->getThread())) - 1;

+++ b/core/modules/comment/src/Entity/Comment.php
@@ -102,7 +102,7 @@ public function preSave(EntityStorageInterface $storage) {
+          $max = rtrim((string) $max, '/');

Personally I prefer $max ?? '' syntax OTOH (string) cast makes sense and works for me

PS: PHP docs every-time require 3v4l)

daffie’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

I am fine with (string), just as I am fine with $max ?? ''.
Added a CR and updated the IS.
For me it is RTBC.

alexpott’s picture

I think we should delete the CR. There is no API change here and having a CR suggests there is. The documentation is wrong. If we're going to add a CR for every bit of incorrect documentation in core we're going to have way way too many CRs.

  • catch committed 6fb2c09 on 9.3.x
    Issue #3240167 by andypost, alexpott, daffie, larowlan: \Drupal\comment\...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 6fb2c09 and pushed to 9.3.x. Thanks!

Status: Fixed » Closed (fixed)

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

quietone’s picture

Published the change record.