Skip to content

Commit 604c78f

Browse files
authored
ENGCOM-4774: #22317: CodeSniffer should not mark correctly aligned DocBlock elements as code style violation. #22321
2 parents e05cfbe + ea9faba commit 604c78f

File tree

3 files changed

+182
-37
lines changed

3 files changed

+182
-37
lines changed

dev/tests/static/framework/Magento/Sniffs/Annotation/AnnotationFormatValidator.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* See COPYING.txt for license details.
55
*/
66
declare(strict_types=1);
7+
78
namespace Magento\Sniffs\Annotation;
89

910
use PHP_CodeSniffer\Files\File;
@@ -249,6 +250,60 @@ public function validateTagGroupingFormat(File $phpcsFile, int $commentStartPtr)
249250
}
250251
}
251252

253+
/**
254+
* Validates tag aligning format
255+
*
256+
* @param File $phpcsFile
257+
* @param int $commentStartPtr
258+
*/
259+
public function validateTagAligningFormat(File $phpcsFile, int $commentStartPtr) : void
260+
{
261+
$tokens = $phpcsFile->getTokens();
262+
$noAlignmentPositions = [];
263+
$actualPositions = [];
264+
$stackPtr = null;
265+
foreach ($tokens[$commentStartPtr]['comment_tags'] as $tag) {
266+
$content = $tokens[$tag]['content'];
267+
if (preg_match('/^@/', $content) && ($tokens[$tag]['line'] === $tokens[$tag + 2]['line'])) {
268+
$noAlignmentPositions[] = $tokens[$tag + 1]['column'] + 1;
269+
$actualPositions[] = $tokens[$tag + 2]['column'];
270+
$stackPtr = $stackPtr ?? $tag;
271+
}
272+
}
273+
274+
if (!$this->allTagsAligned($actualPositions)
275+
&& !$this->noneTagsAligned($actualPositions, $noAlignmentPositions)) {
276+
$phpcsFile->addFixableError(
277+
'Tags visual alignment must be consistent',
278+
$stackPtr,
279+
'MethodArguments'
280+
);
281+
}
282+
}
283+
284+
/**
285+
* Check whether all docblock params are aligned.
286+
*
287+
* @param array $actualPositions
288+
* @return bool
289+
*/
290+
private function allTagsAligned(array $actualPositions)
291+
{
292+
return count(array_unique($actualPositions)) === 1;
293+
}
294+
295+
/**
296+
* Check whether all docblock params are not aligned.
297+
*
298+
* @param array $actualPositions
299+
* @param array $noAlignmentPositions
300+
* @return bool
301+
*/
302+
private function noneTagsAligned(array $actualPositions, array $noAlignmentPositions)
303+
{
304+
return $actualPositions === $noAlignmentPositions;
305+
}
306+
252307
/**
253308
* Validates extra newline before short description
254309
*

dev/tests/static/framework/Magento/Sniffs/Annotation/MethodAnnotationStructureSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function process(File $phpcsFile, $stackPtr)
7575
$emptyTypeTokens
7676
);
7777
$this->annotationFormatValidator->validateTagGroupingFormat($phpcsFile, $commentStartPtr);
78+
$this->annotationFormatValidator->validateTagAligningFormat($phpcsFile, $commentStartPtr);
7879
}
7980
}
8081
}

0 commit comments

Comments
 (0)