-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #5391 PhpdocOrderByValueFixer - Add additional annotations to s…
…ort (localheinz) This PR was merged into the 2.17 branch. Discussion ---------- PhpdocOrderByValueFixer - Add additional annotations to sort This PR * [x] adjusts the `phpdoc_order_by_value` fixer to allow sorting of additional annotations by value * [x] `@method` * [x] `@property` * [x] `@property-read` * [x] `@property-write` Commits ------- f98de85 Enhancement: Allow sorting of property, property-read, and property-write annotations by value Enhancement: Allow sorting of method annotations by value
- Loading branch information
Showing
3 changed files
with
497 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
use PhpCsFixer\Preg; | ||
use PhpCsFixer\Tokenizer\Token; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
use Symfony\Component\OptionsResolver\Options; | ||
|
||
/** | ||
* @author Filippo Tessarotto <[email protected]> | ||
|
@@ -96,7 +97,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) | |
} | ||
|
||
for ($index = $tokens->count() - 1; $index > 0; --$index) { | ||
foreach ($this->configuration['annotations'] as $type) { | ||
foreach ($this->configuration['annotations'] as $type => $typeLowerCase) { | ||
$findPattern = sprintf( | ||
'/@%s\s.+@%s\s/s', | ||
$type, | ||
|
@@ -113,20 +114,33 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) | |
$docBlock = new DocBlock($tokens[$index]->getContent()); | ||
|
||
$annotations = $docBlock->getAnnotationsOfType($type); | ||
|
||
$annotationMap = []; | ||
|
||
$replacePattern = sprintf( | ||
'/\*\s*@%s\s+(.+)/', | ||
$type | ||
); | ||
if (\in_array($type, ['property', 'property-read', 'property-write'], true)) { | ||
$replacePattern = sprintf( | ||
'/(?s)\*\s*@%s\s+(?P<optionalTypes>.+\s+)?\$(?P<comparableContent>[^\s]+).*/', | ||
$type | ||
); | ||
|
||
$replacement = '\2'; | ||
} elseif ('method' === $type) { | ||
$replacePattern = '/(?s)\*\s*@method\s+(?P<optionalReturnTypes>.+\s+)?(?P<comparableContent>.+)\(.*/'; | ||
$replacement = '\2'; | ||
} else { | ||
$replacePattern = sprintf( | ||
'/\*\s*@%s\s+(?P<comparableContent>.+)/', | ||
$typeLowerCase | ||
); | ||
|
||
$replacement = '\1'; | ||
} | ||
|
||
foreach ($annotations as $annotation) { | ||
$rawContent = $annotation->getContent(); | ||
|
||
$comparableContent = Preg::replace( | ||
$replacePattern, | ||
'\1', | ||
$replacement, | ||
strtolower(trim($rawContent)) | ||
); | ||
|
||
|
@@ -167,6 +181,10 @@ protected function createConfigurationDefinition() | |
'depends', | ||
'group', | ||
'internal', | ||
'method', | ||
'property', | ||
'property-read', | ||
'property-write', | ||
'requires', | ||
'throws', | ||
'uses', | ||
|
@@ -180,6 +198,17 @@ protected function createConfigurationDefinition() | |
->setAllowedValues([ | ||
new AllowedValueSubset($allowedValues), | ||
]) | ||
->setNormalizer(function (Options $options, $value) { | ||
$normalized = []; | ||
|
||
foreach ($value as $index => $annotation) { | ||
// since we will be using strtolower on the input annotations when building the sorting | ||
// map we must match the type in lower case as well | ||
$normalized[$annotation] = strtolower($annotation); | ||
} | ||
|
||
return $normalized; | ||
}) | ||
->setDefault([ | ||
'covers', | ||
]) | ||
|
Oops, something went wrong.