Skip to content
Snippets Groups Projects
Commit 2d05248a authored by Jonathan Smith's avatar Jonathan Smith Committed by Klaus Purer
Browse files

fix(FunctionTriggerError): Allow concatenations with constants in message argument (#3050697)

parent 2f63f218
No related branches found
No related tags found
No related merge requests found
......@@ -69,11 +69,32 @@ class FunctionTriggerErrorSniff extends FunctionCall
// Get the first argument passed to trigger_error().
$argument = $this->getArgument(1);
// Extract the message text to check. Using findNext() will allow for
// an optional __NAMESPACE__ concatenated at the start and also cater
// for function calls such as sprintf() inside the message.
$message_position = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $argument['start']);
$message_text = $tokens[$message_position]['content'];
// Extract the message text to check. If if it formed using sprintf()
// then find the single overall string using ->findNext.
if ($tokens[$argument['start']]['code'] === T_STRING
&& strcasecmp($tokens[$argument['start']]['content'], 'sprintf') === 0
) {
$message_position = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $argument['start']);
// Remove the quotes using substr, because trim would take multiple
// quotes away and possibly not report a faulty message.
$message_text = substr($tokens[$message_position]['content'], 1, ($tokens[$message_position]['length'] - 2));
} else {
// If not sprintf() then extract and store all the items except
// whitespace, concatenation operators and comma. This will give all
// real content such as concatenated strings and constants.
for ($i = $argument['start']; $i <= $argument['end']; $i++) {
if (in_array($tokens[$i]['code'], [T_WHITESPACE, T_STRING_CONCAT, T_COMMA]) === false) {
// For strings, remove the quotes using substr not trim.
if ($tokens[$i]['code'] === T_CONSTANT_ENCAPSED_STRING) {
$message_parts[] = substr($tokens[$i]['content'], 1, ($tokens[$i]['length'] - 2));
} else {
$message_parts[] = $tokens[$i]['content'];
}
}
}
$message_text = implode(' ', $message_parts);
}//end if
// The standard format for @trigger_error() is:
// %thing% is deprecated in %in-version%. %extra-info%. See %cr-link%
......@@ -81,12 +102,12 @@ class FunctionTriggerErrorSniff extends FunctionCall
// the first '. ' is matched, as there may be more than one sentence in
// the extra-info part.
$matches = array();
preg_match('/[\'\"](.+) is deprecated in (?U)(.+)\. (.+)\. See (.+)[\'\"]/', $message_text, $matches);
preg_match('/(.+) is deprecated in (?U)(.+)\. (.+)\. See (.+)$/', $message_text, $matches);
// There should be 5 items in $matches: 0 is full text, 1 = thing,
// 2 = in-version, 3 = extra-info, 4 = cr-link.
if (count($matches) !== 5) {
$error = "The deprecation message %s does not match the standard format: %%thing%% is deprecated in %%in-version%%. %%extra-info%%. See %%cr-link%%";
$error = "The deprecation message '%s' does not match the standard format: %%thing%% is deprecated in %%in-version%%. %%extra-info%%. See %%cr-link%%";
$phpcsFile->addError($error, $argument['start'], 'TriggerErrorTextLayout', array($message_text));
} else {
// The text follows the basic layout. Now check that the version
......
......@@ -20,6 +20,10 @@
@trigger_error('Function foo_bar() is deprecated in my_module:8.x-2.0. Use bar_baz() instead. See https://www.drupal.org/project/my_module/issues/2908490', E_USER_DEPRECATED);
// Cater for sprintf inside the message.
@trigger_error(sprintf('Function %s is deprecated in drupal:8.5.0. Use foo() instead. See http://www.drupal.org/project/my_module/issues/123', $settings), E_USER_DEPRECATED);
// Cater for concatenations in the message.
@trigger_error(__METHOD__ . ' method is deprecated in drupal:8.4.0. Use ' . __CLASS__ . '::bundle() instead. See https://www.drupal.org/project/drupal/issues/12345', E_USER_DEPRECATED);
// Cater for concatenation with nothing else before 'is deprecated.
@trigger_error(__NAMESPACE__ . ' is deprecated in drupal:8.5.0. It will be removed from Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED);
// ERRORS on general layout.
// Wrong first part - missing word.
......@@ -28,12 +32,8 @@
@trigger_error('drupal_set_message() is deprecated in Drupal:8.5.0. It will be removed from Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED);
// Wrong first part - no final dot.
@trigger_error('drupal_set_message() is deprecated in Drupal:8.5.0 Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED);
// Wrong first part - nothing before 'is deprecated'.
@trigger_error(__NAMESPACE__ . ' is deprecated in Drupal:8.5.0. It will be removed from Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED);
// Missing 'extra info' part.
@trigger_error('drupal_set_message() is deprecated in Drupal:8.5.0. See https://www.drupal.org/node/2774931', E_User_Deprecated);
// String would be OK but is in more than one part.
@trigger_error('drupal_set_message() is deprecated in Drupal:8.5.0. ' . 'It will be removed from Drupal 9.0.0. Use \Drupal\Core\Messenger\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED);
// WARNINGS for version. Version with only one component.
@trigger_error(__NAMESPACE__ . '\CommentTestBase is deprecated in drupal:8. Use \Drupal\Tests\comment\Functional\Views\CommentTestBase instead. See http://www.drupal.org/node/2908490', E_USER_DEPRECATED);
// Version with only two components.
......@@ -62,3 +62,5 @@
@trigger_error(__NAMESPACE__ . '\CommentTestBase is deprecated in drupal:8.6.0. Use \Drupal\Tests\comment\Functional\Views\CommentTestBase instead. See https://www.drupal.org/node/123a', E_USER_DEPRECATED);
// Do not want period after url.
@trigger_error(__NAMESPACE__ . '\CommentTestBase is deprecated in drupal:8.6.0. Use \Drupal\Tests\comment\Functional\Views\CommentTestBase instead. See https://www.drupal.org/node/123.', E_USER_DEPRECATED);
// Ensure that concatenation at end of string is checked.
@trigger_error(__NAMESPACE__ . '\CommentTestBase is deprecated in drupal:8.6.0. Use \Drupal\Tests\comment\Functional\Views\CommentTestBase instead. See https://www.drupal.org/node/123' . __FUNCTION__ , E_USER_DEPRECATED);
......@@ -18,8 +18,6 @@ class FunctionTriggerErrorUnitTest extends CoderSniffUnitTest
public function getErrorList($testFile = NULL)
{
return array(
26 => 1,
28 => 1,
30 => 1,
32 => 1,
34 => 1,
......@@ -54,6 +52,7 @@ class FunctionTriggerErrorUnitTest extends CoderSniffUnitTest
60 => 1,
62 => 1,
64 => 1,
66 => 1,
);
}//end getWarningList()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment