comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Files/EndFileNewlineSniff.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 * Drupal_Sniffs_Files_EndFileNewlineSniff.
4 *
5 * @category PHP
6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */
9
10 /**
11 * Ensures the file ends with a newline character.
12 *
13 * Largely copied from PSR2, but we need to run it on *.txt files and templates as
14 * well.
15 *
16 * @category PHP
17 * @package PHP_CodeSniffer
18 * @link http://pear.php.net/package/PHP_CodeSniffer
19 */
20 class Drupal_Sniffs_Files_EndFileNewlineSniff implements PHP_CodeSniffer_Sniff
21 {
22
23
24 /**
25 * A list of tokenizers this sniff supports.
26 *
27 * @var array
28 */
29 public $supportedTokenizers = array(
30 'PHP',
31 'JS',
32 'CSS',
33 );
34
35
36 /**
37 * Returns an array of tokens this test wants to listen for.
38 *
39 * @return array
40 */
41 public function register()
42 {
43 return array(
44 T_OPEN_TAG,
45 T_INLINE_HTML,
46 );
47
48 }//end register()
49
50
51 /**
52 * Processes this sniff, when one of its tokens is encountered.
53 *
54 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
55 * @param int $stackPtr The position of the current token in
56 * the stack passed in $tokens.
57 *
58 * @return void
59 */
60 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
61 {
62 // Skip to the end of the file.
63 $tokens = $phpcsFile->getTokens();
64 if ($phpcsFile->tokenizerType === 'PHP') {
65 $lastToken = ($phpcsFile->numTokens - 1);
66 } else {
67 // JS and CSS have an artificial token at the end which we have to
68 // ignore.
69 $lastToken = ($phpcsFile->numTokens - 2);
70 }
71
72 // Hard-coding the expected \n in this sniff as it is PSR-2 specific and
73 // PSR-2 enforces the use of unix style newlines.
74 if (substr($tokens[$lastToken]['content'], -1) !== "\n") {
75 $error = 'Expected 1 newline at end of file; 0 found';
76 $fix = $phpcsFile->addFixableError($error, $lastToken, 'NoneFound');
77 if ($fix === true) {
78 $phpcsFile->fixer->addNewline($lastToken);
79 }
80
81 $phpcsFile->recordMetric($stackPtr, 'Number of newlines at EOF', '0');
82 return ($phpcsFile->numTokens + 1);
83 }
84
85 // Go looking for the last non-empty line.
86 $lastLine = $tokens[$lastToken]['line'];
87 if ($tokens[$lastToken]['code'] === T_WHITESPACE) {
88 $lastCode = $phpcsFile->findPrevious(T_WHITESPACE, ($lastToken - 1), null, true);
89 } else if ($tokens[$lastToken]['code'] === T_INLINE_HTML) {
90 $lastCode = $lastToken;
91 while ($lastCode > 0 && trim($tokens[$lastCode]['content']) === '') {
92 $lastCode--;
93 }
94 } else {
95 $lastCode = $lastToken;
96 }
97
98 $lastCodeLine = $tokens[$lastCode]['line'];
99 $blankLines = ($lastLine - $lastCodeLine + 1);
100 $phpcsFile->recordMetric($stackPtr, 'Number of newlines at EOF', $blankLines);
101
102 if ($blankLines > 1) {
103 $error = 'Expected 1 newline at end of file; %s found';
104 $data = array($blankLines);
105 $fix = $phpcsFile->addFixableError($error, $lastCode, 'TooMany', $data);
106
107 if ($fix === true) {
108 $phpcsFile->fixer->beginChangeset();
109 $phpcsFile->fixer->replaceToken($lastCode, rtrim($tokens[$lastCode]['content']));
110 for ($i = ($lastCode + 1); $i < $lastToken; $i++) {
111 $phpcsFile->fixer->replaceToken($i, '');
112 }
113
114 $phpcsFile->fixer->replaceToken($lastToken, $phpcsFile->eolChar);
115 $phpcsFile->fixer->endChangeset();
116 }
117 }
118
119 // Skip the rest of the file.
120 return ($phpcsFile->numTokens + 1);
121
122 }//end process()
123
124
125 }//end class