Commit efe6ac6d authored by Jonathan Smith's avatar Jonathan Smith Committed by Klaus Purer
Browse files

fix(FunctionTriggerError): Relax checks a bit and adopt different wording (#2908391)

parent bcfac795
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -83,21 +83,21 @@ class DeprecatedSniff implements Sniff
        // the first '. ' is matched, as there may be more than one sentence in
        // the extra-info part.
        $matches = [];
        preg_match('/in (.+) and will be removed from (?U)(.+)\. (.+)$/', $depText, $matches);
        preg_match('/in (.+) and is removed from (?U)(.+)\. (.+)$/', $depText, $matches);
        // There should be 4 items in $matches: 0 is full text, 1 = in-version,
        // 2 = removal-version, 3 = extra-info.
        if (count($matches) !== 4) {
            $error = "The deprecation text '@deprecated %s' does not match the standard format: @deprecated in %%in-version%% and will be removed from %%removal-version%%. %%extra-info%%.";
            $error = "The text '@deprecated %s' does not match the standard format: @deprecated in %%deprecation-version%% and is removed from %%removal-version%%. %%extra-info%%.";
            $phpcsFile->addError($error, $stackPtr, 'IncorrectTextLayout', [$depText]);
        } else {
            // The text follows the basic layout. Now check that the versions
            // match drupal:n.n.n or project:n.x-n.n. The text must be all lower
            // case and numbers can be one or two digits.
            foreach (['in-version' => $matches[1], 'removal-version' => $matches[2]] as $name => $version) {
            foreach (['deprecation-version' => $matches[1], 'removal-version' => $matches[2]] as $name => $version) {
                if (preg_match('/^drupal:\d{1,2}\.\d{1,2}\.\d{1,2}$/', $version) === 0
                    && preg_match('/^[a-z\d_]+:\d{1,2}\.x\-\d{1,2}\.\d{1,2}$/', $version) === 0
                ) {
                    $error = "The deprecation %s '%s' does not match the standard: drupal:n.n.n or project:n.x-n.n";
                    $error = "The %s '%s' does not match the lower-case machine-name standard: drupal:n.n.n or project:n.x-n.n";
                    $phpcsFile->addWarning($error, $stackPtr, 'DeprecatedVersionFormat', [$name, $version]);
                }
            }
+62 −26
Original line number Diff line number Diff line
@@ -96,43 +96,79 @@ class FunctionTriggerErrorSniff extends FunctionCall
            $messageText = implode(' ', $messageParts);
        }//end if

        // The standard format for @trigger_error() is:
        // %thing% is deprecated in %in-version%. %extra-info%. See %cr-link%
        // Use (?U) 'ungreedy' before the version so that only the text up to
        // the first '. ' is matched, as there may be more than one sentence in
        // the extra-info part.
        // Check if there is a @deprecated tag in an associated doc comment
        // block. If the @trigger_error was level 0 (entire class or file) then
        // try to find a doc comment after the trigger_error also at level 0.
        // If the @trigger_error was at level > 0 it means it is inside a
        // function so search backwards for the function comment block, which
        // will be at one level lower.
        $strictStandard    = false;
        $triggerErrorLevel = $tokens[$stackPtr]['level'];
        if ($triggerErrorLevel === 0) {
            $requiredLevel = 0;
            $block         = $phpcsFile->findNext(T_DOC_COMMENT_OPEN_TAG, $argument['start']);
        } else {
            $requiredLevel = ($triggerErrorLevel - 1);
            $block         = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $argument['start']);
        }

        if (isset($block) === true && $tokens[$block]['level'] === $requiredLevel && isset($tokens[$block]['comment_tags']) === true) {
            foreach ($tokens[$block]['comment_tags'] as $tag) {
                $strictStandard = $strictStandard || (strtolower($tokens[$tag]['content']) === '@deprecated');
            }
        }

        // The string standard format for @trigger_error() is:
        // %thing% is deprecated in %deprecation-version% and is removed in
        // %removal-version%. %extra-info%. See %cr-link%
        // For the 'relaxed' standard the 'and is removed in' can be replaced
        // with any text.
        $matches = [];
        preg_match('/(.+) is deprecated in (?U)(.+)\. (.+)\. See (.+)$/', $messageText, $matches);
        if ($strictStandard === true) {
            // Use (?U) 'ungreedy' before the version so that only the text up
            // to the first period followed by a space is matched, as there may
            // be more than one sentence in the extra-info part.
            preg_match('/(.+) is deprecated in (\S+) (and is removed from) (?U)(.+)\. (.*)\. See (\S+)$/', $messageText, $matches);
            $sniff = 'TriggerErrorTextLayoutStrict';
            $error = "The trigger_error message '%s' does not match the strict standard format: %%thing%% is deprecated in %%deprecation-version%% and is removed from %%removal-version%%. %%extra-info%%. See %%cr-link%%";
        } else {
            // Allow %extra-info% to be empty as this is optional in the relaxed
            // version.
            preg_match('/(.+) is deprecated in (\S+) (?U)(.+) (\S+)\. (.*)See (\S+)$/', $messageText, $matches);
            $sniff = 'TriggerErrorTextLayoutRelaxed';
            $error = "The trigger_error message '%s' does not match the relaxed standard format: %%thing%% is deprecated in %%deprecation-version%% any free text %%removal-version%%. %%extra-info%%. See %%cr-link%%";
        }

        // 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%%";
            $phpcsFile->addError($error, $argument['start'], 'TriggerErrorTextLayout', [$messageText]);
        // There should be 7 items in $matches: 0 is full text, 1 = thing,
        // 2 = deprecation-version, 3 = middle text, 4 = removal-version,
        // 5 = extra-info, 6 = cr-link.
        if (count($matches) !== 7) {
            $phpcsFile->addError($error, $argument['start'], $sniff, [$messageText]);
        } else {
            // The text follows the basic layout. Now check that the version
            // matches drupal:n.n.n or project:n.x-n.n. The text must be all
            // lower case and numbers can be one or two digits.
            $inVersion = $matches[2];
            if (preg_match('/^drupal:\d{1,2}\.\d{1,2}\.\d{1,2}$/', $inVersion) === 0
                && preg_match('/^[a-z\d_]+:\d{1,2}\.x\-\d{1,2}\.\d{1,2}$/', $inVersion) === 0
            foreach (['deprecation-version' => $matches[2], 'removal-version' => $matches[4]] as $name => $version) {
                if (preg_match('/^drupal:\d{1,2}\.\d{1,2}\.\d{1,2}$/', $version) === 0
                    && preg_match('/^[a-z\d_]+:\d{1,2}\.x\-\d{1,2}\.\d{1,2}$/', $version) === 0
                ) {
                $error = "The deprecation version '%s' does not match the standard: drupal:n.n.n or project:n.x-n.n";
                $phpcsFile->addWarning($error, $argument['start'], 'TriggerErrorVersion', [$inVersion]);
                    $error = "The %s '%s' does not match the lower-case machine-name standard: drupal:n.n.n or project:n.x-n.n";
                    $phpcsFile->addWarning($error, $argument['start'], 'TriggerErrorVersion', [$name, $version]);
                }
            }

            // Check the 'See' link.
            $crLink = $matches[4];
            $crLink = $matches[6];
            // Allow for the alternative 'node' or 'project/aaa/issues' format.
            preg_match('[^http(s*)://www.drupal.org/(node|project/\w+/issues)/(\d+)(\.*)$]', $crLink, $matches);
            // If matches[4] is not blank it means that the url is correct but it
            // ends with a period. As this can be a common mistake give a specific
            // message to assist in fixing.
            if (isset($matches[4]) === true && empty($matches[4]) === false) {
                $error = "The 'See' url '%s' should not end with a period.";
            preg_match('[^http(s*)://www.drupal.org/(node|project/\w+/issues)/(\d+)(\.*)$]', $crLink, $crMatches);
            // If cr_matches[4] is not blank it means that the url is correct
            // but it ends with a period. As this can be a common mistake give a
            // specific message to assist in fixing.
            if (isset($crMatches[4]) === true && empty($crMatches[4]) === false) {
                $error = "The url '%s' should not end with a period.";
                $phpcsFile->addWarning($error, $argument['start'], 'TriggerErrorPeriodAfterSeeUrl', [$crLink]);
            } else if (empty($matches) === true) {
                $error = "The 'See' url '%s' does not match the standard: http(s)://www.drupal.org/node/n or http(s)://www.drupal.org/project/aaa/issues/n";
            } else if (empty($crMatches) === true) {
                $error = "The url '%s' does not match the standard: http(s)://www.drupal.org/node/n or http(s)://www.drupal.org/project/aaa/issues/n";
                $phpcsFile->addWarning($error, $argument['start'], 'TriggerErrorSeeUrlFormat', [$crLink]);
            }
        }//end if
+11 −11
Original line number Diff line number Diff line
@@ -3,8 +3,8 @@
/**
 * @file
 *
 * @deprecated in drupal:8.4.0 and will be removed from drupal:8.6.0.
 * Use \Drupal\Tests\comment\Functional\Views\CommentTestBase instead.
 * @deprecated in drupal:8.4.0 and is removed from drupal:8.6.0.
 * Use something else instead.
 * This file doc block passes the 'Deprecated' standards checks.
 *
 * @see https://www.drupal.org/node/1234
@@ -13,7 +13,7 @@
/**
 * This doc block will also pass the 'Deprecated' standards checks.
 *
 * @deprecated in drupal:8.6.0 and will be removed from drupal:9.0.0.
 * @deprecated in drupal:8.6.0 and is removed from drupal:9.0.0.
 *   This function block is OK.
 * @see https://www.drupal.org/project/drupal/issues/5678
 */
@@ -21,8 +21,8 @@ function x() {
}

/**
 * @deprecated in Drupal 8.4. This will fail basic layout.
 *   Use \Drupal\Tests\comment\Functional\Views\CommentTestBase instead.
 * @deprecated in Drupal 8.4.
 *   This will fail basic layout.
 *   It also has no 'see' url.
 */

@@ -32,9 +32,9 @@ function x() {
 */

/**
 * This function does something.
 * This block will produce three warnings.
 *
 * @Deprecated in drupal 8.4.0 and will be removed from drupal:9.0.
 * @Deprecated in drupal 8.4.0 and is removed from drupal:9.0.
 *   Both version formats are incorrect, and the see url is wrong.
 * @see http:drupal.org
 */
@@ -42,9 +42,9 @@ function y() {
}

/**
 * This function does something.
 * This block will produce three warnings.
 *
 * @deprecated in My_Proj:8.x-1.8 and will be removed from my_Proj:8.x-2.
 * @deprecated in My_Proj:8.x-1.8 and is removed from my_Proj:8.x-2.
 *   Both version formats are incorrect, and the see url ends with a period.
 * @see http://www.drupal.org/node/7890.
 */
@@ -52,9 +52,9 @@ function z() {
}

/**
 * This doc block will also pass the 'Deprecated' standards checks.
 * This doc block will pass the 'Deprecated' standards checks.
 *
 * @deprecated in myproj:8.x-1.8 and will be removed from myproj:8.x-2.0.
 * @deprecated in myproj:8.x-1.8 and is removed from myproj:8.x-2.0.
 *   This function block is OK.
 * @see http://www.drupal.org/node/7890
 */
+66 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Example code lines for FunctionTriggerError sniff test.
 */

// Passes.
// No second parameter, so cannot fail it.
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0');
// Not E_USER_DEPRECATED, so cannot fail it.
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_SOMETHING_ELSE);
// All correct.
@trigger_error('\Drupal\Some\Class\Foo is deprecated in drupal:8.5.0 and wont work in in drupal:9.0.0. Use \Drupal\Foo\Bar instead. See http://www.drupal.org/node/2908490', E_USER_DEPRECATED);
// Alternative using __NAMESPACE__.
@trigger_error(__NAMESPACE__ . '\Foo is deprecated in drupal:8.5.12 and wont work in drupal:9.0.0. Use \Drupal\Foo\Bar instead. See http://www.drupal.org/node/2908490', E_USER_DEPRECATED);
// Alternative https url and cater for E_User_Deprecated in non-upper case.
@trigger_error("\Drupal\Some\Class\Foo is deprecated in drupal:8.11.0 and wont work in drupal:9.0.0. Use \Drupal\Foo. See https://www.drupal.org/project/drupal/issues/2908490", E_User_Deprecated);
// Alternative syntax for contrib project and issue url.
@trigger_error('Function foo_bar() is deprecated in my_module:8.x-2.0 and wont work in my_module:9.x-1.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 and wont work in drupal:9.0.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 and wont work in drupal:9.0.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 and wont work in 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.
@trigger_error('foo() is deprecated drupal:8.5.0 and will not work in drupal:9.0.0. Use bar() instead. See https://www.drupal.org/node/123', E_USER_DEPRECATED);
// Wrong first part - bad spacing.
@trigger_error('foo() is   deprecated in drupal:8.5.0 and will not work in drupal:9.0.0. Use bar() instead. See https://www.drupal.org/node/123', E_USER_DEPRECATED);
// No removal version.
@trigger_error('foo() is deprecated in drupal:8.5.0. See https://www.drupal.org/node/123', E_User_Deprecated);
// Incorrect see url - extra text before http.
@trigger_error('foo() is deprecated in drupal:8.6.0 and will not work in drupal:9.0.0. Use bar() instead. See this http://www.drupal.org/node/123', E_USER_DEPRECATED);
// Incorrect see url - extra text after link.
@trigger_error('foo() is deprecated in drupal:8.6.0 and will not work in drupal:9.0.0. Use bar() instead. See http://www.drupal.org/node/123 for details', E_USER_DEPRECATED);
// Ensure that concatenation at end of string is checked.
@trigger_error('foo() is deprecated in drupal:8.6.0 and will not work in drupal:9.0.0. Use bar() instead. See https://www.drupal.org/node/123' . __FUNCTION__ , E_USER_DEPRECATED);
// WARNINGS for version. Version with only one component.
@trigger_error('foo() is deprecated in drupal:8 and will not work in drupal:9.0.0. Use \bar\baz instead. See http://www.drupal.org/node/123', E_USER_DEPRECATED);
// Wrong first part - no final dot so next .
@trigger_error('foo() is deprecated in drupal:8.5.0 Use bar() instead. See https://www.drupal.org/node/123', E_USER_DEPRECATED);
// Version with only two components.
@trigger_error('foo() is deprecated in drupal:8.6 and will not work in drupal:9.0.0. Use \bar\baz instead. See http://www.drupal.org/node/123', E_USER_DEPRECATED);
// Version without :
@trigger_error('foo() is deprecated in drupal 8.6.0 and will not work in drupal:9.0.0. Use \bar\baz instead. See http://www.drupal.org/node/123', E_USER_DEPRECATED);
// Wrong case for Drupal.
@trigger_error('foo() is deprecated in Drupal:8.6.0 and will not work in drupal:9.0.0. Use \bar\baz instead. See http://www.drupal.org/node/123', E_USER_DEPRECATED);
// Non digit in core format.
@trigger_error('foo() is deprecated in drupal:8.6.x and will not work in drupal:9.0.0. Use \bar\baz instead. See http://www.drupal.org/node/123', E_USER_DEPRECATED);
// No 'x' in contrib format.
@trigger_error('foo() is deprecated in my_project:8.6-2.x and will not work in my_project:9.x-1.0. Use bar() instead. See http://www.drupal.org/node/123', E_USER_DEPRECATED);
// Version more than 2 digits.
@trigger_error('foo() is deprecated in project:19.x-2.0 and will not work in project:182.x-2.0. Use bar() instead. See http://www.drupal.org/node/123', E_USER_DEPRECATED);
// Wrong case for Project.
@trigger_error('foo() is deprecated in project:8.x-2.0 and will not work in Project:9.x-1.0. Use bar() instead. See http://www.drupal.org/node/123', E_USER_DEPRECATED);
// WARNINGS for 'see' url. Incorrect see url - no http.
@trigger_error('foo() is deprecated in drupal:8.6.0 and will not work in drupal:9.0.0. Use bar() instead. See www.drupal.org/node/123', E_USER_DEPRECATED);
// Incorrect see url - no node id.
@trigger_error('foo() is deprecated in drupal:8.6.0 and will not work in drupal:9.0.0. Use bar() instead. See http://www.drupal.org/node/', E_USER_DEPRECATED);
// Incorrect see url - non-numeric node id.
@trigger_error('foo() is deprecated in drupal:8.6.0 and will not work in drupal:9.0.0. Use bar() instead. See https://www.drupal.org/node/123a', E_USER_DEPRECATED);
// Do not want period after url.
@trigger_error('foo() is deprecated in drupal:8.6.0 and will not work in drupal:9.0.0. Use bar() instead. See https://www.drupal.org/node/123.', E_USER_DEPRECATED);
+31 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Example code for strict deprecated FunctionTriggerError sniff test.
 */

/**
 * Test function one.
 *
 * @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use bar()
 *   instead.
 * @see https://www.drupal.org/node/123
 */
function foo1() {
  // This conforms the strict version of the trigger_error deprecation standard.
  @trigger_error('Function foo1() is deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use bar() instead. See https://www.drupal.org/node/123', E_USER_DEPRECATED);
}

/**
 * Test function two.
 *
 * @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use bar()
 *   instead.
 * @see https://www.drupal.org/node/123
 */
function foo2() {
  // This fails the strict version of the trigger_error deprecation standard.
  @trigger_error('Function foo2() is deprecated in drupal:8.5.0 but instead use bar() in drupal:9.0.0. See https://www.drupal.org/node/123', E_USER_DEPRECATED);
}
Loading