|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
6 | 6 | declare(strict_types=1);
|
| 7 | + |
7 | 8 | namespace Magento\Sniffs\Annotation;
|
8 | 9 |
|
9 | 10 | use PHP_CodeSniffer\Files\File;
|
@@ -249,6 +250,60 @@ public function validateTagGroupingFormat(File $phpcsFile, int $commentStartPtr)
|
249 | 250 | }
|
250 | 251 | }
|
251 | 252 |
|
| 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 | + |
252 | 307 | /**
|
253 | 308 | * Validates extra newline before short description
|
254 | 309 | *
|
|
0 commit comments