function DocCommentSpacingSniff::checkAnnotationsGroupsOrder
*
Parameters
list<list<Annotation>> $annotationsGroups: * @param list<Annotation> $annotations
1 call to DocCommentSpacingSniff::checkAnnotationsGroupsOrder()
- DocCommentSpacingSniff::checkAnnotationsGroups in vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ Commenting/ DocCommentSpacingSniff.php - *
File
-
vendor/
slevomat/ coding-standard/ SlevomatCodingStandard/ Sniffs/ Commenting/ DocCommentSpacingSniff.php, line 443
Class
Namespace
SlevomatCodingStandard\Sniffs\CommentingCode
private function checkAnnotationsGroupsOrder(File $phpcsFile, int $docCommentOpenerPointer, array $annotationsGroups, array $annotations) : void {
$getAnnotationsPointers = static function (Annotation $annotation) : int {
return $annotation->getStartPointer();
};
$equals = static function (array $firstAnnotationsGroup, array $secondAnnotationsGroup) use ($getAnnotationsPointers) : bool {
$firstAnnotationsPointers = array_map($getAnnotationsPointers, $firstAnnotationsGroup);
$secondAnnotationsPointers = array_map($getAnnotationsPointers, $secondAnnotationsGroup);
return count(array_diff($firstAnnotationsPointers, $secondAnnotationsPointers)) === 0 && count(array_diff($secondAnnotationsPointers, $firstAnnotationsPointers)) === 0;
};
$sortedAnnotationsGroups = $this->sortAnnotationsToGroups($annotations);
$incorrectAnnotationsGroupsExist = false;
$annotationsGroupsPositions = [];
$fix = false;
$undefinedAnnotationsGroups = [];
foreach ($annotationsGroups as $annotationsGroupPosition => $annotationsGroup) {
foreach ($sortedAnnotationsGroups as $sortedAnnotationsGroupPosition => $sortedAnnotationsGroup) {
if ($equals($annotationsGroup, $sortedAnnotationsGroup)) {
$annotationsGroupsPositions[$annotationsGroupPosition] = $sortedAnnotationsGroupPosition;
continue 2;
}
$undefinedAnnotationsGroup = true;
foreach ($annotationsGroup as $annotation) {
foreach ($this->getAnnotationsGroups() as $annotationNames) {
foreach ($annotationNames as $annotationName) {
if ($this->isAnnotationMatched($annotation, $annotationName)) {
$undefinedAnnotationsGroup = false;
break 3;
}
}
}
}
if ($undefinedAnnotationsGroup) {
$undefinedAnnotationsGroups[] = $annotationsGroupPosition;
continue 2;
}
}
$incorrectAnnotationsGroupsExist = true;
$fix = $phpcsFile->addFixableError('Incorrect annotations group.', $annotationsGroup[0]->getStartPointer(), self::CODE_INCORRECT_ANNOTATIONS_GROUP);
}
if (count($annotationsGroupsPositions) === 0 && count($undefinedAnnotationsGroups) > 1) {
$incorrectAnnotationsGroupsExist = true;
$fix = $phpcsFile->addFixableError('Incorrect annotations group.', $annotationsGroups[0][0]->getStartPointer(), self::CODE_INCORRECT_ANNOTATIONS_GROUP);
}
if (!$incorrectAnnotationsGroupsExist) {
foreach ($undefinedAnnotationsGroups as $undefinedAnnotationsGroupPosition) {
$annotationsGroupsPositions[$undefinedAnnotationsGroupPosition] = (count($annotationsGroupsPositions) > 0 ? max($annotationsGroupsPositions) : 0) + 1;
}
ksort($annotationsGroupsPositions);
$positionsMappedToGroups = array_keys($annotationsGroupsPositions);
$tmp = array_values($annotationsGroupsPositions);
asort($tmp);
/** @var list<int> $normalizedAnnotationsGroupsPositions */
$normalizedAnnotationsGroupsPositions = array_combine(array_keys($positionsMappedToGroups), array_keys($tmp));
foreach ($normalizedAnnotationsGroupsPositions as $normalizedAnnotationsGroupPosition => $sortedAnnotationsGroupPosition) {
if ($normalizedAnnotationsGroupPosition === $sortedAnnotationsGroupPosition) {
continue;
}
$fix = $phpcsFile->addFixableError('Incorrect order of annotations groups.', $annotationsGroups[$positionsMappedToGroups[$normalizedAnnotationsGroupPosition]][0]->getStartPointer(), self::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_GROUPS);
break;
}
}
foreach ($annotationsGroups as $annotationsGroupPosition => $annotationsGroup) {
if (!array_key_exists($annotationsGroupPosition, $annotationsGroupsPositions)) {
continue;
}
if (!array_key_exists($annotationsGroupsPositions[$annotationsGroupPosition], $sortedAnnotationsGroups)) {
continue;
}
$sortedAnnotationsGroup = $sortedAnnotationsGroups[$annotationsGroupsPositions[$annotationsGroupPosition]];
foreach ($annotationsGroup as $annotationPosition => $annotation) {
if ($annotation === $sortedAnnotationsGroup[$annotationPosition]) {
continue;
}
$fix = $phpcsFile->addFixableError('Incorrect order of annotations in group.', $annotation->getStartPointer(), self::CODE_INCORRECT_ORDER_OF_ANNOTATIONS_IN_GROUP);
break;
}
}
if (!$fix) {
return;
}
$firstAnnotation = $annotationsGroups[0][0];
$lastAnnotationsGroup = $annotationsGroups[count($annotationsGroups) - 1];
$lastAnnotation = $lastAnnotationsGroup[count($lastAnnotationsGroup) - 1];
$indentation = IndentationHelper::getIndentation($phpcsFile, $docCommentOpenerPointer);
$fixedAnnotations = '';
$firstGroup = true;
foreach ($sortedAnnotationsGroups as $sortedAnnotationsGroup) {
if ($firstGroup) {
$firstGroup = false;
}
else {
for ($i = 0; $i < $this->linesCountBetweenAnnotationsGroups; $i++) {
$fixedAnnotations .= sprintf('%s *%s', $indentation, $phpcsFile->eolChar);
}
}
foreach ($sortedAnnotationsGroup as $sortedAnnotation) {
$fixedAnnotations .= sprintf('%s * %s%s', $indentation, trim(TokenHelper::getContent($phpcsFile, $sortedAnnotation->getStartPointer(), $sortedAnnotation->getEndPointer())), $phpcsFile->eolChar);
}
}
$tokens = $phpcsFile->getTokens();
$docCommentCloserPointer = $tokens[$docCommentOpenerPointer]['comment_closer'];
$endOfLineBeforeFirstAnnotation = TokenHelper::findPreviousContent($phpcsFile, T_DOC_COMMENT_WHITESPACE, $phpcsFile->eolChar, $firstAnnotation->getStartPointer() - 1, $docCommentOpenerPointer);
$docCommentContentEndPointer = TokenHelper::findNextContent($phpcsFile, T_DOC_COMMENT_WHITESPACE, $phpcsFile->eolChar, $lastAnnotation->getEndPointer() + 1, $docCommentCloserPointer);
if ($docCommentContentEndPointer === null) {
$docCommentContentEndPointer = $lastAnnotation->getEndPointer();
}
$phpcsFile->fixer
->beginChangeset();
if ($endOfLineBeforeFirstAnnotation === null) {
FixerHelper::change($phpcsFile, $docCommentOpenerPointer, $docCommentContentEndPointer, '/**' . $phpcsFile->eolChar . $fixedAnnotations);
}
else {
FixerHelper::change($phpcsFile, $endOfLineBeforeFirstAnnotation + 1, $docCommentContentEndPointer, $fixedAnnotations);
}
$phpcsFile->fixer
->endChangeset();
}