-
Notifications
You must be signed in to change notification settings - Fork 1
/
MemberVarSpacingSniff.php
176 lines (135 loc) · 6.48 KB
/
MemberVarSpacingSniff.php
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
<?php
declare(strict_types = 1);
namespace InfinityloopCodingStandard\Sniffs\WhiteSpace;
class MemberVarSpacingSniff extends \PHP_CodeSniffer\Sniffs\AbstractVariableSniff
{
public int $spacing = 1;
public int $spacingBeforeFirst = 1;
public bool $ignoreFirstMemberVar = false;
//@phpcs:ignore Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
protected function processMemberVar(\PHP_CodeSniffer\Files\File $phpcsFile, $stackPtr) : ?int
{
$tokens = $phpcsFile->getTokens();
$validPrefixes = \PHP_CodeSniffer\Util\Tokens::$methodPrefixes;
$validPrefixes[] = \T_VAR;
$startOfStatement = $phpcsFile->findPrevious($validPrefixes, $stackPtr - 1, null, false, null, true);
if ($startOfStatement === false) {
return null;
}
$endOfStatement = $phpcsFile->findNext(\T_SEMICOLON, $stackPtr + 1, null, false, null, true);
$ignore = $validPrefixes;
$ignore[] = \T_WHITESPACE;
$start = $startOfStatement;
$prev = $phpcsFile->findPrevious($ignore, $startOfStatement - 1, null, true);
if (isset(\PHP_CodeSniffer\Util\Tokens::$commentTokens[$tokens[$prev]['code']]) === true) {
// Assume the comment belongs to the member var if it is on a line by itself.
$prevContent = $phpcsFile->findPrevious(\PHP_CodeSniffer\Util\Tokens::$emptyTokens, $prev - 1, null, true);
if ($tokens[$prevContent]['line'] !== $tokens[$prev]['line']) {
// Check the spacing, but then skip it.
$foundLines = $tokens[$startOfStatement]['line'] - $tokens[$prev]['line'] - 1;
if ($foundLines > 0) {
$error = 'Expected 0 blank lines after member var comment; %s found';
$data = [$foundLines];
$fix = $phpcsFile->addFixableError($error, $prev, 'AfterComment', $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
// Inline comments have the newline included in the content but
// docblock do not.
if ($tokens[$prev]['code'] === \T_COMMENT) {
$phpcsFile->fixer->replaceToken($prev, \rtrim($tokens[$prev]['content']));
}
for ($i = $prev + 1; $i <= $startOfStatement; $i++) {
if ($tokens[$i]['line'] === $tokens[$startOfStatement]['line']) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->addNewline($prev);
$phpcsFile->fixer->endChangeset();
}
}//end if
$start = $prev;
}//end if
}//end if
// There needs to be n blank lines before the var, not counting comments.
if ($start === $startOfStatement) {
// No comment found.
$first = $phpcsFile->findFirstOnLine(\PHP_CodeSniffer\Util\Tokens::$emptyTokens, $start, true);
if ($first === false) {
$first = $start;
}
} elseif ($tokens[$start]['code'] === \T_DOC_COMMENT_CLOSE_TAG) {
$first = $tokens[$start]['comment_opener'];
} else {
$first = $phpcsFile->findPrevious(\PHP_CodeSniffer\Util\Tokens::$emptyTokens, $start - 1, null, true);
$first = $phpcsFile->findNext(\PHP_CodeSniffer\Util\Tokens::$commentTokens, $first + 1);
}
// Determine if this is the first member var.
$prev = $phpcsFile->findPrevious(\T_WHITESPACE, $first - 1, null, true);
if ($tokens[$prev]['code'] === \T_CLOSE_CURLY_BRACKET
&& isset($tokens[$prev]['scope_condition']) === true
&& $tokens[$tokens[$prev]['scope_condition']]['code'] === \T_FUNCTION
) {
return null;
}
$prevVar = $phpcsFile->findPrevious(\T_VARIABLE, $first - 1);
if ($this->ignoreFirstMemberVar && $tokens[$prevVar]['code'] !== \T_VARIABLE) {
return null;
}
if ($tokens[$prev]['code'] === \T_OPEN_CURLY_BRACKET
&& isset(\PHP_CodeSniffer\Util\Tokens::$ooScopeTokens[$tokens[$tokens[$prev]['scope_condition']]['code']]) === true
) {
$errorMsg = 'Expected %s blank line(s) before first member var; %s found';
$errorCode = 'FirstIncorrect';
$spacing = (int) $this->spacingBeforeFirst;
} else {
$errorMsg = 'Expected %s blank line(s) before member var; %s found';
$errorCode = 'Incorrect';
$spacing = (int) $this->spacing;
}
$foundLines = $tokens[$first]['line'] - $tokens[$prev]['line'] - 1;
if ($errorCode === 'FirstIncorrect') {
$phpcsFile->recordMetric($stackPtr, 'Member var spacing before first', $foundLines);
} else {
$phpcsFile->recordMetric($stackPtr, 'Member var spacing before', $foundLines);
}
if ($foundLines === $spacing) {
if ($endOfStatement !== false) {
return $endOfStatement;
}
return null;
}
$data = [
$spacing,
$foundLines,
];
$fix = $phpcsFile->addFixableError($errorMsg, $startOfStatement, $errorCode, $data);
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = $prev + 1; $i < $first; $i++) {
if ($tokens[$i]['line'] === $tokens[$prev]['line']) {
continue;
}
if ($tokens[$i]['line'] === $tokens[$first]['line']) {
for ($x = 1; $x <= $spacing; $x++) {
$phpcsFile->fixer->addNewlineBefore($i);
}
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}//end if
if ($endOfStatement !== false) {
return $endOfStatement;
}
}
//@phpcs:ignore Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
protected function processVariable(\PHP_CodeSniffer\Files\File $phpcsFile, $stackPtr) : void
{
}
//@phpcs:ignore Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
protected function processVariableInString(\PHP_CodeSniffer\Files\File $phpcsFile, $stackPtr) : void
{
}
}