|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +namespace MoodleHQ\MoodleCS\moodle\Sniffs\Commenting; |
| 18 | + |
| 19 | +// phpcs:disable moodle.NamingConventions |
| 20 | + |
| 21 | +use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil; |
| 22 | +use MoodleHQ\MoodleCS\moodle\Util\Docblocks; |
| 23 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 24 | +use PHP_CodeSniffer\Files\File; |
| 25 | +use PHPCSUtils\Utils\ObjectDeclarations; |
| 26 | + |
| 27 | +/** |
| 28 | + * Checks that all test classes and global functions have appropriate @package tags. |
| 29 | + * |
| 30 | + * @copyright 2024 Andrew Lyons <[email protected]> |
| 31 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 32 | + */ |
| 33 | +class PackageSniff implements Sniff { |
| 34 | + |
| 35 | + /** |
| 36 | + * Register for open tag (only process once per file). |
| 37 | + */ |
| 38 | + public function register() { |
| 39 | + return [ |
| 40 | + T_OPEN_TAG, |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Processes php files and perform various checks with file. |
| 46 | + * |
| 47 | + * @param File $phpcsFile The file being scanned. |
| 48 | + * @param int $stackPtr The position in the stack. |
| 49 | + */ |
| 50 | + public function process(File $phpcsFile, $stackPtr) { |
| 51 | + $tokens = $phpcsFile->getTokens(); |
| 52 | + |
| 53 | + $docblock = Docblocks::getDocBlock($phpcsFile, $stackPtr); |
| 54 | + if ($docblock) { |
| 55 | + $filePackageFound = $this->checkDocblock( |
| 56 | + $phpcsFile, |
| 57 | + $stackPtr, |
| 58 | + $docblock |
| 59 | + ); |
| 60 | + if ($filePackageFound) { |
| 61 | + return; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + $find = [ |
| 66 | + T_CLASS, |
| 67 | + T_FUNCTION, |
| 68 | + T_TRAIT, |
| 69 | + T_INTERFACE, |
| 70 | + ]; |
| 71 | + $typePtr = $stackPtr + 1; |
| 72 | + while ($typePtr = $phpcsFile->findNext($find, $typePtr + 1)) { |
| 73 | + $token = $tokens[$typePtr]; |
| 74 | + if ($token['code'] === T_FUNCTION && !empty($token['conditions'])) { |
| 75 | + // Skip methods of classes, traits and interfaces. |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + $docblock = Docblocks::getDocBlock($phpcsFile, $typePtr); |
| 80 | + |
| 81 | + if ($docblock === null) { |
| 82 | + $objectName = $this->getObjectName($phpcsFile, $typePtr); |
| 83 | + $objectType = $this->getObjectType($phpcsFile, $typePtr); |
| 84 | + $phpcsFile->addError('Missing doc comment for %s %s', $typePtr, 'Missing', [$objectType, $objectName]); |
| 85 | + |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + $this->checkDocblock($phpcsFile, $typePtr, $docblock); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get the human-readable object type. |
| 96 | + * |
| 97 | + * @param File $phpcsFile |
| 98 | + * @param int $stackPtr |
| 99 | + * @return string |
| 100 | + */ |
| 101 | + protected function getObjectType( |
| 102 | + File $phpcsFile, |
| 103 | + int $stackPtr |
| 104 | + ): string { |
| 105 | + $tokens = $phpcsFile->getTokens(); |
| 106 | + if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { |
| 107 | + return 'file'; |
| 108 | + } |
| 109 | + return $tokens[$stackPtr]['content']; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get the human readable object name. |
| 114 | + * |
| 115 | + * @param File $phpcsFile |
| 116 | + * @param int $stackPtr |
| 117 | + * @return string |
| 118 | + */ |
| 119 | + protected function getObjectName( |
| 120 | + File $phpcsFile, |
| 121 | + int $stackPtr |
| 122 | + ): string { |
| 123 | + $tokens = $phpcsFile->getTokens(); |
| 124 | + if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { |
| 125 | + return basename($phpcsFile->getFilename()); |
| 126 | + } |
| 127 | + |
| 128 | + return ObjectDeclarations::getName($phpcsFile, $stackPtr); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Check the docblock for a @package tag. |
| 133 | + * |
| 134 | + * @param File $phpcsFile |
| 135 | + * @param int $stackPtr |
| 136 | + * @param array $docblock |
| 137 | + * @return bool Whether any package tag was found, whether or not it was correct |
| 138 | + */ |
| 139 | + protected function checkDocblock( |
| 140 | + File $phpcsFile, |
| 141 | + int $stackPtr, |
| 142 | + array $docblock |
| 143 | + ): bool { |
| 144 | + $tokens = $phpcsFile->getTokens(); |
| 145 | + $objectName = $this->getObjectName($phpcsFile, $stackPtr); |
| 146 | + $objectType = $this->getObjectType($phpcsFile, $stackPtr); |
| 147 | + $expectedPackage = MoodleUtil::getMoodleComponent($phpcsFile, true); |
| 148 | + |
| 149 | + $packageTokens = Docblocks::getMatchingDocTags($phpcsFile, $stackPtr, '@package'); |
| 150 | + if (empty($packageTokens)) { |
| 151 | + $fix = $phpcsFile->addFixableError( |
| 152 | + 'DocBlock missing a @package tag for %s %s. Expected @package %s', |
| 153 | + $stackPtr, |
| 154 | + 'Missing', |
| 155 | + [$objectType, $objectName, $expectedPackage] |
| 156 | + ); |
| 157 | + |
| 158 | + if ($fix) { |
| 159 | + $phpcsFile->fixer->beginChangeset(); |
| 160 | + $phpcsFile->fixer->addContentBefore($docblock['comment_closer'], '* @package ' . $expectedPackage . PHP_EOL . ' '); |
| 161 | + $phpcsFile->fixer->endChangeset(); |
| 162 | + } |
| 163 | + |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + if (count($packageTokens) > 1) { |
| 168 | + $fix = $phpcsFile->addFixableError( |
| 169 | + 'More than one @package tag found in %s %s.', |
| 170 | + $stackPtr, |
| 171 | + 'Multiple', |
| 172 | + [$objectType, $objectName] |
| 173 | + ); |
| 174 | + |
| 175 | + if ($fix) { |
| 176 | + $phpcsFile->fixer->beginChangeset(); |
| 177 | + $validTokenFound = false; |
| 178 | + |
| 179 | + foreach ($packageTokens as $i => $packageToken) { |
| 180 | + $packageValuePtr = $phpcsFile->findNext( |
| 181 | + T_DOC_COMMENT_STRING, |
| 182 | + $packageToken, |
| 183 | + $docblock['comment_closer'] |
| 184 | + ); |
| 185 | + $packageValue = $tokens[$packageValuePtr]['content']; |
| 186 | + if (!$validTokenFound && $packageValue === $expectedPackage) { |
| 187 | + $validTokenFound = true; |
| 188 | + continue; |
| 189 | + } |
| 190 | + $lineNo = $tokens[$packageToken]['line']; |
| 191 | + foreach (array_keys(MoodleUtil::getTokensOnLine($phpcsFile, $lineNo)) as $lineToken) { |
| 192 | + $phpcsFile->fixer->replaceToken($lineToken, ''); |
| 193 | + } |
| 194 | + } |
| 195 | + if (!$validTokenFound) { |
| 196 | + $phpcsFile->fixer->addContentBefore($packageTokens[0], ' * @package ' . $expectedPackage . PHP_EOL); |
| 197 | + } |
| 198 | + $phpcsFile->fixer->endChangeset(); |
| 199 | + } |
| 200 | + return true; |
| 201 | + } |
| 202 | + |
| 203 | + $packageToken = reset($packageTokens); |
| 204 | + |
| 205 | + // Check the value of the package tag. |
| 206 | + $packageValuePtr = $phpcsFile->findNext( |
| 207 | + T_DOC_COMMENT_STRING, |
| 208 | + $packageToken, |
| 209 | + $docblock['comment_closer'] |
| 210 | + ); |
| 211 | + $packageValue = $tokens[$packageValuePtr]['content']; |
| 212 | + |
| 213 | + // Compare to expected value. |
| 214 | + if ($packageValue === $expectedPackage) { |
| 215 | + return true; |
| 216 | + } |
| 217 | + |
| 218 | + $fix = $phpcsFile->addFixableError( |
| 219 | + 'Incorrect @package tag for %s %s. Expected %s, found %s.', |
| 220 | + $packageToken, |
| 221 | + 'Incorrect', |
| 222 | + [$objectType, $objectName, $expectedPackage, $packageValue] |
| 223 | + ); |
| 224 | + |
| 225 | + if ($fix) { |
| 226 | + $phpcsFile->fixer->beginChangeset(); |
| 227 | + $phpcsFile->fixer->replaceToken($packageValuePtr, $expectedPackage); |
| 228 | + $phpcsFile->fixer->endChangeset(); |
| 229 | + } |
| 230 | + |
| 231 | + return true; |
| 232 | + } |
| 233 | +} |
0 commit comments