forked from pfrenssen/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassDeclarationSniff.php
More file actions
190 lines (162 loc) · 6.73 KB
/
ClassDeclarationSniff.php
File metadata and controls
190 lines (162 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* Class Declaration Test.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\Classes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\ClassDeclarationSniff as PSR2ClassDeclarationSniff;
use PHP_CodeSniffer\Util\Tokens;
/**
* Class Declaration Test.
*
* Checks the declaration of the class is correct.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ClassDeclarationSniff extends PSR2ClassDeclarationSniff
{
/**
* {@inheritdoc}
*
* @var integer
*/
public $indent = 2;
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register()
{
return [
T_CLASS,
T_INTERFACE,
T_TRAIT,
T_ENUM,
];
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param integer $stackPtr The position of the current token in the
* stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$errorData = [strtolower($tokens[$stackPtr]['content'])];
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
$error = 'Possible parse error: %s missing opening or closing brace';
$phpcsFile->addWarning($error, $stackPtr, 'MissingBrace', $errorData);
return;
}
$openingBrace = $tokens[$stackPtr]['scope_opener'];
$next = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true);
if ($tokens[$next]['line'] === $tokens[$openingBrace]['line'] && $tokens[$next]['code'] !== T_CLOSE_CURLY_BRACKET) {
$error = 'Opening brace must be the last content on the line';
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace');
if ($fix === true) {
$phpcsFile->fixer->addNewline($openingBrace);
}
}
$previous = $phpcsFile->findPrevious(T_WHITESPACE, ($openingBrace - 1), null, true);
$declarationLine = $tokens[$previous]['line'];
$braceLine = $tokens[$openingBrace]['line'];
$lineDifference = ($braceLine - $declarationLine);
if ($lineDifference > 0) {
$error = 'Opening brace should be on the same line as the declaration';
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnNewLine');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($previous + 1); $i < $openingBrace; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->addContent($previous, ' ');
$phpcsFile->fixer->endChangeset();
}
return;
}
$openingBrace = $tokens[$stackPtr]['scope_opener'];
if ($tokens[($openingBrace - 1)]['code'] !== T_WHITESPACE) {
$length = 0;
} elseif ($tokens[($openingBrace - 1)]['content'] === "\t") {
$length = '\t';
} else {
$length = strlen($tokens[($openingBrace - 1)]['content']);
}
if ($length !== 1) {
$error = 'Expected 1 space before opening brace; found %s';
$data = [$length];
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'SpaceBeforeBrace', $data);
if ($fix === true) {
if ($length === 0) {
$phpcsFile->fixer->replaceToken(($openingBrace), ' {');
} else {
$phpcsFile->fixer->replaceToken(($openingBrace - 1), ' ');
}
}
}
// Now call the open spacing method from PSR2.
$this->processOpen($phpcsFile, $stackPtr);
$this->processClose($phpcsFile, $stackPtr);
}
/**
* Processes the closing section of a class declaration.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return void
*/
public function processClose(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Just in case.
if (isset($tokens[$stackPtr]['scope_closer']) === false) {
return;
}
// Check that the closing brace comes right after the code body.
$closeBrace = $tokens[$stackPtr]['scope_closer'];
$prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBrace - 1), null, true);
if ($prevContent !== $tokens[$stackPtr]['scope_opener']
&& $tokens[$prevContent]['line'] !== ($tokens[$closeBrace]['line'] - 2)
// If the class only contains a comment no extra line is needed.
&& isset(Tokens::COMMENT_TOKENS[$tokens[$prevContent]['code']]) === false
// Enums are allowed to enclose the cases without an extra line.
&& $tokens[$stackPtr]['code'] !== T_ENUM
) {
$error = 'The closing brace for the %s must have an empty line before it';
$data = [$tokens[$stackPtr]['content']];
$fix = $phpcsFile->addFixableError($error, $closeBrace, 'CloseBraceAfterBody', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($prevContent + 1); $i < $closeBrace; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($closeBrace, $phpcsFile->eolChar . $phpcsFile->eolChar . $tokens[$closeBrace]['content']);
$phpcsFile->fixer->endChangeset();
}
}//end if
// Check the closing brace is on it's own line, but allow
// for comments like "//end class".
$nextContent = $phpcsFile->findNext(T_COMMENT, ($closeBrace + 1), null, true);
if ($tokens[$nextContent]['content'] !== $phpcsFile->eolChar
&& $tokens[$nextContent]['line'] === $tokens[$closeBrace]['line']
) {
$type = strtolower($tokens[$stackPtr]['content']);
$error = 'Closing %s brace must be on a line by itself';
$data = [$tokens[$stackPtr]['content']];
$phpcsFile->addError($error, $closeBrace, 'CloseBraceSameLine', $data);
}
}
}