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
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 3240167-18.patch | 3.43 KB | alexpott |
| #18 | 12-18-interdiff.txt | 4.28 KB | alexpott |
Comments
Comment #2
alexpottHere's the fix from the meta.
Comment #3
andypostCould use to extend test #3240172-6: \Drupal\comment\Entity\Comment::getThread() can cause deprecations on PHP 8.1
Moreover it's used only in
\Drupal\comment\Entity\Comment::preSave()and there's usage in contrib http://grep.xnddx.ru/search?text=getMaxThread&filename=Comment #4
andypostBtw it happens only on first comment when query returns no results
select max(tread) from comment_field_data where cid=0because https://www.php.net/manual/en/pdostatement.fetchcolumn could return FALSE when results are missingAlso there's usage http://grep.xnddx.ru/search?text=getMaxThreadPerThread&filename=
Patch - in case of FALSE will return "" (empty string)
Comment #5
andypostfix typo
Comment #6
daffie commentedThese are for me in the wrong place. To me, they belong in the
CommentStorageInterface.Comment #7
alexpottHere'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.
Comment #8
alexpottFixing the title and issue summary.
Comment #9
alexpottThis is a subtle and probably not important behaviour change but a string that evals to empty has different behaviour now... ie...
Comment #10
alexpottRe #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.
Comment #11
daffie commentedCould we do this then?
Comment #12
andypostI 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
Comment #13
larowlanI 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
Comment #14
larowlanComment #15
daffie commentedLooks good to me.
@andypost: Thank you for updating the interfaces.
Comment #16
alexpottI'm not sure that we should be starting sentences with "Or". I think this should be an unsaved comment as new comment feels ambiguous.
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.
Comment #17
alexpottI've investigated the deprecations a little further...
The
\Drupal\comment\CommentStorage::getMaxThread()is due to anrtrim()in\Drupal\comment\Entity\Comment::preSave()...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 anexplode()\Drupal\comment\CommentViewBuilder::buildComponents()where it does: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.
Comment #18
alexpottHere's the latest from the meta...
Comment #19
andypost++ to change the interface, then it needs CR
Personally I prefer
$max ?? ''syntax OTOH(string)cast makes sense and works for mePS: PHP docs every-time require 3v4l)
Comment #20
daffie commentedI am fine with
(string), just as I am fine with$max ?? ''.Added a CR and updated the IS.
For me it is RTBC.
Comment #21
alexpottI 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.
Comment #23
catchCommitted 6fb2c09 and pushed to 9.3.x. Thanks!
Comment #25
quietone commentedPublished the change record.