|
11 | 11 |
|
12 | 12 | namespace PhpCsFixerCustomFixers\Fixer; |
13 | 13 |
|
14 | | -use PhpCsFixer\FixerDefinition\CodeSample; |
15 | | -use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 14 | +use PhpCsFixer\Fixer\DeprecatedFixerInterface; |
16 | 15 | use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
17 | | -use PhpCsFixer\Preg; |
18 | | -use PhpCsFixer\Tokenizer\Token; |
19 | 16 | use PhpCsFixer\Tokenizer\Tokens; |
20 | 17 |
|
21 | 18 | /** |
| 19 | + * @deprecated |
| 20 | + * |
22 | 21 | * @no-named-arguments |
23 | 22 | */ |
24 | | -final class PhpDocPropertySorterFixer extends AbstractFixer |
| 23 | +final class PhpDocPropertySorterFixer extends AbstractFixer implements DeprecatedFixerInterface |
25 | 24 | { |
| 25 | + private PhpdocPropertySortedFixer $phpdocPropertySortedFixer; |
| 26 | + |
| 27 | + public function __construct() |
| 28 | + { |
| 29 | + $this->phpdocPropertySortedFixer = new PhpdocPropertySortedFixer(); |
| 30 | + } |
| 31 | + |
26 | 32 | public function getDefinition(): FixerDefinitionInterface |
27 | 33 | { |
28 | | - return new FixerDefinition( |
29 | | - 'Sorts @property annotations in PHPDoc blocks alphabetically within groups separated by empty lines.', |
30 | | - [new CodeSample('<?php |
31 | | -/** |
32 | | - * @property string $zzz |
33 | | - * @property int $aaa |
34 | | - * @property bool $mmm |
35 | | - */ |
36 | | -class Foo {} |
37 | | -')], |
38 | | - '', |
39 | | - ); |
| 34 | + return $this->phpdocPropertySortedFixer->getDefinition(); |
40 | 35 | } |
41 | 36 |
|
42 | 37 | public function getPriority(): int |
43 | 38 | { |
44 | | - return 0; |
| 39 | + return $this->phpdocPropertySortedFixer->getPriority(); |
45 | 40 | } |
46 | 41 |
|
47 | 42 | public function isCandidate(Tokens $tokens): bool |
48 | 43 | { |
49 | | - return $tokens->isTokenKindFound(\T_DOC_COMMENT); |
| 44 | + return $this->phpdocPropertySortedFixer->isCandidate($tokens); |
50 | 45 | } |
51 | 46 |
|
52 | 47 | public function isRisky(): bool |
53 | 48 | { |
54 | | - return false; |
| 49 | + return $this->phpdocPropertySortedFixer->isRisky(); |
55 | 50 | } |
56 | 51 |
|
57 | 52 | public function fix(\SplFileInfo $file, Tokens $tokens): void |
58 | 53 | { |
59 | | - foreach ($tokens as $index => $token) { |
60 | | - if (!$token->isGivenKind(\T_DOC_COMMENT)) { |
61 | | - continue; |
62 | | - } |
63 | | - |
64 | | - $originalDocContent = $token->getContent(); |
65 | | - $sortedDocContent = self::sortPropertiesInDocBlock($originalDocContent); |
66 | | - |
67 | | - if ($originalDocContent !== $sortedDocContent) { |
68 | | - $tokens[$index] = new Token([\T_DOC_COMMENT, $sortedDocContent]); |
69 | | - } |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - private static function sortPropertiesInDocBlock(string $docContent): string |
74 | | - { |
75 | | - $docLines = \explode("\n", $docContent); |
76 | | - $processedLines = []; |
77 | | - $currentPropertyGroup = []; |
78 | | - |
79 | | - foreach ($docLines as $line) { |
80 | | - if (self::isPropertyAnnotation($line)) { |
81 | | - $currentPropertyGroup[] = $line; |
82 | | - } else { |
83 | | - self::flushPropertyGroup($currentPropertyGroup, $processedLines); |
84 | | - $processedLines[] = $line; |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - return \implode("\n", $processedLines); |
89 | | - } |
90 | | - |
91 | | - private static function isPropertyAnnotation(string $line): bool |
92 | | - { |
93 | | - return Preg::match('/@property/', $line); |
94 | | - } |
95 | | - |
96 | | - /** |
97 | | - * Sorts and adds a property group to the processed lines. |
98 | | - * |
99 | | - * @param list<string> $propertyGroup |
100 | | - * @param list<string> $processedLines |
101 | | - */ |
102 | | - private static function flushPropertyGroup(array &$propertyGroup, array &$processedLines): void |
103 | | - { |
104 | | - if (\count($propertyGroup) === 0) { |
105 | | - return; |
106 | | - } |
107 | | - |
108 | | - self::sortPropertiesByName($propertyGroup); |
109 | | - $processedLines = \array_merge($processedLines, $propertyGroup); |
110 | | - $propertyGroup = []; |
| 54 | + $this->phpdocPropertySortedFixer->fix($file, $tokens); |
111 | 55 | } |
112 | 56 |
|
113 | 57 | /** |
114 | | - * @param list<string> $properties |
| 58 | + * @return list<string> |
115 | 59 | */ |
116 | | - private static function sortPropertiesByName(array &$properties): void |
| 60 | + public function getSuccessorsNames(): array |
117 | 61 | { |
118 | | - \usort($properties, static function (string $propertyA, string $propertyB): int { |
119 | | - $nameA = self::extractPropertyName($propertyA); |
120 | | - $nameB = self::extractPropertyName($propertyB); |
121 | | - |
122 | | - return \strcmp($nameA ?? '', $nameB ?? ''); |
123 | | - }); |
124 | | - } |
125 | | - |
126 | | - private static function extractPropertyName(string $propertyLine): ?string |
127 | | - { |
128 | | - $matches = []; |
129 | | - Preg::match('/@property(?:-read|-write)?\\s+[^\\s]+\\s+\\$(\\w+)/', $propertyLine, $matches); |
130 | | - /** @var array<array-key, string> $matches */ |
131 | | - if (\count($matches) > 1) { |
132 | | - return $matches[1]; |
133 | | - } |
134 | | - |
135 | | - return null; |
| 62 | + return [$this->phpdocPropertySortedFixer->getName()]; |
136 | 63 | } |
137 | 64 | } |
0 commit comments