Mercurial > hg > cmmr2012-drupal-site
comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/DeprecatedSniff.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
1 <?php | |
2 /** | |
3 * \Drupal\Sniffs\Commenting\DeprecatedSniff. | |
4 * | |
5 * @category PHP | |
6 * @package PHP_CodeSniffer | |
7 * @link http://pear.php.net/package/PHP_CodeSniffer | |
8 */ | |
9 | |
10 namespace Drupal\Sniffs\Commenting; | |
11 | |
12 use PHP_CodeSniffer\Files\File; | |
13 use PHP_CodeSniffer\Sniffs\Sniff; | |
14 | |
15 /** | |
16 * Ensures standard format of a @deprecated text. | |
17 * | |
18 * @category PHP | |
19 * @package PHP_CodeSniffer | |
20 * @link http://pear.php.net/package/PHP_CodeSniffer | |
21 */ | |
22 class DeprecatedSniff implements Sniff | |
23 { | |
24 | |
25 | |
26 /** | |
27 * Returns an array of tokens this test wants to listen for. | |
28 * | |
29 * @return array | |
30 */ | |
31 public function register() | |
32 { | |
33 return array(T_DOC_COMMENT_TAG); | |
34 | |
35 }//end register() | |
36 | |
37 | |
38 /** | |
39 * Processes this test, when one of its tokens is encountered. | |
40 * | |
41 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | |
42 * @param int $stackPtr The position of the current token | |
43 * in the stack passed in $tokens. | |
44 * | |
45 * @return void | |
46 */ | |
47 public function process(File $phpcsFile, $stackPtr) | |
48 { | |
49 $tokens = $phpcsFile->getTokens(); | |
50 | |
51 // Only process @deprecated tags. | |
52 if (strcasecmp($tokens[$stackPtr]['content'], '@deprecated') !== 0) { | |
53 return; | |
54 } | |
55 | |
56 // Get the end point of the comment block which has the deprecated tag. | |
57 $commentEnd = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, ($stackPtr + 1)); | |
58 | |
59 // Get the full @deprecated text which may cover multiple lines. | |
60 $depText = ''; | |
61 $depEnd = ($stackPtr + 1); | |
62 for ($i = ($stackPtr + 1); $i < $commentEnd; $i++) { | |
63 if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) { | |
64 if ($tokens[$i]['line'] <= ($tokens[$depEnd]['line'] + 1)) { | |
65 $depText .= ' '.$tokens[$i]['content']; | |
66 $depEnd = $i; | |
67 } else { | |
68 break; | |
69 } | |
70 } | |
71 | |
72 // Found another tag, so we have all the deprecation text. | |
73 if ($tokens[$i]['code'] === T_DOC_COMMENT_TAG) { | |
74 break; | |
75 } | |
76 } | |
77 | |
78 $depText = trim($depText); | |
79 | |
80 // The standard format for the deprecation text is: | |
81 // @deprecated in %in-version% and will be removed from %removal-version%. %extra-info%. | |
82 // Use (?U) 'ungreedy' before the version so that only the text up to | |
83 // the first '. ' is matched, as there may be more than one sentence in | |
84 // the extra-info part. | |
85 $matches = array(); | |
86 preg_match('/in (.+) and will be removed from (?U)(.+)\. (.+)$/', $depText, $matches); | |
87 // There should be 4 items in $matches: 0 is full text, 1 = in-version, | |
88 // 2 = removal-version, 3 = extra-info. | |
89 if (count($matches) !== 4) { | |
90 $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%%."; | |
91 $phpcsFile->addError($error, $stackPtr, 'IncorrectTextLayout', array($depText)); | |
92 } else { | |
93 // The text follows the basic layout. Now check that the versions | |
94 // match drupal:n.n.n or project:n.x-n.n. The text must be all lower | |
95 // case and numbers can be one or two digits. | |
96 foreach (array('in-version' => $matches[1], 'removal-version' => $matches[2]) as $name => $version) { | |
97 if (preg_match('/^drupal:\d{1,2}\.\d{1,2}\.\d{1,2}$/', $version) === 0 | |
98 && preg_match('/^[a-z\d_]:\d{1,2}\.x\-\d{1,2}\.\d{1,2}$/', $version) === 0 | |
99 ) { | |
100 $error = "The deprecation %s '%s' does not match the standard: drupal:n.n.n or project:n.x-n.n"; | |
101 $phpcsFile->addWarning($error, $stackPtr, 'DeprecatedVersionFormat', array($name, $version)); | |
102 } | |
103 } | |
104 } | |
105 | |
106 // The next tag in this comment block after @deprecated must be @see. | |
107 $seeTag = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($stackPtr + 1), $commentEnd, false, '@see'); | |
108 if ($seeTag === false) { | |
109 $error = 'Each @deprecated tag must have a @see tag immediately following it.'; | |
110 $phpcsFile->addError($error, $stackPtr, 'DeprecatedMissingSeeTag'); | |
111 return; | |
112 } | |
113 | |
114 // Check the format of the @see url. | |
115 $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, ($seeTag + 1), $commentEnd); | |
116 $cr_link = $tokens[$string]['content']; | |
117 // Allow for the alternative 'node' or 'project/aaa/issues' format. | |
118 preg_match('[^http(s*)://www.drupal.org/(node|project/\w+/issues)/(\d+)(\.*)$]', $cr_link, $matches); | |
119 // If matches[4] is not blank it means that the url is correct but it | |
120 // ends with a period. As this can be a common mistake give a specific | |
121 // message to assist in fixing. | |
122 if (isset($matches[4]) === true && empty($matches[4]) === false) { | |
123 $error = "The @see url '%s' should not end with a period."; | |
124 $phpcsFile->addWarning($error, $seeTag, 'DeprecatedPeriodAfterSeeUrl', array($cr_link)); | |
125 } else if (empty($matches) === true) { | |
126 $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"; | |
127 $phpcsFile->addWarning($error, $seeTag, 'DeprecatedWrongSeeUrlFormat', array($cr_link)); | |
128 } | |
129 | |
130 }//end process() | |
131 | |
132 | |
133 }//end class |