Skip to content

Commit 7fd35ea

Browse files
committed
DocCommentSpacingSniff: Cleanup
1 parent ae2e465 commit 7fd35ea

File tree

1 file changed

+50
-51
lines changed

1 file changed

+50
-51
lines changed

SlevomatCodingStandard/Sniffs/Commenting/DocCommentSpacingSniff.php

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ public function register(): array
8181
/**
8282
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
8383
* @param \PHP_CodeSniffer\Files\File $phpcsFile
84-
* @param int $docCommentOpenPointer
84+
* @param int $docCommentOpenerPointer
8585
*/
86-
public function process(File $phpcsFile, $docCommentOpenPointer): void
86+
public function process(File $phpcsFile, $docCommentOpenerPointer): void
8787
{
88-
if (DocCommentHelper::isInline($phpcsFile, $docCommentOpenPointer)) {
88+
if (DocCommentHelper::isInline($phpcsFile, $docCommentOpenerPointer)) {
8989
return;
9090
}
9191

@@ -94,8 +94,8 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
9494
$firstContentStartPointer = TokenHelper::findNextExcluding(
9595
$phpcsFile,
9696
[T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR],
97-
$docCommentOpenPointer + 1,
98-
$tokens[$docCommentOpenPointer]['comment_closer']
97+
$docCommentOpenerPointer + 1,
98+
$tokens[$docCommentOpenerPointer]['comment_closer']
9999
);
100100

101101
if ($firstContentStartPointer === null) {
@@ -110,7 +110,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
110110
$phpcsFile,
111111
[T_DOC_COMMENT_STAR, T_DOC_COMMENT_WHITESPACE],
112112
$actualPointer + 1,
113-
$tokens[$docCommentOpenPointer]['comment_closer'] + 1
113+
$tokens[$docCommentOpenerPointer]['comment_closer'] + 1
114114
);
115115

116116
if ($tokens[$actualPointer]['code'] !== T_DOC_COMMENT_STRING) {
@@ -120,7 +120,7 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
120120
$firstContentEndPointer = $actualPointer;
121121
} while (true);
122122

123-
$annotations = array_merge([], ...array_values(AnnotationHelper::getAnnotations($phpcsFile, $docCommentOpenPointer)));
123+
$annotations = array_merge([], ...array_values(AnnotationHelper::getAnnotations($phpcsFile, $docCommentOpenerPointer)));
124124
uasort($annotations, function (Annotation $a, Annotation $b): int {
125125
return $a->getStartPointer() <=> $b->getEndPointer();
126126
});
@@ -131,30 +131,30 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
131131

132132
$lastContentEndPointer = $annotationsCount > 0 ? $annotations[$annotationsCount - 1]->getEndPointer() : $firstContentEndPointer;
133133

134-
$this->checkLinesBeforeFirstContent($phpcsFile, $docCommentOpenPointer, $firstContentStartPointer);
135-
$this->checkLinesBetweenDescriptionAndFirstAnnotation($phpcsFile, $docCommentOpenPointer, $firstContentStartPointer, $firstContentEndPointer, $firstAnnotation);
134+
$this->checkLinesBeforeFirstContent($phpcsFile, $docCommentOpenerPointer, $firstContentStartPointer);
135+
$this->checkLinesBetweenDescriptionAndFirstAnnotation($phpcsFile, $docCommentOpenerPointer, $firstContentStartPointer, $firstContentEndPointer, $firstAnnotation);
136136

137137
if (count($annotations) > 1) {
138138
if (count($this->getAnnotationsGroups()) === 0) {
139-
$this->checkLinesBetweenDifferentAnnotationsTypes($phpcsFile, $docCommentOpenPointer, $annotations);
139+
$this->checkLinesBetweenDifferentAnnotationsTypes($phpcsFile, $docCommentOpenerPointer, $annotations);
140140
} else {
141-
$this->checkAnnotationsGroups($phpcsFile, $docCommentOpenPointer, $annotations);
141+
$this->checkAnnotationsGroups($phpcsFile, $docCommentOpenerPointer, $annotations);
142142
}
143143
}
144144

145-
$this->checkLinesAfterLastContent($phpcsFile, $docCommentOpenPointer, $tokens[$docCommentOpenPointer]['comment_closer'], $lastContentEndPointer);
145+
$this->checkLinesAfterLastContent($phpcsFile, $docCommentOpenerPointer, $tokens[$docCommentOpenerPointer]['comment_closer'], $lastContentEndPointer);
146146
}
147147

148148
private function checkLinesBeforeFirstContent(
149149
File $phpcsFile,
150-
int $docCommentOpenPointer,
150+
int $docCommentOpenerPointer,
151151
int $firstContentStartPointer
152152
): void
153153
{
154154
$tokens = $phpcsFile->getTokens();
155155

156-
$whitespaceBeforeFirstContent = substr($tokens[$docCommentOpenPointer]['content'], 0, strlen('/**'));
157-
$whitespaceBeforeFirstContent .= TokenHelper::getContent($phpcsFile, $docCommentOpenPointer + 1, $firstContentStartPointer - 1);
156+
$whitespaceBeforeFirstContent = substr($tokens[$docCommentOpenerPointer]['content'], 0, strlen('/**'));
157+
$whitespaceBeforeFirstContent .= TokenHelper::getContent($phpcsFile, $docCommentOpenerPointer + 1, $firstContentStartPointer - 1);
158158

159159
$requiredLinesCountBeforeFirstContent = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeFirstContent);
160160
$linesCountBeforeFirstContent = max(substr_count($whitespaceBeforeFirstContent, $phpcsFile->eolChar) - 1, 0);
@@ -172,27 +172,27 @@ private function checkLinesBeforeFirstContent(
172172
return;
173173
}
174174

175-
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenPointer);
175+
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenerPointer);
176176

