FilterInterface::tips return does not match core implementations

Created on 7 July 2023, over 1 year ago
Updated 14 July 2023, over 1 year ago

Problem/Motivation

While checking the Glossary 2 module with phpstan, I noticed a warning about the tips method.

FilterInterface::tips() is defined as:

  /**
   * ...snip...
   *
   * @return string|null
   *   Translated text to display as a tip, or NULL if this filter has no tip.
   */
  public function tips($long = FALSE);

However, all implementations in core actually return TranslatableMarkup|null.

Steps to reproduce

Read all filter implementations in core.

Proposed resolution

  • Modify FilterInterface to match what code actually does: update the PHPdoc to @return \Drupal\Core\StringTranslation|null
  • Optional: add a return type hint: public function tips($long = FALSE): ?TranslatableMarkup . I'm not sure where we stand on return typing for D10.

Remaining tasks

Decide on the return typing.

User interface changes

None.

API changes

If it is just a phpdoc, none. If we type the return value, it may qualify as an API change, but one that matches core implementations, and those implementations I looked at in contrib.

Data model changes

None.

Release notes snippet

None.

🐛 Bug report
Status

Needs work

Version

10.1

Component
Filter 

Last updated about 10 hours ago

No maintainer
Created by

🇫🇷France fgm Paris, France

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @fgm
  • Assigned to anchal_gupta
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,439 pass
  • 🇮🇳India anchal_gupta

    I have uploaded the patch. please review

  • 🇫🇷France fgm Paris, France

    What about the return type ? I thought that starting with D10 we were adding return type hints, are we not ? (not saying we do: I just wonder)

    Otherwise LGTM.

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    @fgm we do for D10.

  • Status changed to Needs review over 1 year ago
  • 🇮🇳India AkashKumar07

    Added a return type hint. Please review.
    Thanks.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7 updated deps
    last update over 1 year ago
    Custom Commands Failed
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Custom Commands Failed
  • Status changed to Needs work over 1 year ago
  • 🇫🇷France fgm Paris, France

    @AkashKumar07 as you can see this patch doesn't pass tests. Can you fix it before we can review ?

    Once you get an error, like this, iou may want to test locally on your machine before submitting the patch to drupalci, as you then see where the failure is, so you don't have to run the whole test suite, but just one test that failed on CI, and it's much faster to run locally.

  • 🇮🇳India AkashKumar07

    Adding ? for null type hint.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Custom Commands Failed
  • 🇫🇷France fgm Paris, France

    Look at the results of that patch: there are more errors: https://www.drupal.org/pift-ci-job/2712812

    You could run phpstan locally yourself to get results faster: just install phpstan-drupal, e.g. using the Extension Installer https://github.com/phpstan/extension-installer : that way you can get the same results as those given by DrupalCI on your own machine.

  • Status changed to Needs review over 1 year ago
  • last update over 1 year ago
    29,444 pass
  • 🇫🇷France fgm Paris, France

    Actually, there are a couple strings returned too, so the patch is a bit larger.

  • Status changed to RTBC over 1 year ago
  • 🇺🇸United States smustgrave

    Thanks @fgm for taking care of this

    Tests are all green so adding typehint is good.

    Short and sweet!

  • Status changed to Needs work over 1 year ago
  • 🇬🇧United Kingdom catch

    We can't add the return type hint to the interface in a minor release. See 📌 Enable existing interfaces to add return type hints with a deprecation message for implementors Active .

    Also wonder, could we use Stringable instead of TranslatableMarkup here? It'd be more generic.

  • 🇫🇷France fgm Paris, France

    Thanks. I see that 📌 Enable existing interfaces to add return type hints with a deprecation message for implementors Active does not currently suggest a path forward. So what should we do here ?

    • Just fix the phpdoc ? :-(
    • Define and add the [#ReturnTypeHintDeprecation] ? That seems out of scope.

    Regarding \Stringable, the current phpdoc incorrectly specifies string|null, so it does not even allow it, convenient though it would be. If we dig a little, the string return is only ever used in FilterHtml, with all other core implementations using either naked returns (so NULL) or TranslatableMarkup so we could just as well consider it is an error and only allow ?\TranslatableMarkup.

    But since this is what we document the interface to be, we must probably assume that contrib/custom devs have looked at the core implementations and just handled the actual returns, so they also return ?\TranslatableMarkup. Should be move to \Stringable<\code>, that would be the type to add to the typed return in 11.x, and it could in the future break any contrib actually using any of the public methods on <code>?\TranslatableMarkup (because that's what core actually used) whenever someone decides to update core to return a \Stringable that is not also a \TranslatableMarkup because, hey, that's what core documents... so I think the safest PHPdoc signature would be ?\Drupal\Core\StringTranslation\TranslatableMarkup.

    I removed the novice tag because that's no longer a novice question.

Production build 0.71.5 2024