Skip to content

Commit 710c256

Browse files
committed
Speedup
1 parent 14169a4 commit 710c256

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

SlevomatCodingStandard/Sniffs/Commenting/AnnotationNameSniff.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use function array_map;
1414
use function array_merge;
1515
use function array_unique;
16+
use function implode;
1617
use function ltrim;
1718
use function preg_match_all;
1819
use function sprintf;
@@ -204,41 +205,42 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
204205

205206
$docCommentContent = TokenHelper::getContent($phpcsFile, $docCommentOpenPointer, $tokens[$docCommentOpenPointer]['comment_closer']);
206207

207-
foreach ($correctAnnotationNames as $correctAnnotationName) {
208-
if (preg_match_all('~\{(' . $correctAnnotationName . ')\}~i', $docCommentContent, $matches, PREG_OFFSET_CAPTURE) === 0) {
208+
if (preg_match_all(
209+
'~\{(' . implode('|', $correctAnnotationNames) . ')\}~i',
210+
$docCommentContent,
211+
$matches,
212+
PREG_OFFSET_CAPTURE
213+
) === 0) {
214+
return;
215+
}
216+
217+
foreach ($matches[1] as $match) {
218+
$correctAnnotationName = $correctAnnotationNames[strtolower($match[0])];
219+
220+
if ($correctAnnotationName === $match[0]) {
209221
continue;
210222
}
211223

212-
foreach ($matches[1] as $match) {
213-
if ($match[0] === $correctAnnotationName) {
214-
continue;
215-
}
216-
217-
$fix = $phpcsFile->addFixableError(
218-
sprintf('Annotation name is incorrect. Expected %s, found %s.', $correctAnnotationName, $match[0]),
219-
$docCommentOpenPointer,
220-
self::CODE_ANNOTATION_NAME_INCORRECT
221-
);
222-
if (!$fix) {
223-
continue;
224-
}
224+
$fix = $phpcsFile->addFixableError(
225+
sprintf('Annotation name is incorrect. Expected %s, found %s.', $correctAnnotationName, $match[0]),
226+
$docCommentOpenPointer,
227+
self::CODE_ANNOTATION_NAME_INCORRECT
228+
);
229+
if (!$fix) {
230+
continue;
231+
}
225232

226-
$phpcsFile->fixer->beginChangeset();
233+
$phpcsFile->fixer->beginChangeset();
227234

228-
$fixedDocCommentContent = substr($docCommentContent, 0, $match[1]) . $correctAnnotationName . substr(
229-
$docCommentContent,
230-
$match[1] + strlen($match[0])
231-
);
235+
$fixedDocCommentContent = substr($docCommentContent, 0, $match[1]) . $correctAnnotationName . substr(
236+
$docCommentContent,
237+
$match[1] + strlen($match[0])
238+
);
232239

233-
$phpcsFile->fixer->replaceToken($docCommentOpenPointer, $fixedDocCommentContent);
234-
FixerHelper::removeBetweenIncluding(
235-
$phpcsFile,
236-
$docCommentOpenPointer + 1,
237-
$tokens[$docCommentOpenPointer]['comment_closer']
238-
);
240+
$phpcsFile->fixer->replaceToken($docCommentOpenPointer, $fixedDocCommentContent);
241+
FixerHelper::removeBetweenIncluding($phpcsFile, $docCommentOpenPointer + 1, $tokens[$docCommentOpenPointer]['comment_closer']);
239242

240-
$phpcsFile->fixer->endChangeset();
241-
}
243+
$phpcsFile->fixer->endChangeset();
242244
}
243245
}
244246

0 commit comments

Comments
 (0)