Mercurial > hg > isophonics-drupal-site
comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 4c8ae668cc8c |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
5 * @category PHP | 5 * @category PHP |
6 * @package PHP_CodeSniffer | 6 * @package PHP_CodeSniffer |
7 * @link http://pear.php.net/package/PHP_CodeSniffer | 7 * @link http://pear.php.net/package/PHP_CodeSniffer |
8 */ | 8 */ |
9 | 9 |
10 namespace Drupal\Sniffs\Commenting; | |
11 | |
12 use PHP_CodeSniffer\Files\File; | |
13 use PHP_CodeSniffer\Sniffs\Sniff; | |
14 use PHP_CodeSniffer\Util\Tokens; | |
15 | |
10 /** | 16 /** |
11 * Parses and verifies the doc comments for functions. Largely copied from | 17 * Parses and verifies the doc comments for functions. Largely copied from |
12 * Squiz_Sniffs_Commenting_FunctionCommentSniff. | 18 * PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FunctionCommentSniff. |
13 * | 19 * |
14 * @category PHP | 20 * @category PHP |
15 * @package PHP_CodeSniffer | 21 * @package PHP_CodeSniffer |
16 * @link http://pear.php.net/package/PHP_CodeSniffer | 22 * @link http://pear.php.net/package/PHP_CodeSniffer |
17 */ | 23 */ |
18 class Drupal_Sniffs_Commenting_FunctionCommentSniff implements PHP_CodeSniffer_Sniff | 24 class FunctionCommentSniff implements Sniff |
19 { | 25 { |
20 | 26 |
21 /** | 27 /** |
22 * A map of invalid data types to valid ones for param and return documentation. | 28 * A map of invalid data types to valid ones for param and return documentation. |
23 * | 29 * |
70 | 76 |
71 | 77 |
72 /** | 78 /** |
73 * Processes this test, when one of its tokens is encountered. | 79 * Processes this test, when one of its tokens is encountered. |
74 * | 80 * |
75 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | 81 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
76 * @param int $stackPtr The position of the current token | 82 * @param int $stackPtr The position of the current token |
77 * in the stack passed in $tokens. | 83 * in the stack passed in $tokens. |
78 * | 84 * |
79 * @return void | 85 * @return void |
80 */ | 86 */ |
81 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | 87 public function process(File $phpcsFile, $stackPtr) |
82 { | 88 { |
83 $tokens = $phpcsFile->getTokens(); | 89 $tokens = $phpcsFile->getTokens(); |
84 $find = PHP_CodeSniffer_Tokens::$methodPrefixes; | 90 $find = Tokens::$methodPrefixes; |
85 $find[] = T_WHITESPACE; | 91 $find[] = T_WHITESPACE; |
86 | 92 |
87 $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); | 93 $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); |
88 $beforeCommentEnd = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($commentEnd - 1), null, true); | 94 $beforeCommentEnd = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($commentEnd - 1), null, true); |
89 if (($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG | 95 if (($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
90 && $tokens[$commentEnd]['code'] !== T_COMMENT) | 96 && $tokens[$commentEnd]['code'] !== T_COMMENT) |
91 || ($beforeCommentEnd !== false | 97 || ($beforeCommentEnd !== false |
92 // If there is something more on the line than just the comment then the | 98 // If there is something more on the line than just the comment then the |
93 // comment does not belong to the function. | 99 // comment does not belong to the function. |
160 | 166 |
161 | 167 |
162 /** | 168 /** |
163 * Process the return comment of this function comment. | 169 * Process the return comment of this function comment. |
164 * | 170 * |
165 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | 171 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
166 * @param int $stackPtr The position of the current token | 172 * @param int $stackPtr The position of the current token |
167 * in the stack passed in $tokens. | 173 * in the stack passed in $tokens. |
168 * @param int $commentStart The position in the stack where the comment started. | 174 * @param int $commentStart The position in the stack where the comment started. |
169 * | 175 * |
170 * @return void | 176 * @return void |
171 */ | 177 */ |
172 protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) | 178 protected function processReturn(File $phpcsFile, $stackPtr, $commentStart) |
173 { | 179 { |
174 $tokens = $phpcsFile->getTokens(); | 180 $tokens = $phpcsFile->getTokens(); |
175 | 181 |
176 // Skip constructor and destructor. | 182 // Skip constructor and destructor. |
177 $className = ''; | 183 $className = ''; |
363 | 369 |
364 | 370 |
365 /** | 371 /** |
366 * Process any throw tags that this function comment has. | 372 * Process any throw tags that this function comment has. |
367 * | 373 * |
368 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | 374 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
369 * @param int $stackPtr The position of the current token | 375 * @param int $stackPtr The position of the current token |
370 * in the stack passed in $tokens. | 376 * in the stack passed in $tokens. |
371 * @param int $commentStart The position in the stack where the comment started. | 377 * @param int $commentStart The position in the stack where the comment started. |
372 * | 378 * |
373 * @return void | 379 * @return void |
374 */ | 380 */ |
375 protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) | 381 protected function processThrows(File $phpcsFile, $stackPtr, $commentStart) |
376 { | 382 { |
377 $tokens = $phpcsFile->getTokens(); | 383 $tokens = $phpcsFile->getTokens(); |
378 | 384 |
379 foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { | 385 foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { |
380 if ($tokens[$tag]['content'] !== '@throws') { | 386 if ($tokens[$tag]['content'] !== '@throws') { |
443 | 449 |
444 | 450 |
445 /** | 451 /** |
446 * Process the function parameter comments. | 452 * Process the function parameter comments. |
447 * | 453 * |
448 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | 454 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
449 * @param int $stackPtr The position of the current token | 455 * @param int $stackPtr The position of the current token |
450 * in the stack passed in $tokens. | 456 * in the stack passed in $tokens. |
451 * @param int $commentStart The position in the stack where the comment started. | 457 * @param int $commentStart The position in the stack where the comment started. |
452 * | 458 * |
453 * @return void | 459 * @return void |
454 */ | 460 */ |
455 protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) | 461 protected function processParams(File $phpcsFile, $stackPtr, $commentStart) |
456 { | 462 { |
457 $tokens = $phpcsFile->getTokens(); | 463 $tokens = $phpcsFile->getTokens(); |
458 | 464 |
459 $params = array(); | 465 $params = array(); |
460 $maxType = 0; | 466 $maxType = 0; |
566 if ($comment === '') { | 572 if ($comment === '') { |
567 $error = 'Missing parameter comment'; | 573 $error = 'Missing parameter comment'; |
568 $phpcsFile->addError($error, $tag, 'MissingParamComment'); | 574 $phpcsFile->addError($error, $tag, 'MissingParamComment'); |
569 $commentLines[] = array('comment' => ''); | 575 $commentLines[] = array('comment' => ''); |
570 }//end if | 576 }//end if |
577 | |
571 $variableArguments = false; | 578 $variableArguments = false; |
572 // Allow the "..." @param doc for a variable number of parameters. | 579 // Allow the "..." @param doc for a variable number of parameters. |
573 // This could happen with type defined as @param array ... or | 580 // This could happen with type defined as @param array ... or |
574 // without type defined as @param ... | 581 // without type defined as @param ... |
575 if ($tokens[($tag + 2)]['content'] === '...' | 582 if ($tokens[($tag + 2)]['content'] === '...' |
870 | 877 |
871 | 878 |
872 /** | 879 /** |
873 * Process the function "see" comments. | 880 * Process the function "see" comments. |
874 * | 881 * |
875 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | 882 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
876 * @param int $stackPtr The position of the current token | 883 * @param int $stackPtr The position of the current token |
877 * in the stack passed in $tokens. | 884 * in the stack passed in $tokens. |
878 * @param int $commentStart The position in the stack where the comment started. | 885 * @param int $commentStart The position in the stack where the comment started. |
879 * | 886 * |
880 * @return void | 887 * @return void |
881 */ | 888 */ |
882 protected function processSees(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart) | 889 protected function processSees(File $phpcsFile, $stackPtr, $commentStart) |
883 { | 890 { |
884 $tokens = $phpcsFile->getTokens(); | 891 $tokens = $phpcsFile->getTokens(); |
885 foreach ($tokens[$commentStart]['comment_tags'] as $tag) { | 892 foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
886 if ($tokens[$tag]['content'] !== '@see') { | 893 if ($tokens[$tag]['content'] !== '@see') { |
887 continue; | 894 continue; |
936 | 943 |
937 | 944 |
938 /** | 945 /** |
939 * Checks if a used type hint is an alias defined by a "use" statement. | 946 * Checks if a used type hint is an alias defined by a "use" statement. |
940 * | 947 * |
941 * @param string $typeHint The type hint used. | 948 * @param string $typeHint The type hint used. |
942 * @param string $suggestedTypeHint The fully qualified type to | 949 * @param string $suggestedTypeHint The fully qualified type to |
943 * check against. | 950 * check against. |
944 * @param PHP_CodeSniffer_File $phpcsFile The file being checked. | 951 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being checked. |
945 * | 952 * |
946 * @return boolean | 953 * @return boolean |
947 */ | 954 */ |
948 protected function isAliasedType($typeHint, $suggestedTypeHint, PHP_CodeSniffer_File $phpcsFile) | 955 protected function isAliasedType($typeHint, $suggestedTypeHint, File $phpcsFile) |
949 { | 956 { |
950 $tokens = $phpcsFile->getTokens(); | 957 $tokens = $phpcsFile->getTokens(); |
951 | 958 |
952 // Iterate over all "use" statements in the file. | 959 // Iterate over all "use" statements in the file. |
953 $usePtr = 0; | 960 $usePtr = 0; |
962 continue; | 969 continue; |
963 } | 970 } |
964 | 971 |
965 // Now comes the original class name, possibly with namespace | 972 // Now comes the original class name, possibly with namespace |
966 // backslashes. | 973 // backslashes. |
967 $originalClass = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($usePtr + 1), null, true); | 974 $originalClass = $phpcsFile->findNext(Tokens::$emptyTokens, ($usePtr + 1), null, true); |
968 if ($originalClass === false || ($tokens[$originalClass]['code'] !== T_STRING | 975 if ($originalClass === false || ($tokens[$originalClass]['code'] !== T_STRING |
969 && $tokens[$originalClass]['code'] !== T_NS_SEPARATOR) | 976 && $tokens[$originalClass]['code'] !== T_NS_SEPARATOR) |
970 ) { | 977 ) { |
971 continue; | 978 continue; |
972 } | 979 } |
980 if (ltrim($originalClassName, '\\') !== ltrim($suggestedTypeHint, '\\')) { | 987 if (ltrim($originalClassName, '\\') !== ltrim($suggestedTypeHint, '\\')) { |
981 continue; | 988 continue; |
982 } | 989 } |
983 | 990 |
984 // Now comes the "as" keyword signaling an alias name for the class. | 991 // Now comes the "as" keyword signaling an alias name for the class. |
985 $asPtr = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($originalClass + 1), null, true); | 992 $asPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($originalClass + 1), null, true); |
986 if ($asPtr === false || $tokens[$asPtr]['code'] !== T_AS) { | 993 if ($asPtr === false || $tokens[$asPtr]['code'] !== T_AS) { |
987 continue; | 994 continue; |
988 } | 995 } |
989 | 996 |
990 // Now comes the name the class is aliased to. | 997 // Now comes the name the class is aliased to. |
991 $aliasPtr = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($asPtr + 1), null, true); | 998 $aliasPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($asPtr + 1), null, true); |
992 if ($aliasPtr === false || $tokens[$aliasPtr]['code'] !== T_STRING | 999 if ($aliasPtr === false || $tokens[$aliasPtr]['code'] !== T_STRING |
993 || $tokens[$aliasPtr]['content'] !== $typeHint | 1000 || $tokens[$aliasPtr]['content'] !== $typeHint |
994 ) { | 1001 ) { |
995 continue; | 1002 continue; |
996 } | 1003 } |