Skip to content

Commit 788978d

Browse files
committed
Add missing ControlStructures sniff.
1 parent 9d1f35b commit 788978d

35 files changed

+528
-543
lines changed

PSR2R/Sniffs/Classes/BraceOnSameLineSniff.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,19 @@ public function process(File $phpcsFile, $stackPtr) {
5858
return;
5959
}
6060

61-
if ($lastContent === $curlyBrace - 1) {
62-
$error = 'Opening brace of a %s must have one whitespace after closing parentheses';
63-
$fix = $phpcsFile->addFixableError($error, $curlyBrace, 'OpenBraceWhitespace', $errorData);
64-
if ($fix === true) {
65-
$phpcsFile->fixer->beginChangeset();
66-
$phpcsFile->fixer->addContent($lastContent, ' ');
67-
$phpcsFile->fixer->endChangeset();
68-
}
61+
if ($lastContent !== $curlyBrace - 1) {
62+
return;
6963
}
64+
65+
$error = 'Opening brace of a %s must have one whitespace after closing parentheses';
66+
$fix = $phpcsFile->addFixableError($error, $curlyBrace, 'OpenBraceWhitespace', $errorData);
67+
if ($fix !== true) {
68+
return;
69+
}
70+
71+
$phpcsFile->fixer->beginChangeset();
72+
$phpcsFile->fixer->addContent($lastContent, ' ');
73+
$phpcsFile->fixer->endChangeset();
7074
}
7175

7276
}

PSR2R/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,36 @@ protected function processMemberVar(File $phpcsFile, $stackPtr) {
6363
$phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
6464
}
6565

66-
if ($propertyInfo['scope_specified'] === true && $propertyInfo['is_static'] === true) {
67-
$scopePtr = $phpcsFile->findPrevious(Tokens::$scopeModifiers, ($stackPtr - 1));
68-
$staticPtr = $phpcsFile->findPrevious(T_STATIC, ($stackPtr - 1));
69-
if ($scopePtr < $staticPtr) {
70-
return;
71-
}
72-
73-
$error = 'The static declaration must come after the visibility declaration';
74-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'StaticBeforeVisibility');
75-
if ($fix === true) {
76-
$phpcsFile->fixer->beginChangeset();
66+
if ($propertyInfo['scope_specified'] !== true || $propertyInfo['is_static'] !== true) {
67+
return;
68+
}
7769

78-
for ($i = ($scopePtr + 1); $scopePtr < $stackPtr; $i++) {
79-
if ($tokens[$i]['code'] !== T_WHITESPACE) {
80-
break;
81-
}
70+
$scopePtr = $phpcsFile->findPrevious(Tokens::$scopeModifiers, ($stackPtr - 1));
71+
$staticPtr = $phpcsFile->findPrevious(T_STATIC, ($stackPtr - 1));
72+
if ($scopePtr < $staticPtr) {
73+
return;
74+
}
8275

83-
$phpcsFile->fixer->replaceToken($i, '');
84-
}
76+
$error = 'The static declaration must come after the visibility declaration';
77+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'StaticBeforeVisibility');
78+
if ($fix !== true) {
79+
return;
80+
}
8581

86-
$phpcsFile->fixer->replaceToken($scopePtr, '');
87-
$phpcsFile->fixer->addContentBefore($staticPtr, $propertyInfo['scope'] . ' ');
82+
$phpcsFile->fixer->beginChangeset();
8883

89-
$phpcsFile->fixer->endChangeset();
84+
for ($i = ($scopePtr + 1); $scopePtr < $stackPtr; $i++) {
85+
if ($tokens[$i]['code'] !== T_WHITESPACE) {
86+
break;
9087
}
88+
89+
$phpcsFile->fixer->replaceToken($i, '');
9190
}
91+
92+
$phpcsFile->fixer->replaceToken($scopePtr, '');
93+
$phpcsFile->fixer->addContentBefore($staticPtr, $propertyInfo['scope'] . ' ');
94+
95+
$phpcsFile->fixer->endChangeset();
9296
}
9397