177177
$phpcsFile->fixer->beginChangeset();
178178

179-
$phpcsFile->fixer->replaceToken($docCommentOpenPointer, '/**' . $phpcsFile->eolChar);
180-
for ($i = $docCommentOpenPointer + 1; $i < $firstContentStartPointer; $i++) {
179+
$phpcsFile->fixer->replaceToken($docCommentOpenerPointer, '/**' . $phpcsFile->eolChar);
180+
for ($i = $docCommentOpenerPointer + 1; $i < $firstContentStartPointer; $i++) {
181181
$phpcsFile->fixer->replaceToken($i, '');
182182
}
183183

184184
for ($i = 1; $i <= $requiredLinesCountBeforeFirstContent; $i++) {
185-
$phpcsFile->fixer->addContent($docCommentOpenPointer, sprintf('%s *%s', $indentation, $phpcsFile->eolChar));
185+
$phpcsFile->fixer->addContent($docCommentOpenerPointer, sprintf('%s *%s', $indentation, $phpcsFile->eolChar));
186186
}
187187

188-
$phpcsFile->fixer->addContentBefore($firstContentStartPointer, $this->getIndentation($phpcsFile, $firstContentStartPointer));
188+
$phpcsFile->fixer->addContentBefore($firstContentStartPointer, $indentation . ' * ');
189189

190190
$phpcsFile->fixer->endChangeset();
191191
}
192192

193193
private function checkLinesBetweenDescriptionAndFirstAnnotation(
194194
File $phpcsFile,
195-
int $docCommentOpenPointer,
195+
int $docCommentOpenerPointer,
196196
int $firstContentStartPointer,
197197
int $firstContentEndPointer,
198198
?Annotation $firstAnnotation
@@ -229,7 +229,7 @@ private function checkLinesBetweenDescriptionAndFirstAnnotation(
229229
return;
230230
}
231231

232-
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenPointer);
232+
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenerPointer);
233233

234234
$phpcsFile->fixer->beginChangeset();
235235

@@ -242,27 +242,27 @@ private function checkLinesBetweenDescriptionAndFirstAnnotation(
242242
$phpcsFile->fixer->addContent($firstContentEndPointer, sprintf('%s *%s', $indentation, $phpcsFile->eolChar));
243243
}
244244

245-
$phpcsFile->fixer->addContentBefore($firstAnnotation->getStartPointer(), $this->getIndentation($phpcsFile, $firstAnnotation->getStartPointer()));
245+
$phpcsFile->fixer->addContentBefore($firstAnnotation->getStartPointer(), $indentation . ' * ');
246246

247247
$phpcsFile->fixer->endChangeset();
248248
}
249249

