1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodeIgniter_Sniffs_Files_ClosingFileCommentSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Thomas Ernest <[email protected]> |
10
|
|
|
* @copyright 2006 Thomas Ernest |
11
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
12
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CodeIgniter_Sniffs_Files_ClosingFileCommentSniff. |
17
|
|
|
* |
18
|
|
|
* Ensures that a comment containing the file name is available at the end of file. |
19
|
|
|
* Only other comments and whitespaces are allowed to follow this specific comment. |
20
|
|
|
* |
21
|
|
|
* It may be all kind of comment like multi-line and inline C-style comments as |
22
|
|
|
* well as PERL-style comments. Any number of white may separate comment delimiters |
23
|
|
|
* from comment content. However, content has to be equal to template |
24
|
|
|
* "End of file <file_name>". Comparison between content and template is |
25
|
|
|
* case-sensitive. |
26
|
|
|
* |
27
|
|
|
* @category PHP |
28
|
|
|
* @package PHP_CodeSniffer |
29
|
|
|
* @author Thomas Ernest <[email protected]> |
30
|
|
|
* @copyright 2006 Thomas Ernest |
31
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
32
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
namespace CodeIgniter\Sniffs\Files; |
36
|
|
|
|
37
|
|
|
use PHP_CodeSniffer\Files\File; |
38
|
|
|
|
39
|
|
|
class ClosingFileCommentSniff extends AbstractClosingCommentSniff |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns an array of tokens this test wants to listen for. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function register() |
48
|
|
|
{ |
49
|
|
|
return array( |
50
|
|
|
T_OPEN_TAG, |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
}//end register() |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Processes this test, when one of its tokens is encountered. |
58
|
|
|
* |
59
|
|
|
* @param File $phpcsFile The current file being scanned. |
60
|
|
|
* @param int $stackPtr The position of the current token |
61
|
|
|
* in the stack passed in $tokens. |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function process(File $phpcsFile, $stackPtr) |
66
|
|
|
{ |
67
|
|
|
// We are only interested if this is the first open tag. |
68
|
|
View Code Duplication |
if ($stackPtr !== 0) { |
|
|
|
|
69
|
|
|
if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$fullFilename = $phpcsFile->getFilename(); |
75
|
|
|
$filename = basename($fullFilename); |
76
|
|
|
$commentTemplate = "End of file $filename"; |
77
|
|
|
|
78
|
|
|
$tokens = $phpcsFile->getTokens(); |
79
|
|
|
$currentToken = count($tokens) - 1; |
80
|
|
|
$hasClosingFileComment = false; |
81
|
|
|
$isNotAWhitespaceOrAComment = false; |
82
|
|
View Code Duplication |
while ($currentToken >= 0 |
|
|
|
|
83
|
|
|
&& ! $isNotAWhitespaceOrAComment |
84
|
|
|
&& ! $hasClosingFileComment |
85
|
|
|
) { |
86
|
|
|
$token = $tokens[$currentToken]; |
87
|
|
|
$tokenCode = $token['code']; |
88
|
|
|
if (T_COMMENT === $tokenCode) { |
89
|
|
|
$commentString = self::_getCommentContent($token['content']); |
90
|
|
|
if (0 === strcmp($commentString, $commentTemplate)) { |
91
|
|
|
$hasClosingFileComment = true; |
92
|
|
|
} |
93
|
|
|
} else if (T_WHITESPACE === $tokenCode) { |
94
|
|
|
// Whitespaces are allowed between the closing file comment, |
95
|
|
|
// other comments and end of file |
96
|
|
|
} else { |
97
|
|
|
$isNotAWhitespaceOrAComment = true; |
98
|
|
|
} |
99
|
|
|
$currentToken--; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ( ! $hasClosingFileComment) { |
103
|
|
|
$error = 'No comment block marks the end of file instead of the closing PHP tag. Please add a comment block containing only "' . $commentTemplate . '".'; |
104
|
|
|
$phpcsFile->addError($error, $currentToken); |
105
|
|
|
} |
106
|
|
|
}//end process() |
107
|
|
|
}//end class |
108
|
|
|
|
109
|
|
|
?> |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.