9498
/**

PSR2R/Sniffs/Commenting/DocBlockEndingSniff.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public function process(File $phpcsFile, $stackPtr) {
5959
$currentComment = $stackPtr;
6060
$lastComment = $stackPtr;
6161
while (($currentComment = $phpcsFile->findNext(T_DOC_COMMENT, $currentComment + 1)) !== false) {
62-
if ($tokens[$lastComment]['line'] === ($tokens[$currentComment]['line'] - 1)) {
63-
$comments[] = $currentComment;
64-
$lastComment = $currentComment;
65-
} else {
62+
if ($tokens[$lastComment]['line'] !== ($tokens[$currentComment]['line'] - 1)) {
6663
break;
6764
}
65+
66+
$comments[] = $currentComment;
67+
$lastComment = $currentComment;
6868
}
6969

7070
// The $comments array now contains pointers to each token in the comment block.
@@ -87,11 +87,13 @@ public function process(File $phpcsFile, $stackPtr) {
8787
$error = 'Expected 1 asterisk on closing line; %s found';
8888
$data = [$count];
8989
$fix = $phpcsFile->addFixableError($error, $commentPointer, 'SpaceBeforeTag', $data);
90-
if ($fix === true && $phpcsFile->fixer->enabled === true) {
91-
$pos = strpos($content, '*');
92-
$content = substr($content, 0, $pos + 1) . substr($content, $pos + $count);
93-
$phpcsFile->fixer->replaceToken($commentPointer, $content);
90+
if ($fix !== true || $phpcsFile->fixer->enabled !== true) {
91+
continue;
9492
}
93+
94+
$pos = strpos($content, '*');
95+
$content = substr($content, 0, $pos + 1) . substr($content, $pos + $count);
96+
$phpcsFile->fixer->replaceToken($commentPointer, $content);
9597
}
9698
}
9799

PSR2R/Sniffs/Commenting/DocBlockNoEmptySniff.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ protected function assertNonEmptyDocBlock(File $phpcsFile, int $stackPtr, int $e
4949
}
5050

5151
$fix = $phpcsFile->addFixableError('There should be no empty docblocks.', $stackPtr, 'Superfluous');
52-
if ($fix) {
53-
for ($i = $stackPtr; $i <= $endIndex; $i++) {
54-
$phpcsFile->fixer->replaceToken($i, '');
55-
}
52+
if (!$fix) {
53+
return;
54+
}
55+
56+
for ($i = $stackPtr; $i <= $endIndex; $i++) {
57+
$phpcsFile->fixer->replaceToken($i, '');
5658
}
5759
}
5860

PSR2R/Sniffs/Commenting/DocBlockReturnSelfSniff.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ protected function fixParts(File $phpCsFile, int $classNameIndex, array $parts,
136136
}
137137

138138
$fix = $phpCsFile->addFixableError(implode(', ', $message), $classNameIndex, 'ReturnSelf');
139-
if ($fix) {
140-
$newContent = implode('|', $parts);
141-
$phpCsFile->fixer->replaceToken($classNameIndex, $newContent . $appendix);
139+
if (!$fix) {
140+
return;
142141
}
142+
143+
$newContent = implode('|', $parts);
144+
$phpCsFile->fixer->replaceToken($classNameIndex, $newContent . $appendix);
143145
}
144146

145147
}

PSR2R/Sniffs/Commenting/DocBlockShortTypeSniff.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ protected function fixParts(File $phpCsFile, int $classNameIndex, array $parts,
108108
}
109109

110110
$fix = $phpCsFile->addFixableError(implode(', ', $message), $classNameIndex, 'ShortType');
111-
if ($fix) {
112-
$newContent = implode('|', $parts);
113-
$phpCsFile->fixer->replaceToken($classNameIndex, $newContent . $appendix);
111+
if (!$fix) {
112+
return;
114113
}
114+
115+
$newContent = implode('|', $parts);
116+
$phpCsFile->fixer->replaceToken($classNameIndex, $newContent . $appendix);
115117
}
116118

117119
}

PSR2R/Sniffs/Commenting/DocBlockTagTypesSniff.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,21 @@ public function process(File $phpcsFile, $stackPtr) {
179179
}
180180

181181
$phpcsFile->addFixableError($error, $i, 'Invalid');
182-
if ($phpcsFile->fixer->enabled) {
183-
if ($mappingTag) {
184-
$phpcsFile->fixer->replaceToken($i, $mappingTag);
182+
if (!$phpcsFile->fixer->enabled) {
183+
continue;
184+
}
185185

186-
continue;
187-
}
186+
if ($mappingTag) {
187+
$phpcsFile->fixer->replaceToken($i, $mappingTag);
188188

189-
$phpcsFile->fixer->beginChangeset();
190-
for ($j = $prevAsterix; $j < $nextAsterix; $j++) {
191-
$phpcsFile->fixer->replaceToken($j, '');
192-
}
193-
$phpcsFile->fixer->endChangeset();
189+
continue;
190+
}
191+
192+
$phpcsFile->fixer->beginChangeset();
193+
for ($j = $prevAsterix; $j < $nextAsterix; $j++) {
194+
$phpcsFile->fixer->replaceToken($j, '');
194195
}
196+
$phpcsFile->fixer->endChangeset();
195197
}
196198
}
197199

@@ -202,9 +204,11 @@ protected function prepareWhitelist(): void {
202204
if (!empty($this->whitelist)) {
203205
$whitelist = explode(',', $this->whitelist);
204206
foreach ($whitelist as $tag) {
205-
if (!in_array($tag, static::$whitelistedTags, true)) {
206-
static::$whitelistedTags[] = $tag;
207+
if (in_array($tag, static::$whitelistedTags, true)) {
208+
continue;
207209
}
210+
211+
static::$whitelistedTags[] = $tag;
208212
}
209213
}
210214
$this->whitelist = '';

PSR2R/Sniffs/Commenting/DocBlockVarWithoutNameSniff.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,20 @@ public function process(File $phpcsFile, $stackPtr) {
6464
$content = $tokens[$nextIndex]['content'];
6565
preg_match_all('/ \$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $content, $matches);
6666

67-
if (isset($matches[0][0]) && trim($matches[0][0]) !== '$this') {
68-
$fix = $phpcsFile->addFixableError(
69-
'@var annotations should not contain the variable name.',
70-
$i,
71-
'RemoveVarName',
72-
);
73-
if ($fix) {
74-
$phpcsFile->fixer->replaceToken($nextIndex, str_replace($matches[0][0], '', $content));
75-
}
67+
if (!isset($matches[0][0]) || trim($matches[0][0]) === '$this') {
68+
continue;
7669
}
70+
71+
$fix = $phpcsFile->addFixableError(
72+
'@var annotations should not contain the variable name.',
73+
$i,
74+
'RemoveVarName',
75+
);
76+
if (!$fix) {
77+
continue;
78+
}
79+
80+
$phpcsFile->fixer->replaceToken($nextIndex, str_replace($matches[0][0], '', $content));
7781
}
7882
}
7983

PSR2R/Sniffs/Commenting/DocCommentSniff.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,16 @@ public function process(File $phpcsFile, $stackPtr) {
150150
$shortContent = $tokens[$short]['content'];
151151
$shortEnd = $short;
152152
for ($i = ($short + 1); $i < $commentEnd; $i++) {
153-
if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
154-
if ($tokens[$i]['line'] === ($tokens[$shortEnd]['line'] + 1)) {
155-
$shortContent .= $tokens[$i]['content'];
156-
$shortEnd = $i;
157-
} else {
158-
break;
159-
}
153+
if ($tokens[$i]['code'] !== T_DOC_COMMENT_STRING) {
154+
continue;
155+
}
156+
157+
if ($tokens[$i]['line'] !== ($tokens[$shortEnd]['line'] + 1)) {
158+
break;
160159
}
160+
161+
$shortContent .= $tokens[$i]['content'];
162+
$shortEnd = $i;
161163
}
162164

163165
if (empty($tokens[$commentStart]['comment_tags']) === true) {
@@ -171,24 +173,28 @@ public function process(File $phpcsFile, $stackPtr) {
171173
}
172174

173175
$prev = $phpcsFile->findPrevious($empty, $firstTag - 1, $stackPtr, true);
174-
if ($tokens[$firstTag]['line'] !== ($tokens[$prev]['line'] + 2)) {
175-
$error = 'There must be exactly one blank line before the tags in a doc comment';
176-
$fix = $phpcsFile->addFixableError($error, $firstTag, 'SpacingBeforeTags');
177-
if ($fix === true) {
178-
$phpcsFile->fixer->beginChangeset();
179-
for ($i = ($prev + 1); $i < $firstTag; $i++) {
180-
if ($tokens[$i]['line'] === $tokens[$firstTag]['line']) {
181-
break;
182-
}
176+
if ($tokens[$firstTag]['line'] === ($tokens[$prev]['line'] + 2)) {
177+
return;
178+
}
183179

184-
$phpcsFile->fixer->replaceToken($i, '');
185-
}
180+
$error = 'There must be exactly one blank line before the tags in a doc comment';
181+
$fix = $phpcsFile->addFixableError($error, $firstTag, 'SpacingBeforeTags');
182+
if ($fix !== true) {
183+
return;
184+
}
186185

187-
$indent = str_repeat("\t", $tokens[$stackPtr]['column'] - 1) . ' ';
188-
$phpcsFile->fixer->addContent($prev, $phpcsFile->eolChar . $indent . '*' . $phpcsFile->eolChar);
189-
$phpcsFile->fixer->endChangeset();
186+
$phpcsFile->fixer->beginChangeset();
187+
for ($i = ($prev + 1); $i < $firstTag; $i++) {
188+
if ($tokens[$i]['line'] === $tokens[$firstTag]['line']) {
189+
break;
190190
}
191+
192+
$phpcsFile->fixer->replaceToken($i, '');
191193
}
194+
195+
$indent = str_repeat("\t", $tokens[$stackPtr]['column'] - 1) . ' ';
196+
$phpcsFile->fixer->addContent($prev, $phpcsFile->eolChar . $indent . '*' . $phpcsFile->eolChar);
197+
$phpcsFile->fixer->endChangeset();
192198
}
193199

194200
}

PSR2R/Sniffs/Commenting/NoControlStructureEndCommentSniff.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ public function process(File $phpcsFile, $stackPtr) {
3535

3636
$error = 'The unnecessary end comment must be removed';
3737
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Unnecessary');
38-
if ($fix === true) {
39-
/** @noinspection NotOptimalRegularExpressionsInspection */
40-
$phpcsFile->fixer->replaceToken($stackPtr, preg_replace('/[^\s]/', '', $content));
38+
if ($fix !== true) {
39+
return;
4140
}
41+
42+
/** @noinspection NotOptimalRegularExpressionsInspection */
43+
$phpcsFile->fixer->replaceToken($stackPtr, preg_replace('/[^\s]/', '', $content));
4244
}
4345

4446
}

0 commit comments

Comments
 (0)