| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Squiz_Sniffs_Commenting_EmptyCatchCommentSniff.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
11 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
12 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
13 |
* @version CVS: $Id: DocCommentAlignmentSniff.php 252415 2008-02-06 22:48:09Z squiz $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Squiz_Sniffs_Commenting_DocCommentAlignmentSniff.
|
|
|
19 |
*
|
|
|
20 |
* Tests that the stars in a doc comment align correctly.
|
|
|
21 |
*
|
|
|
22 |
* @category PHP
|
|
|
23 |
* @package PHP_CodeSniffer
|
|
|
24 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
25 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
26 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
27 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
28 |
* @version Release: 1.2.1
|
|
|
29 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
30 |
*/
|
|
|
31 |
class Squiz_Sniffs_Commenting_DocCommentAlignmentSniff implements PHP_CodeSniffer_Sniff
|
|
|
32 |
{
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Returns an array of tokens this test wants to listen for.
|
|
|
37 |
*
|
|
|
38 |
* @return array
|
|
|
39 |
*/
|
|
|
40 |
public function register()
|
|
|
41 |
{
|
|
|
42 |
return array(T_DOC_COMMENT);
|
|
|
43 |
|
|
|
44 |
}//end register()
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Processes this test, when one of its tokens is encountered.
|
|
|
49 |
*
|
|
|
50 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
51 |
* @param int $stackPtr The position of the current token
|
|
|
52 |
* in the stack passed in $tokens.
|
|
|
53 |
*
|
|
|
54 |
* @return void
|
|
|
55 |
*/
|
|
|
56 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
57 |
{
|
|
|
58 |
$tokens = $phpcsFile->getTokens();
|
|
|
59 |
|
|
|
60 |
// We are only interested in function/class/interface doc block comments.
|
|
|
61 |
$nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
|
|
|
62 |
$ignore = array(
|
|
|
63 |
T_CLASS,
|
|
|
64 |
T_INTERFACE,
|
|
|
65 |
T_FUNCTION,
|
|
|
66 |
T_PUBLIC,
|
|
|
67 |
T_PRIVATE,
|
|
|
68 |
T_PROTECTED,
|
|
|
69 |
T_STATIC,
|
|
|
70 |
T_ABSTRACT,
|
|
|
71 |
);
|
|
|
72 |
if (in_array($tokens[$nextToken]['code'], $ignore) === false) {
|
|
|
73 |
// Could be a file comment.
|
|
|
74 |
$prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
|
|
|
75 |
if ($tokens[$prevToken]['code'] !== T_OPEN_TAG) {
|
|
|
76 |
return;
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
// We only want to get the first comment in a block. If there is
|
|
|
81 |
// a comment on the line before this one, return.
|
|
|
82 |
$docComment = $phpcsFile->findPrevious(T_DOC_COMMENT, ($stackPtr - 1));
|
|
|
83 |
if ($docComment !== false) {
|
|
|
84 |
if ($tokens[$docComment]['line'] === ($tokens[$stackPtr]['line'] - 1)) {
|
|
|
85 |
return;
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
$comments = array($stackPtr);
|
|
|
90 |
$currentComment = $stackPtr;
|
|
|
91 |
$lastComment = $stackPtr;
|
|
|
92 |
while (($currentComment = $phpcsFile->findNext(T_DOC_COMMENT, ($currentComment + 1))) !== false) {
|
|
|
93 |
if ($tokens[$lastComment]['line'] === ($tokens[$currentComment]['line'] - 1)) {
|
|
|
94 |
$comments[] = $currentComment;
|
|
|
95 |
$lastComment = $currentComment;
|
|
|
96 |
} else {
|
|
|
97 |
break;
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
// The $comments array now contains pointers to each token in the
|
|
|
102 |
// comment block.
|
|
|
103 |
$requiredColumn = strpos($tokens[$stackPtr]['content'], '*');
|
|
|
104 |
$requiredColumn += $tokens[$stackPtr]['column'];
|
|
|
105 |
|
|
|
106 |
foreach ($comments as $commentPointer) {
|
|
|
107 |
// Check the spacing after each asterisk.
|
|
|
108 |
$content = $tokens[$commentPointer]['content'];
|
|
|
109 |
$firstChar = substr($content, 0, 1);
|
|
|
110 |
$lastChar = substr($content, -1);
|
|
|
111 |
if ($firstChar !== '/' && $lastChar !== '/') {
|
|
|
112 |
$matches = array();
|
|
|
113 |
preg_match('|^(\s+)?\*(\s+)?@|', $content, $matches);
|
|
|
114 |
if (empty($matches) === false) {
|
|
|
115 |
if (isset($matches[2]) === false) {
|
|
|
116 |
$error = "Expected 1 space between asterisk and tag; 0 found";
|
|
|
117 |
$phpcsFile->addError($error, $commentPointer);
|
|
|
118 |
} else {
|
|
|
119 |
$length = strlen($matches[2]);
|
|
|
120 |
if ($length !== 1) {
|
|
|
121 |
$error = "Expected 1 space between asterisk and tag; $length found";
|
|
|
122 |
$phpcsFile->addError($error, $commentPointer);
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
}//end foreach
|
|
|
127 |
|
|
|
128 |
// Check the alignment of each asterisk.
|
|
|
129 |
$currentColumn = strpos($content, '*');
|
|
|
130 |
$currentColumn += $tokens[$commentPointer]['column'];
|
|
|
131 |
|
|
|
132 |
if ($currentColumn === $requiredColumn) {
|
|
|
133 |
// Star is aligned correctly.
|
|
|
134 |
continue;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
$expected = ($requiredColumn - 1);
|
|
|
138 |
$expected .= ($expected === 1) ? ' space' : ' spaces';
|
|
|
139 |
$found = ($currentColumn - 1);
|
|
|
140 |
$error = "Expected $expected before asterisk; $found found";
|
|
|
141 |
$phpcsFile->addError($error, $commentPointer);
|
|
|
142 |
}//end foreach
|
|
|
143 |
|
|
|
144 |
}//end process()
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
}//end class
|
|
|
148 |
|
|
|
149 |
?>
|