|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the FiveLab CiRules package |
| 7 | + * |
| 8 | + * (c) FiveLab |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code |
| 12 | + */ |
| 13 | + |
| 14 | +namespace FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\Commenting; |
| 15 | + |
| 16 | +use FiveLab\Component\CiRules\PhpCs\FiveLab\ErrorCodes; |
| 17 | +use PHP_CodeSniffer\Files\File; |
| 18 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 19 | + |
| 20 | +class ProhibitedDocCommentsSniff implements Sniff |
| 21 | +{ |
| 22 | + public function register(): array |
| 23 | + { |
| 24 | + return [ |
| 25 | + T_DOC_COMMENT_OPEN_TAG, |
| 26 | + ]; |
| 27 | + } |
| 28 | + |
| 29 | + public function process(File $phpcsFile, mixed $stackPtr): void |
| 30 | + { |
| 31 | + $tokens = $phpcsFile->getTokens(); |
| 32 | + $commentToken = $tokens[$stackPtr]; |
| 33 | + |
| 34 | + $possiblyDeclaration = $phpcsFile->findNext([ |
| 35 | + T_CLASS, T_INTERFACE, T_TRAIT, T_FUNCTION, |
| 36 | + T_ABSTRACT, T_FINAL, T_ENUM |
| 37 | + ], $commentToken['comment_closer'] + 1, local: true); |
| 38 | + |
| 39 | + if ($possiblyDeclaration) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + $varPtr = $phpcsFile->findNext(T_VARIABLE, $commentToken['comment_closer'] + 1, local: true); |
| 44 | + |
| 45 | + if ($varPtr) { |
| 46 | + $commentTokenPtr = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($stackPtr + 1), $commentToken['comment_closer'], local: true); |
| 47 | + |
| 48 | + if (false === $commentTokenPtr) { |
| 49 | + $phpcsFile->addError( |
| 50 | + 'PHPDoc comment can contains only @var here.', |
| 51 | + $stackPtr, |
| 52 | + ErrorCodes::PHPDOC_NOT_ALLOWED |
| 53 | + ); |
| 54 | + |
| 55 | + return; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + $isOnlyVarTag = $this->isHasOnlyVarTag($phpcsFile, $stackPtr, $commentToken['comment_closer'], $tokens); |
| 60 | + |
| 61 | + if (true === $isOnlyVarTag) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $phpcsFile->addError( |
| 66 | + 'PHPDoc comment is not allowed here.', |
| 67 | + $stackPtr, |
| 68 | + ErrorCodes::PHPDOC_NOT_ALLOWED |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Check has T_DOC_COMMENT_TAG another tags |
| 74 | + * |
| 75 | + * @param File $phpcsFile |
| 76 | + * @param int $startPtr |
| 77 | + * @param int $endPtr |
| 78 | + * @param array<int, mixed> $tokens |
| 79 | + * |
| 80 | + * @return bool|null |
| 81 | + */ |
| 82 | + private function isHasOnlyVarTag(File $phpcsFile, int $startPtr, int $endPtr, array $tokens): ?bool |
| 83 | + { |
| 84 | + $commentTokenPtr = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($startPtr + 1), $endPtr); |
| 85 | + |
| 86 | + if (false === $commentTokenPtr) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + if ($commentTokenPtr && $tokens[$commentTokenPtr]['content'] !== '@var') { |
| 91 | + $phpcsFile->addError( |
| 92 | + \sprintf('PHPDoc comment tag %s is not allowed here.', $tokens[$commentTokenPtr]['content']), |
| 93 | + $commentTokenPtr, |
| 94 | + ErrorCodes::PHPDOC_NOT_ALLOWED |
| 95 | + ); |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + $this->isHasOnlyVarTag($phpcsFile, $commentTokenPtr, $endPtr, $tokens); |
| 101 | + |
| 102 | + return true; |
| 103 | + } |
| 104 | +} |
0 commit comments