bestit /
PHP_CodeSniffer
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace BestIt\Sniffs\DocTags; |
||
| 6 | |||
| 7 | use BestIt\CodeSniffer\CodeWarning; |
||
| 8 | use BestIt\CodeSniffer\Helper\DocTagHelper; |
||
| 9 | use BestIt\CodeSniffer\Helper\LineHelper; |
||
| 10 | use BestIt\Sniffs\AbstractSniff; |
||
| 11 | use function array_filter; |
||
| 12 | use function array_key_exists; |
||
| 13 | use function array_shift; |
||
| 14 | use function preg_match; |
||
| 15 | use function str_pad; |
||
| 16 | use function str_repeat; |
||
| 17 | use function strcasecmp; |
||
| 18 | use function strlen; |
||
| 19 | use function usort; |
||
| 20 | use const T_DOC_COMMENT_OPEN_TAG; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Checks the sorting and grouping of the doc comment tags. |
||
| 24 | * |
||
| 25 | * Same tags are grouped. The groups are sorted by occurrence, where the tags which only occure once are collected in |
||
| 26 | * one block. Blocks of tags with the same occurrence-count are sorted alphabetically. |
||
| 27 | * |
||
| 28 | * The return tag comes last, everytime! |
||
| 29 | * |
||
| 30 | * @author blange <[email protected]> |
||
| 31 | * @package BestIt\Sniffs\DocTags |
||
| 32 | */ |
||
| 33 | class TagSortingSniff extends AbstractSniff |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * You SHOULD separate tag groups and the final return with a newline. |
||
| 37 | */ |
||
| 38 | public const CODE_MISSING_NEWLINE_BETWEEN_TAGS = 'MissingNewlineBetweenTags'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * You SHOULD sort the tags by their occurrence and then alphabetically, but @return SHOULD be the last. |
||
| 42 | */ |
||
| 43 | public const CODE_WRONG_TAG_SORTING = 'WrongTagSorting'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The message for the missing new line between tags. |
||
| 47 | */ |
||
| 48 | private const MESSAGE_MISSING_NEWLINE_BETWEEN_TAGS = 'There should be a newline after the tag (group): %s.'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The message for the wrong sorting order. |
||
| 52 | */ |
||
| 53 | private const MESSAGE_WRONG_TAG_SORTING = 'Please provide the tags in occurrence and then alphabetical order |
||
| 54 | (a-z) but with return at last position.'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The doc tag helper. |
||
| 58 | * |
||
| 59 | * @var DocTagHelper |
||
| 60 | */ |
||
| 61 | private DocTagHelper $docTagHelper; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 62 | |||
| 63 | /** |
||
| 64 | * The loaded tokens of this comment. |
||
| 65 | * |
||
| 66 | * @var array|null |
||
| 67 | */ |
||
| 68 | private ?array $loadedTagTokens = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns true if the requirements for this sniff are met. |
||
| 72 | * |
||
| 73 | * @return bool Are the requirements met and the sniff should proceed? |
||
| 74 | */ |
||
| 75 | protected function areRequirementsMet(): bool |
||
| 76 | { |
||
| 77 | return !$this->isSniffSuppressed() && (bool) $this->getTagTokens(); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Checks for line break errors and registers the errors if there are any. |
||
| 82 | * |
||
| 83 | * This should be called after the sorting is already checked and fixed! |
||
| 84 | * |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | private function checkAndRegisterLineBreakErrors(): void |
||
| 88 | { |
||
| 89 | $tokens = $this->getTagTokens(); |
||
| 90 | $prevToken = []; |
||
| 91 | $tagCounts = $this->docTagHelper->getTagCounts($tokens); |
||
| 92 | $withReturn = false; |
||
| 93 | |||
| 94 | foreach ($tokens as $tokenPos => $token) { |
||
| 95 | $thisTagContent = $token['content']; |
||
| 96 | $isReturn = $thisTagContent === '@return'; |
||
| 97 | |||
| 98 | $isGroupSwitch = |
||
| 99 | // Did we switch the tag ... |
||
| 100 | $prevToken && ($prevToken['content'] !== $thisTagContent) && |
||
| 101 | // ... but do we skip singles or are we entering the return block which should contain only 1 tag. |
||
| 102 | (($tagCounts[$thisTagContent] !== 1) || ($isReturn && !$withReturn)); |
||
| 103 | |||
| 104 | // Insert new line between groups or before the return tag if there is no line break already. |
||
| 105 | if ($isGroupSwitch && (($prevToken['line'] + 1) === $token['line'])) { |
||
| 106 | $isFixing = $this->file->addFixableWarning( |
||
| 107 | static::MESSAGE_MISSING_NEWLINE_BETWEEN_TAGS, |
||
| 108 | $tokenPos, |
||
| 109 | static::CODE_MISSING_NEWLINE_BETWEEN_TAGS, |
||
| 110 | [ |
||
| 111 | $prevToken['content'] |
||
| 112 | ] |
||
| 113 | ); |
||
| 114 | |||
| 115 | if ($isFixing) { |
||
| 116 | $this->insertNewLine($token); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | $prevToken = $token; |
||
| 121 | // Return should be the last element, so this is ok. |
||
| 122 | $withReturn = $isReturn; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Checks the alphabetic sorting and registers and error if it sorted wrongly. |
||
| 128 | * |
||
| 129 | * @throws CodeWarning If the tags are not correctly sorted. |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | private function checkAndRegisterSortingError(): void |
||
| 134 | { |
||
| 135 | $orgTokens = $this->getTagTokens(); |
||
| 136 | $sortedTokens = $this->sortTokens($orgTokens); |
||
| 137 | |||
| 138 | if (array_values($orgTokens) !== $sortedTokens) { |
||
| 139 | $error = (new CodeWarning( |
||
| 140 | static::CODE_WRONG_TAG_SORTING, |
||
| 141 | self::MESSAGE_WRONG_TAG_SORTING, |
||
| 142 | array_shift($orgTokens)['pointer'] |
||
| 143 | ))->setToken($this->token); |
||
| 144 | |||
| 145 | $error->isFixable(true); |
||
| 146 | |||
| 147 | throw $error; |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * The callback to sort tokens. |
||
| 153 | * |
||
| 154 | * 1. @return goes to the bottom |
||
| 155 | * 2. Single tags are group alphabetically in the top group. |
||
| 156 | * 3. Groups are sorted then by occurrence, that the largest group is the last one before the return. |
||
| 157 | * 4. Same annotations are kept in the order of their code. |
||
| 158 | * |
||
| 159 | * @param array $leftToken Provided by usort. |
||
| 160 | * @param array $rightToken Provided by usort. |
||
| 161 | * @param array $tagCounts Saves the occurence count for every tag. |
||
| 162 | * |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | private function compareTokensForSorting(array $leftToken, array $rightToken, array $tagCounts): int |
||
| 166 | { |
||
| 167 | $leftTagName = $leftToken['content']; |
||
| 168 | $rightTagName = $rightToken['content']; |
||
| 169 | $realLeftTagName = $this->getRealtagName($leftTagName); |
||
| 170 | $realRightTagName = $this->getRealtagName($rightTagName); |
||
| 171 | |||
| 172 | // If they have the same content, leave them, where they where ... |
||
| 173 | $return = $leftToken['line'] > $rightToken['line'] ? 1 : -1; |
||
| 174 | |||
| 175 | // But if they are different. |
||
| 176 | if ($realLeftTagName !== $realRightTagName) { |
||
| 177 | $leftTagCount = $tagCounts[$leftTagName]; |
||
| 178 | $rightTagCount = $tagCounts[$rightTagName]; |
||
| 179 | |||
| 180 | switch (true) { |
||
| 181 | // Move return to bottom everytime ... |
||
| 182 | case ($realLeftTagName === '@return'): |
||
| 183 | $return = 1; |
||
| 184 | break; |
||
| 185 | |||
| 186 | // ... yes, everytime |
||
| 187 | case ($realRightTagName === '@return'): |
||
| 188 | $return = -1; |
||
| 189 | break; |
||
| 190 | |||
| 191 | // Move single items to the top. |
||
| 192 | case ($leftTagCount !== $rightTagCount): |
||
| 193 | $return = $leftTagCount > $rightTagCount ? +1 : -1; |
||
| 194 | break; |
||
| 195 | |||
| 196 | // Compare tag name |
||
| 197 | default: |
||
| 198 | $return = strcasecmp($realLeftTagName, $realRightTagName) > 1 ? 1 : -1; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | return $return; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Sorts the tags and creates a new doc comment part for them to replace it with the old content. |
||
| 207 | * |
||
| 208 | * @return string The new content. |
||
| 209 | */ |
||
| 210 | private function createNewSortedTagsContent(): string |
||
| 211 | { |
||
| 212 | $file = $this->file; |
||
| 213 | $eolChar = $file->eolChar; |
||
| 214 | $newContent = ''; |
||
| 215 | $prevTagContent = ''; |
||
| 216 | $sortedTags = $this->sortTokens($this->getTagTokens()); |
||
| 217 | $tagCounts = $this->docTagHelper->getTagCounts($sortedTags); |
||
| 218 | $withReturn = false; |
||
| 219 | |||
| 220 | foreach ($sortedTags as $tag) { |
||
| 221 | $lineStartingPadding = str_pad('', $tag['column'] - 3, ' '); |
||
| 222 | $thisTagContent = $tag['content']; |
||
| 223 | $isReturn = $thisTagContent === '@return'; |
||
| 224 | |||
| 225 | $isGroupSwitch = |
||
| 226 | // Did we switch the tag ... |
||
| 227 | $prevTagContent && ($prevTagContent !== $thisTagContent) && |
||
| 228 | // ... but do we skip singles or are we entering the return block which should contain only 1 tag. |
||
| 229 | (($tagCounts[$thisTagContent] !== 1) || ($isReturn && !$withReturn)); |
||
| 230 | |||
| 231 | // Insert new line between groups. |
||
| 232 | if ($isGroupSwitch) { |
||
| 233 | $newContent .= $lineStartingPadding . '*' . $eolChar; |
||
| 234 | } |
||
| 235 | |||
| 236 | // Create the new Tag. |
||
| 237 | // WARNING We do not need a line break in the tag summary. |
||
| 238 | $newContent .= $lineStartingPadding . '* ' . trim($thisTagContent); |
||
| 239 | |||
| 240 | if ($tag['contents']) { |
||
| 241 | $prevLine = $tag['line']; |
||
| 242 | foreach ($tag['contents'] as $subToken) { |
||
| 243 | // If we have a line switch, we need to create the correct indentation from before ... |
||
| 244 | if ($withLineSwitch = $subToken['line'] > $prevLine) { |
||
| 245 | $newContent .= $eolChar . |
||
| 246 | $lineStartingPadding . '*' . |
||
| 247 | str_repeat(' ', $subToken['column'] - strlen($lineStartingPadding) - 2); |
||
| 248 | |||
| 249 | $prevLine = $subToken['line']; |
||
| 250 | } |
||
| 251 | |||
| 252 | // ... if we have no line switch, then an additional whitespace is enough. |
||
| 253 | $newContent .= ($withLineSwitch ? '' : ' ') . $subToken['content']; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | $newContent .= $eolChar; |
||
| 258 | |||
| 259 | $prevTagContent = $thisTagContent; |
||
| 260 | $withReturn = $isReturn; |
||
| 261 | } |
||
| 262 | |||
| 263 | $newContent .= $lineStartingPadding . '*/' . $eolChar; |
||
| 264 | |||
| 265 | return $newContent; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Sorts the tokens in blocks of their occurences and then alphabetically, but the return at last. |
||
| 270 | * |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | private function fixSorting(): void |
||
| 274 | { |
||
| 275 | $fixer = $this->file->fixer; |
||
| 276 | |||
| 277 | $fixer->beginChangeset(); |
||
| 278 | |||
| 279 | $firstTag = $this->removeOldTagLines(); |
||
| 280 | |||
| 281 | $fixer->addContent($firstTag['pointer'] - 1, $this->createNewSortedTagsContent()); |
||
| 282 | |||
| 283 | $fixer->endChangeset(); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Support for annotations with variable values (like the symfony annotations) which should not influence sorting! |
||
| 288 | * |
||
| 289 | * @param string $tagName The tag name out of the code. |
||
| 290 | * |
||
| 291 | * @return string The tag name without "dynamic values". |
||
| 292 | */ |
||
| 293 | private function getRealtagName(string $tagName): string |
||
| 294 | { |
||
| 295 | $matches = []; |
||
| 296 | |||
| 297 | return (preg_match('/(?P<realTag>@\w+)(?P<separator>$|\(|\\\\|\s)/', $tagName, $matches)) |
||
| 298 | ? $matches['realTag'] |
||
| 299 | : $tagName; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Returns the tokens of the comment tags. |
||
| 304 | * |
||
| 305 | * @return array The tokens of the comment tags. |
||
| 306 | */ |
||
| 307 | private function getTagTokens(): array |
||
| 308 | { |
||
| 309 | if ($this->loadedTagTokens === null) { |
||
| 310 | $this->loadedTagTokens = $this->loadTagTokens(); |
||
| 311 | } |
||
| 312 | |||
| 313 | return $this->loadedTagTokens; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Insert the new line before the given token. |
||
| 318 | * |
||
| 319 | * @param array $token The token where a newline should be. |
||
| 320 | * |
||
| 321 | * @return void |
||
| 322 | */ |
||
| 323 | private function insertNewLine(array $token): void |
||
| 324 | { |
||
| 325 | $fixer = $this->file->fixer; |
||
| 326 | $lineStartPadding = str_pad('', $token['column'] - 3, ' '); |
||
| 327 | |||
| 328 | $fixer->beginChangeset(); |
||
| 329 | |||
| 330 | // Remove the whitespace between the tag and the comments star. |
||
| 331 | $fixer->replaceToken($token['pointer'] - 1, ''); |
||
| 332 | |||
| 333 | $fixer->addContentBefore( |
||
| 334 | $token['pointer'], |
||
| 335 | $this->file->eolChar . $lineStartPadding . '* ' |
||
| 336 | ); |
||
| 337 | |||
| 338 | $fixer->endChangeset(); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Loads the tokens of this comment. |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | private function loadTagTokens(): array |
||
| 347 | { |
||
| 348 | $barrier = 0; |
||
| 349 | $tokens = $this->docTagHelper->getTagTokens(); |
||
| 350 | |||
| 351 | $tokens = array_filter($tokens, function (array $token) use (&$barrier): bool { |
||
| 352 | $allowed = true; |
||
| 353 | |||
| 354 | if ($barrier) { |
||
| 355 | if ($allowed = $token['column'] <= $barrier) { |
||
| 356 | $barrier = 0; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | if ($allowed && array_key_exists('contents', $token)) { |
||
| 361 | $barrier = $token['column']; |
||
| 362 | } |
||
| 363 | |||
| 364 | return $allowed; |
||
| 365 | }); |
||
| 366 | |||
| 367 | return $tokens; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Processes a found registered token. |
||
| 372 | * |
||
| 373 | * @return void |
||
| 374 | */ |
||
| 375 | protected function processToken(): void |
||
| 376 | { |
||
| 377 | try { |
||
| 378 | $this->checkAndRegisterSortingError(); |
||
| 379 | |||
| 380 | $this->checkAndRegisterLineBreakErrors(); |
||
| 381 | } catch (CodeWarning $exception) { |
||
| 382 | $fixable = $this->getExceptionHandler()->handleException($exception); |
||
| 383 | |||
| 384 | if ($fixable) { |
||
| 385 | $this->fixSorting(); |
||
| 386 | } |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Returns an array of registered tokens. |
||
| 392 | * |
||
| 393 | * @return int[] Returns array of tokens to listen for |
||
| 394 | */ |
||
| 395 | public function register(): array |
||
| 396 | { |
||
| 397 | return [T_DOC_COMMENT_OPEN_TAG]; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Removed the lines with the wrongly sorted tags. |
||
| 402 | * |
||
| 403 | * @return array The first tag token of this doc block. |
||
| 404 | */ |
||
| 405 | private function removeOldTagLines(): array |
||
| 406 | { |
||
| 407 | $tags = $this->getTagTokens(); |
||
| 408 | $firstTag = array_shift($tags); |
||
| 409 | |||
| 410 | (new LineHelper($this->file)) |
||
| 411 | ->removeLines( |
||
| 412 | $firstTag['line'], |
||
| 413 | $this->tokens[$this->token['comment_closer']]['line'] |
||
| 414 | ); |
||
| 415 | |||
| 416 | return $firstTag; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Do you want to setup things before processing the token? |
||
| 421 | * |
||
| 422 | * @return void |
||
| 423 | */ |
||
| 424 | protected function setUp(): void |
||
| 425 | { |
||
| 426 | $this->addPointerToTokens(); |
||
| 427 | |||
| 428 | $this->docTagHelper = new DocTagHelper( |
||
| 429 | $this->file, |
||
| 430 | $this->stackPos, |
||
| 431 | $this->tokens |
||
| 432 | ); |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Sorts the tokens in blocks of their occurrences and then alphabetically, but the return at last. |
||
| 437 | * |
||
| 438 | * @param array $tokens The tokens. |
||
| 439 | * |
||
| 440 | * @return array The sorted tokens. |
||
| 441 | */ |
||
| 442 | private function sortTokens(array $tokens): array |
||
| 443 | { |
||
| 444 | $tagCounts = $this->docTagHelper->getTagCounts($tokens); |
||
| 445 | |||
| 446 | usort($tokens, function (array $leftToken, array $rightToken) use ($tagCounts): int { |
||
| 447 | return $this->compareTokensForSorting($leftToken, $rightToken, $tagCounts); |
||
| 448 | }); |
||
| 449 | |||
| 450 | return $tokens; |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Removes the loaded tag tokens. |
||
| 455 | * |
||
| 456 | * @return void |
||
| 457 | */ |
||
| 458 | protected function tearDown(): void |
||
| 459 | { |
||
| 460 | parent::tearDown(); |
||
| 461 | |||
| 462 | $this->loadedTagTokens = null; |
||
| 463 | } |
||
| 464 | } |
||
| 465 |