Skip to content

Commit 2f31b7c

Browse files
committed
Add PhpDocPropertySorterFixer
1 parent 0ecb63a commit 2f31b7c

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/Fixer/PhpDocPropertySorterFixer.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
6262
}
6363

6464
$originalDocContent = $token->getContent();
65-
$sortedDocContent = $this->sortPropertiesInDocBlock($originalDocContent);
65+
$sortedDocContent = self::sortPropertiesInDocBlock($originalDocContent);
6666

6767
if ($originalDocContent !== $sortedDocContent) {
6868
$tokens[$index] = new Token([\T_DOC_COMMENT, $sortedDocContent]);
6969
}
7070
}
7171
}
7272

73-
private function sortPropertiesInDocBlock(string $docContent): string
73+
private static function sortPropertiesInDocBlock(string $docContent): string
7474
{
7575
$docLines = \explode("\n", $docContent);
7676
$processedLines = [];
@@ -80,7 +80,7 @@ private function sortPropertiesInDocBlock(string $docContent): string
8080
if (self::isPropertyAnnotation($line)) {
8181
$currentPropertyGroup[] = $line;
8282
} else {
83-
$this->flushPropertyGroup($currentPropertyGroup, $processedLines);
83+
self::flushPropertyGroup($currentPropertyGroup, $processedLines);
8484
$processedLines[] = $line;
8585
}
8686
}
@@ -99,31 +99,31 @@ private static function isPropertyAnnotation(string $line): bool
9999
* @param list<string> $propertyGroup
100100
* @param list<string> $processedLines
101101
*/
102-
private function flushPropertyGroup(array &$propertyGroup, array &$processedLines): void
102+
private static function flushPropertyGroup(array &$propertyGroup, array &$processedLines): void
103103
{
104104
if (\count($propertyGroup) === 0) {
105105
return;
106106
}
107107

108-
$this->sortPropertiesByName($propertyGroup);
108+
self::sortPropertiesByName($propertyGroup);
109109
$processedLines = \array_merge($processedLines, $propertyGroup);
110110
$propertyGroup = [];
111111
}
112112

113113
/**
114114
* @param list<string> $properties
115115
*/
116-
private function sortPropertiesByName(array &$properties): void
116+
private static function sortPropertiesByName(array &$properties): void
117117
{
118118
\usort($properties, static function (string $propertyA, string $propertyB): int {
119119
$nameA = self::extractPropertyName($propertyA);
120120
$nameB = self::extractPropertyName($propertyB);
121121

122-
return \strcmp($nameA, $nameB);
122+
return \strcmp($nameA ?? '', $nameB ?? '');
123123
});
124124
}
125125

126-
private static function extractPropertyName(string $propertyLine): string
126+
private static function extractPropertyName(string $propertyLine): ?string
127127
{
128128
$matches = [];
129129
Preg::match('/@property\\s+[^\\s]+\\s+\\$(\\w+)/', $propertyLine, $matches);
@@ -132,6 +132,6 @@ private static function extractPropertyName(string $propertyLine): string
132132
return $matches[1];
133133
}
134134

135-
return $propertyLine;
135+
return null;
136136
}
137137
}

tests/Fixer/PhpDocPropertySorterFixerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,23 @@ class Example {}
130130
yield [
131131
'<?php
132132
/**
133+
* @property mixed
134+
* @property bool $firstName
135+
* @property int $lastName
136+
*/
137+
',
138+
'<?php
139+
/**
140+
* @property int $lastName
141+
* @property mixed
142+
* @property bool $firstName
143+
*/
144+
',
145+
];
146+
147+
yield [
148+
'<?php
149+
/**
133150
* @property string $first
134151
* @property int $second
135152
*

0 commit comments

Comments
 (0)