250250
/**
251251
* @param \PHP_CodeSniffer\Files\File $phpcsFile
252-
* @param int $docCommentOpenPointer
252+
* @param int $docCommentOpenerPointer
253253
* @param \SlevomatCodingStandard\Helpers\Annotation[] $annotations
254254
*/
255255
private function checkLinesBetweenDifferentAnnotationsTypes(
256256
File $phpcsFile,
257-
int $docCommentOpenPointer,
257+
int $docCommentOpenerPointer,
258258
array $annotations
259259
): void
260260
{
261261
$requiredLinesCountBetweenDifferentAnnotationsTypes = SniffSettingsHelper::normalizeInteger($this->linesCountBetweenDifferentAnnotationsTypes);
262262

263263
$tokens = $phpcsFile->getTokens();
264264

265-
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenPointer);
265+
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenerPointer);
266266

267267
$previousAnnotation = null;
268268
foreach ($annotations as $annotation) {
@@ -314,20 +314,20 @@ private function checkLinesBetweenDifferentAnnotationsTypes(
314314
$phpcsFile->fixer->addContent($previousAnnotation->getEndPointer(), sprintf('%s *%s', $indentation, $phpcsFile->eolChar));
315315
}
316316

317-
$phpcsFile->fixer->addContentBefore($annotation->getStartPointer(), $this->getIndentation($phpcsFile, $annotation->getStartPointer()));
317+
$phpcsFile->fixer->addContentBefore($annotation->getStartPointer(), $indentation . ' * ');
318318

319319
$phpcsFile->fixer->endChangeset();
320320
}
321321
}
322322

323323
/**
324324
* @param \PHP_CodeSniffer\Files\File $phpcsFile
325-
* @param int $docCommentOpenPointer
325+
* @param int $docCommentOpenerPointer
326326
* @param \SlevomatCodingStandard\Helpers\Annotation[] $annotations
327327
*/
328328
private function checkAnnotationsGroups(
329329
File $phpcsFile,
330-
int $docCommentOpenPointer,
330+
int $docCommentOpenerPointer,
331331
array $annotations
332332
): void
333333
{
@@ -355,18 +355,18 @@ private function checkAnnotationsGroups(
355355
$annotationsGroups[] = $annotationsGroup;
356356
}
357357

358-
$this->checkAnnotationsGroupsOrder($phpcsFile, $docCommentOpenPointer, $annotationsGroups, $annotations);
359-
$this->checkLinesBetweenAnnotationsGroups($phpcsFile, $docCommentOpenPointer, $annotationsGroups);
358+
$this->checkAnnotationsGroupsOrder($phpcsFile, $docCommentOpenerPointer, $annotationsGroups, $annotations);
359+
$this->checkLinesBetweenAnnotationsGroups($phpcsFile, $docCommentOpenerPointer, $annotationsGroups);
360360
}
361361

362362
/**
363363
* @param \PHP_CodeSniffer\Files\File $phpcsFile
364-
* @param int $docCommentOpenPointer
364+
* @param int $docCommentOpenerPointer
365365
* @param \SlevomatCodingStandard\Helpers\Annotation[][] $annotationsGroups
366366
*/
367367
private function checkLinesBetweenAnnotationsGroups(
368368
File $phpcsFile,
369-
int $docCommentOpenPointer,
369+
int $docCommentOpenerPointer,
370370
array $annotationsGroups
371371
): void
372372
{
@@ -406,6 +406,8 @@ private function checkLinesBetweenAnnotationsGroups(
406406
continue;
407407
}
408408

409+
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenerPointer);
410+
409411
$phpcsFile->fixer->beginChangeset();
410412

