Skip to content

Commit

Permalink
minor #5391 PhpdocOrderByValueFixer - Add additional annotations to s…
Browse files Browse the repository at this point in the history
…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
SpacePossum committed Dec 31, 2020
2 parents bb6ee85 + f98de85 commit f6c598f
Show file tree
Hide file tree
Showing 3 changed files with 497 additions and 8 deletions.
2 changes: 1 addition & 1 deletion doc/rules/phpdoc/phpdoc_order_by_value.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Configuration

List of annotations to order, e.g. ``["covers"]``.

Allowed values: a subset of ``['author', 'covers', 'coversNothing', 'dataProvider', 'depends', 'group', 'internal', 'requires', 'throws', 'uses']``
Allowed values: a subset of ``['author', 'covers', 'coversNothing', 'dataProvider', 'depends', 'group', 'internal', 'method', 'property', 'property-read', 'property-write', 'requires', 'throws', 'uses']``

Default value: ``['covers']``

Expand Down
43 changes: 36 additions & 7 deletions src/Fixer/Phpdoc/PhpdocOrderByValueFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
Expand Down Expand Up @@ -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,
Expand All @@ -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))
);

Expand Down Expand Up @@ -167,6 +181,10 @@ protected function createConfigurationDefinition()
'depends',
'group',
'internal',
'method',
'property',
'property-read',
'property-write',
'requires',
'throws',
'uses',
Expand All @@ -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',
])
Expand Down
Loading

0 comments on commit f6c598f

Please sign in to comment.