|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © Magento. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | +namespace Magento2\Sniffs\Commenting; |
| 8 | + |
| 9 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 10 | +use PHP_CodeSniffer\Files\File; |
| 11 | + |
| 12 | +/** |
| 13 | + * Detects PHPDoc formatting for classes and interfaces. |
| 14 | + */ |
| 15 | +class ClassAndInterfacePHPDocFormattingSniff implements Sniff |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var PHPDocFormattingValidator |
| 19 | + */ |
| 20 | + private $PHPDocFormattingValidator; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var string[] List of tags that can not be used in comments |
| 24 | + */ |
| 25 | + public $forbiddenTags = [ |
| 26 | + '@category', |
| 27 | + '@package', |
| 28 | + '@subpackage' |
| 29 | + ]; |
| 30 | + |
| 31 | + /** |
| 32 | + * Helper initialisation |
| 33 | + */ |
| 34 | + public function __construct() |
| 35 | + { |
| 36 | + $this->PHPDocFormattingValidator = new PHPDocFormattingValidator(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @inheritDoc |
| 41 | + */ |
| 42 | + public function register() |
| 43 | + { |
| 44 | + return [ |
| 45 | + T_CLASS, |
| 46 | + T_INTERFACE |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @inheritDoc |
| 52 | + */ |
| 53 | + public function process(File $phpcsFile, $stackPtr) |
| 54 | + { |
| 55 | + $tokens = $phpcsFile->getTokens(); |
| 56 | + |
| 57 | + $namePtr = $phpcsFile->findNext(T_STRING, $stackPtr + 1, null, false, null, true); |
| 58 | + |
| 59 | + $commentStartPtr = $phpcsFile->findPrevious( |
| 60 | + [ |
| 61 | + T_WHITESPACE, |
| 62 | + T_DOC_COMMENT_STAR, |
| 63 | + T_DOC_COMMENT_WHITESPACE, |
| 64 | + T_DOC_COMMENT_TAG, |
| 65 | + T_DOC_COMMENT_STRING, |
| 66 | + T_DOC_COMMENT_CLOSE_TAG |
| 67 | + ], |
| 68 | + $stackPtr - 1, |
| 69 | + null, |
| 70 | + true, |
| 71 | + null, |
| 72 | + true |
| 73 | + ); |
| 74 | + |
| 75 | + if ($tokens[$commentStartPtr]['code'] !== T_DOC_COMMENT_OPEN_TAG) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if ($this->PHPDocFormattingValidator->providesMeaning($namePtr, $commentStartPtr, $tokens) !== true) { |
| 80 | + $phpcsFile->addWarning( |
| 81 | + sprintf( |
| 82 | + '%s description should contain additional information beyond the name already supplies.', |
| 83 | + ucfirst($tokens[$stackPtr]['content']) |
| 84 | + ), |
| 85 | + $stackPtr, |
| 86 | + 'InvalidDescription' |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + $this->validateTags($phpcsFile, $commentStartPtr, $tokens); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Validates that forbidden tags are not used in comment |
| 95 | + * |
| 96 | + * @param File $phpcsFile |
| 97 | + * @param int $commentStartPtr |
| 98 | + * @param array $tokens |
| 99 | + * @return bool |
| 100 | + */ |
| 101 | + private function validateTags(File $phpcsFile, $commentStartPtr, $tokens) |
| 102 | + { |
| 103 | + $commentCloserPtr = $tokens[$commentStartPtr]['comment_closer']; |
| 104 | + |
| 105 | + for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) { |
| 106 | + if ($tokens[$i]['code'] !== T_DOC_COMMENT_TAG) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + if (in_array($tokens[$i]['content'], $this->forbiddenTags) === true) { |
| 111 | + $phpcsFile->addWarning( |
| 112 | + sprintf('Tag %s MUST NOT be used.', $tokens[$i]['content']), |
| 113 | + $i, |
| 114 | + 'ForbiddenTags' |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return false; |
| 120 | + } |
| 121 | +} |
0 commit comments