|
| 1 | +<?php |
| 2 | +namespace Magento2\Sniffs\Commenting; |
| 3 | + |
| 4 | +use PHP_CodeSniffer\Files\File; |
| 5 | +use PHP_CodeSniffer\Sniffs\AbstractVariableSniff; |
| 6 | +use PHP_CodeSniffer\Util\Tokens; |
| 7 | +use Magento2\Helpers\Commenting\PHPDocFormattingValidator; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class ClassPropertyPHPDocFormattingSniff |
| 11 | + */ |
| 12 | +class ClassPropertyPHPDocFormattingSniff extends AbstractVariableSniff |
| 13 | +{ |
| 14 | + |
| 15 | + /** |
| 16 | + * @var array |
| 17 | + */ |
| 18 | + private $ignoreTokens = [ |
| 19 | + T_PUBLIC, |
| 20 | + T_PRIVATE, |
| 21 | + T_PROTECTED, |
| 22 | + T_VAR, |
| 23 | + T_STATIC, |
| 24 | + T_WHITESPACE, |
| 25 | + ]; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var PHPDocFormattingValidator |
| 29 | + */ |
| 30 | + private $PHPDocFormattingValidator; |
| 31 | + |
| 32 | + /** |
| 33 | + * Constructs an ClassPropertyPHPDocFormattingSniff. |
| 34 | + */ |
| 35 | + public function __construct() |
| 36 | + { |
| 37 | + $scopes = Tokens::$ooScopeTokens; |
| 38 | + $this->PHPDocFormattingValidator = new PHPDocFormattingValidator(); |
| 39 | + $listen = [ |
| 40 | + T_VARIABLE, |
| 41 | + T_DOUBLE_QUOTED_STRING, |
| 42 | + T_HEREDOC, |
| 43 | + ]; |
| 44 | + |
| 45 | + parent::__construct($scopes, $listen, true); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritDoc |
| 50 | + */ |
| 51 | + public function processMemberVar(File $phpcsFile, $stackPtr) |
| 52 | + { |
| 53 | + $tokens = $phpcsFile->getTokens(); |
| 54 | + |
| 55 | + $commentEnd = $phpcsFile->findPrevious($this->ignoreTokens, ($stackPtr - 1), null, true); |
| 56 | + if ($commentEnd === false |
| 57 | + || ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG |
| 58 | + && $tokens[$commentEnd]['code'] !== T_COMMENT) |
| 59 | + ) { |
| 60 | + $phpcsFile->addWarning('Missing PHP DocBlock for class property.', $stackPtr, 'Missing'); |
| 61 | + return; |
| 62 | + } |
| 63 | + $commentStart = $tokens[$commentEnd]['comment_opener']; |
| 64 | + $foundVar = null; |
| 65 | + foreach ($tokens[$commentStart]['comment_tags'] as $tag) { |
| 66 | + if ($tokens[$tag]['content'] === '@var') { |
| 67 | + if ($foundVar !== null) { |
| 68 | + $error = 'Only one @var tag is allowed for class property declaration.'; |
| 69 | + $phpcsFile->addWarning($error, $tag, 'DuplicateVar'); |
| 70 | + } else { |
| 71 | + $foundVar = $tag; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if ($foundVar === null) { |
| 77 | + $error = 'Class properties must have type declaration using @var tag.'; |
| 78 | + $phpcsFile->addWarning($error, $stackPtr, 'MissingVar'); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd); |
| 83 | + if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) { |
| 84 | + $error = 'Content missing for @var tag in class property declaration.'; |
| 85 | + $phpcsFile->addWarning($error, $foundVar, 'EmptyVar'); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // Check if class has already have meaningful description after @var tag |
| 90 | + $isShortDescriptionAfterVar = $phpcsFile->findNext( |
| 91 | + T_DOC_COMMENT_STRING, |
| 92 | + $foundVar + 4, |
| 93 | + $commentEnd, |
| 94 | + false, |
| 95 | + null, |
| 96 | + false |
| 97 | + ); |
| 98 | + if ($this->PHPDocFormattingValidator->providesMeaning( |
| 99 | + $isShortDescriptionAfterVar, |
| 100 | + $commentStart, |
| 101 | + $tokens |
| 102 | + ) !== true) { |
| 103 | + preg_match( |
| 104 | + '`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i', |
| 105 | + $tokens[($foundVar + 2)]['content'], |
| 106 | + $varParts |
| 107 | + ); |
| 108 | + if ($varParts[1]) { |
| 109 | + return; |
| 110 | + } |
| 111 | + $error = 'Short description duplicates class property name.'; |
| 112 | + $phpcsFile->addWarning($error, $isShortDescriptionAfterVar, 'AlreadyHaveMeaningFulNameVar'); |
| 113 | + return; |
| 114 | + } |
| 115 | + // Check if class has already have meaningful description before @var tag |
| 116 | + $isShortDescriptionPreviousVar = $phpcsFile->findPrevious( |
| 117 | + T_DOC_COMMENT_STRING, |
| 118 | + $foundVar, |
| 119 | + $commentStart, |
| 120 | + false, |
| 121 | + null, |
| 122 | + false |
| 123 | + ); |
| 124 | + if ($this->PHPDocFormattingValidator->providesMeaning( |
| 125 | + $isShortDescriptionPreviousVar, |
| 126 | + $commentStart, |
| 127 | + $tokens |
| 128 | + ) !== true) { |
| 129 | + preg_match( |
| 130 | + '`^((?:\|?(?:array\([^\)]*\)|[\\\\\[\]]+))*)( .*)?`i', |
| 131 | + $tokens[($foundVar + 2)]['content'], |
| 132 | + $varParts |
| 133 | + ); |
| 134 | + if ($varParts[1]) { |
| 135 | + return; |
| 136 | + } |
| 137 | + $error = 'Short description duplicates class property name.'; |
| 138 | + $phpcsFile->addWarning($error, $isShortDescriptionPreviousVar, 'AlreadyHaveMeaningFulNameVar'); |
| 139 | + return; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @inheritDoc |
| 145 | + * phpcs:disable Magento2.CodeAnalysis.EmptyBlock |
| 146 | + */ |
| 147 | + protected function processVariable(File $phpcsFile, $stackPtr) |
| 148 | + { |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @inheritDoc |
| 153 | + * phpcs:disable Magento2.CodeAnalysis.EmptyBlock |
| 154 | + */ |
| 155 | + protected function processVariableInString(File $phpcsFile, $stackPtr) |
| 156 | + { |
| 157 | + } |
| 158 | +} |
0 commit comments