Drupal.Semantics.FunctionTriggerError rule is not showing the full error message when it contains both __CLASS__ and __METHOD__

The class//method thing might not be the reason at all this goes wrong, but that is my guess looking at the actual trigger_error and the reported message.

FILE: core/modules/taxonomy/src/Entity/Term.php
------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------
276 | ERROR | The deprecation message '::bundle() instead to get the vocabulary ID.' does not match the standard format: %thing% is deprecated in %in-version%. %extra-info%. See %cr-link%
------------------------------------------------------------------------------

Comments

Lendude created an issue. See original summary.

jonathan1055’s picture

Thanks for spotting this. The code line in question is

@trigger_error('The ' . __METHOD__ . ' method is deprecated since version 8.4.0 and will be removed before 9.0.0. Use ' . __CLASS__ . '::bundle() instead to get the vocabulary ID.', E_USER_DEPRECATED);

I think an original assumption (may have been written somewhere as a guide) is that the text would cater for one concatenation at the start of the message, such as __NAMESPACE__, but the rest of the message would be in one string. But clearly that is not going to be adequate, as this example demonstrates.

It should not be too difficult to allow constants within the message. I don't see any need to evaluate (or even verify) the actual constants, just cater for them when building up the entire string to check.

jonathan1055’s picture

Title: Drupal.Semantics.FunctionTriggerError rule is not showing the full error message when it contains __CLASS__ and __METHOD__ » FunctionTriggerError does not extract the full error message when it contains concatenations
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new7.5 KB

When the message is made with sprintf() when we use ->findnext to get the string as before. But now, in all other cases we need to build up the message to check from all the parts within the argument 'start' and 'end', i.e. including concatenated strings and constants. This adds one small complication in that quotes around the strings become embedded as part of the message (and cause incorrect standards messages). So we remove the quotes and adjust the regex not to expect quotes at the start and end.

Using the example above, the output is now:

FILE: ...ary/WebServer/Documents/drupal86/core/modules/taxonomy/src/Entity/Term.php
--------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------
 263 | ERROR | The deprecation message 'The  __METHOD__  method is deprecated
     |       | since version 8.4.0 and will be removed before 9.0.0. Use 
     |       | __CLASS__ ::bundle() instead to get the vocabulary ID.' does not
     |       | match the standard format: %thing% is deprecated in
     |       | %in-version%. %extra-info%. See %cr-link%
     |       | (Drupal.Semantics.FunctionTriggerError.TriggerErrorTextLayout)
--------------------------------------------------------------------------------

Here's a patch for review, with adjusted test data, but I will alos create a pull request on github.

jonathan1055’s picture

  • klausi committed 2d05248 on 8.x-3.x
    fix(FunctionTriggerError): Allow concatenations with constants in...
klausi’s picture

Status: Needs review » Fixed

Thanks, committed!

Status: Fixed » Closed (fixed)

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