comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /**
3 * Parses and verifies the class doc comment.
4 *
5 * @category PHP
6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */
9
10 /**
11 * Checks that comment doc blocks exist on classes, interfaces and traits. Largely
12 * copied from Squiz_Sniffs_Commenting_ClassCommentSniff.
13 *
14 * @category PHP
15 * @package PHP_CodeSniffer
16 * @author Greg Sherwood <gsherwood@squiz.net>
17 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
18 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
19 * @version Release: @package_version@
20 * @link http://pear.php.net/package/PHP_CodeSniffer
21 */
22 class Drupal_Sniffs_Commenting_ClassCommentSniff implements PHP_CodeSniffer_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(
34 T_CLASS,
35 T_INTERFACE,
36 T_TRAIT,
37 );
38
39 }//end register()
40
41
42 /**
43 * Processes this test, when one of its tokens is encountered.
44 *
45 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
46 * @param int $stackPtr The position of the current token
47 * in the stack passed in $tokens.
48 *
49 * @return void
50 */
51 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
52 {
53 $tokens = $phpcsFile->getTokens();
54 $find = PHP_CodeSniffer_Tokens::$methodPrefixes;
55 $find[] = T_WHITESPACE;
56 $name = $tokens[$stackPtr]['content'];
57
58 $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
59 if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
60 && $tokens[$commentEnd]['code'] !== T_COMMENT
61 ) {
62 $fix = $phpcsFile->addFixableError('Missing %s doc comment', $stackPtr, 'Missing', array($name));
63 if ($fix === true) {
64 $phpcsFile->fixer->addContent($commentEnd, "\n/**\n *\n */");
65 }
66
67 return;
68 }
69
70 // Try and determine if this is a file comment instead of a class comment.
71 if ($tokens[$commentEnd]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
72 $start = ($tokens[$commentEnd]['comment_opener'] - 1);
73 } else {
74 $start = ($commentEnd - 1);
75 }
76
77 $fileTag = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($start + 1), $commentEnd, false, '@file');
78 if ($fileTag !== false) {
79 // This is a file comment.
80 $fix = $phpcsFile->addFixableError('Missing %s doc comment', $stackPtr, 'Missing', array($name));
81 if ($fix === true) {
82 $phpcsFile->fixer->addContent($commentEnd, "\n/**\n *\n */");
83 }
84
85 return;
86 }
87
88 if ($tokens[$commentEnd]['code'] === T_COMMENT) {
89 $fix = $phpcsFile->addFixableError('You must use "/**" style comments for a %s comment', $stackPtr, 'WrongStyle', array($name));
90 if ($fix === true) {
91 // Convert the comment into a doc comment.
92 $phpcsFile->fixer->beginChangeset();
93 $comment = '';
94 for ($i = $commentEnd; $tokens[$i]['code'] === T_COMMENT; $i--) {
95 $comment = ' *'.ltrim($tokens[$i]['content'], '/* ').$comment;
96 $phpcsFile->fixer->replaceToken($i, '');
97 }
98
99 $phpcsFile->fixer->replaceToken($commentEnd, "/**\n".rtrim($comment, "*/\n")."\n */");
100 $phpcsFile->fixer->endChangeset();
101 }
102
103 return;
104 }
105
106 if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
107 $error = 'There must be exactly one newline after the %s comment';
108 $fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter', array($name));
109 if ($fix === true) {
110 $phpcsFile->fixer->beginChangeset();
111 for ($i = ($commentEnd + 1); $tokens[$i]['code'] === T_WHITESPACE && $i < $stackPtr; $i++) {
112 $phpcsFile->fixer->replaceToken($i, '');
113 }
114
115 $phpcsFile->fixer->addContent($commentEnd, "\n");
116 $phpcsFile->fixer->endChangeset();
117 }
118 }
119
120 }//end process()
121
122
123 }//end class