|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of fast-forward/dev-tools. |
| 7 | + * |
| 8 | + * This source file is subject to the license bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + * |
| 11 | + * @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | + * @license https://opensource.org/licenses/MIT MIT License |
| 13 | + * |
| 14 | + * @see https://github.com/php-fast-forward/dev-tools |
| 15 | + * @see https://github.com/php-fast-forward |
| 16 | + * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | + */ |
| 18 | + |
| 19 | +namespace FastForward\DevTools\Docblock; |
| 20 | + |
| 21 | +use Override; |
| 22 | +use phootwork\collection\ArrayList; |
| 23 | +use phpowermove\docblock\Docblock; |
| 24 | +use phpowermove\docblock\tags\AbstractTag; |
| 25 | +use ReflectionClass; |
| 26 | +use ReflectionFunctionAbstract; |
| 27 | +use ReflectionProperty; |
| 28 | + |
| 29 | +/** |
| 30 | + * Represents a Docblock that preserves and sorts tags in a defined order for PHPDoc normalization. |
| 31 | + */ |
| 32 | +final class OrderedDocblock extends Docblock |
| 33 | +{ |
| 34 | + /** |
| 35 | + * Creates an OrderedDocblock instance from a docblock string or reflection. |
| 36 | + * |
| 37 | + * @param ReflectionFunctionAbstract|ReflectionClass|ReflectionProperty|string $docblock the docblock source |
| 38 | + * |
| 39 | + * @return self the created OrderedDocblock instance |
| 40 | + */ |
| 41 | + #[Override] |
| 42 | + public static function create( |
| 43 | + ReflectionFunctionAbstract|ReflectionClass|ReflectionProperty|string $docblock = '' |
| 44 | + ): self { |
| 45 | + return new self($docblock); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns the tags sorted by the defined priority order: param, return, throws, then others. |
| 50 | + * |
| 51 | + * @return ArrayList<AbstractTag> the sorted tags |
| 52 | + */ |
| 53 | + #[Override] |
| 54 | + public function getSortedTags(): ArrayList |
| 55 | + { |
| 56 | + $tagOrder = [ |
| 57 | + 'param' => 10, |
| 58 | + 'return' => 20, |
| 59 | + 'throws' => 30, |
| 60 | + ]; |
| 61 | + |
| 62 | + $indexedTags = []; |
| 63 | + |
| 64 | + /** @var AbstractTag $tag */ |
| 65 | + foreach ($this->getTags()->toArray() as $index => $tag) { |
| 66 | + $indexedTags[] = [ |
| 67 | + 'index' => $index, |
| 68 | + 'tag' => $tag, |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + usort($indexedTags, static function (array $left, array $right) use ($tagOrder): int { |
| 73 | + /** @var AbstractTag $leftTag */ |
| 74 | + $leftTag = $left['tag']; |
| 75 | + |
| 76 | + /** @var AbstractTag $rightTag */ |
| 77 | + $rightTag = $right['tag']; |
| 78 | + |
| 79 | + $leftPriority = $tagOrder[$leftTag->getTagName()] ?? 1000; |
| 80 | + $rightPriority = $tagOrder[$rightTag->getTagName()] ?? 1000; |
| 81 | + |
| 82 | + if ($leftPriority !== $rightPriority) { |
| 83 | + return $leftPriority <=> $rightPriority; |
| 84 | + } |
| 85 | + |
| 86 | + $tagNameComparison = $leftTag->getTagName() <=> $rightTag->getTagName(); |
| 87 | + if (0 !== $tagNameComparison) { |
| 88 | + return $tagNameComparison; |
| 89 | + } |
| 90 | + |
| 91 | + return $left['index'] <=> $right['index']; |
| 92 | + }); |
| 93 | + |
| 94 | + $sorted = new ArrayList(); |
| 95 | + |
| 96 | + foreach ($indexedTags as $item) { |
| 97 | + $sorted->add($item['tag']); |
| 98 | + } |
| 99 | + |
| 100 | + return $sorted; |
| 101 | + } |
| 102 | +} |
0 commit comments