411413
$phpcsFile->fixer->addNewline($lastAnnotationInPreviousGroup->getEndPointer());
@@ -416,13 +418,13 @@ private function checkLinesBetweenAnnotationsGroups(
416418
for ($i = 1; $i <= $requiredLinesCountBetweenAnnotationsGroups; $i++) {
417419
$phpcsFile->fixer->addContent(
418420
$lastAnnotationInPreviousGroup->getEndPointer(),
419-
sprintf('%s *%s', $this->getIndentation($phpcsFile, $docCommentOpenPointer), $phpcsFile->eolChar)
421+
sprintf('%s *%s', $indentation, $phpcsFile->eolChar)
420422
);
421423
}
422424

423425
$phpcsFile->fixer->addContentBefore(
424426
$firstAnnotationInActualGroup->getStartPointer(),
425-
$this->getIndentation($phpcsFile, $firstAnnotationInActualGroup->getStartPointer())
427+
$indentation . ' * '
426428
);
427429

428430
$phpcsFile->fixer->endChangeset();
@@ -431,13 +433,13 @@ private function checkLinesBetweenAnnotationsGroups(
431433

432434
/**
433435
* @param \PHP_CodeSniffer\Files\File $phpcsFile
434-
* @param int $docCommentOpenPointer
436+
* @param int $docCommentOpenerPointer
435437
* @param \SlevomatCodingStandard\Helpers\Annotation[][] $annotationsGroups
436438
* @param \SlevomatCodingStandard\Helpers\Annotation[] $annotations
437439
*/
438440
private function checkAnnotationsGroupsOrder(
439441
File $phpcsFile,
440-
int $docCommentOpenPointer,
442+
int $docCommentOpenerPointer,
441443
array $annotationsGroups,
442444
array $annotations
443445
): void
@@ -538,7 +540,7 @@ private function checkAnnotationsGroupsOrder(
538540
$lastAnnotationsGroup = $annotationsGroups[count($annotationsGroups) - 1];
539541
$lastAnnotation = $lastAnnotationsGroup[count($lastAnnotationsGroup) - 1];
540542

541-
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenPointer);
543+
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenerPointer);
542544

543545
$fixedAnnotations = '';
544546
$firstGroup = true;
@@ -641,12 +643,12 @@ private function isAnnotationMatched(Annotation $annotation, string $annotationN
641643

642644
private function checkLinesAfterLastContent(
643645
File $phpcsFile,
644-
int $docCommentOpenPointer,
645-
int $docCommentClosePointer,
646+
int $docCommentOpenerPointer,
647+
int $docCommentCloserPointer,
646648
int $lastContentEndPointer
647649
): void
648650
{
649-
$whitespaceAfterLastContent = TokenHelper::getContent($phpcsFile, $lastContentEndPointer + 1, $docCommentClosePointer);
651+
$whitespaceAfterLastContent = TokenHelper::getContent($phpcsFile, $lastContentEndPointer + 1, $docCommentCloserPointer);
650652

651653
$requiredLinesCountAfterLastContent = SniffSettingsHelper::normalizeInteger($this->linesCountAfterLastContent);
652654
$linesCountAfterLastContent = max(substr_count($whitespaceAfterLastContent, $phpcsFile->eolChar) - 1, 0);
@@ -664,32 +666,29 @@ private function checkLinesAfterLastContent(
664666
return;
665667
}
666668

667-
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenPointer);
669+
$indentation = $this->getIndentation($phpcsFile, $docCommentOpenerPointer);
668670

669671
$phpcsFile->fixer->beginChangeset();
670672

671673
$phpcsFile->fixer->addNewline($lastContentEndPointer);
672-
for ($i = $lastContentEndPointer + 1; $i < $docCommentClosePointer; $i++) {
674+
for ($i = $lastContentEndPointer + 1; $i < $docCommentCloserPointer; $i++) {
673675
$phpcsFile->fixer->replaceToken($i, '');
674676
}
675677

676678
for ($i = 1; $i <= $requiredLinesCountAfterLastContent; $i++) {
677679
$phpcsFile->fixer->addContent($lastContentEndPointer, sprintf('%s *%s', $indentation, $phpcsFile->eolChar));
678680
}
679681

680-
$phpcsFile->fixer->addContentBefore($docCommentClosePointer, $this->getIndentation($phpcsFile, $docCommentClosePointer));
682+
$phpcsFile->fixer->addContentBefore($docCommentCloserPointer, $indentation . ' ');
681683

682684
$phpcsFile->fixer->endChangeset();
683685
}
684686

685-
private function getIndentation(File $phpcsFile, int $pointer): string
687+
private function getIndentation(File $phpcsFile, int $docCommentOpenerPointer): string
686688
{
687-
$pointerBeforeDocComment = TokenHelper::findPreviousExcluding($phpcsFile, [T_WHITESPACE, T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR], $pointer - 1);
688-
$whitespaceBeforeDocComment = TokenHelper::getContent($phpcsFile, $pointerBeforeDocComment + 1, $pointer - 1);
689-
690-
preg_match('~([^' . $phpcsFile->eolChar . ']+)$~', $whitespaceBeforeDocComment, $matches);
691-
692-
return $matches[1] ?? '';
689+
/** @var int $endOfLinePointer */
690+
$endOfLinePointer = TokenHelper::findPreviousContent($phpcsFile, T_WHITESPACE, $phpcsFile->eolChar, $docCommentOpenerPointer - 1);
691+
return TokenHelper::getContent($phpcsFile, $endOfLinePointer + 1, $docCommentOpenerPointer - 1);
693692
}
694693

695694
/**

0 commit comments

Comments
 (0)