Skip to content

Commit

Permalink
bug #4927 PhpdocAlignFixer - fix for whitespace in type (kubawerlos)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.0 branch.

Discussion
----------

PhpdocAlignFixer - fix for whitespace in type

Fixes #4879

Pinging `@fabpot`, `@Seldaek`, `@sstok`, `@GrahamCampbell`, `@keradus` (as authors) and `@enumag` (as issue reporter) for review.

Commits
-------

9711c2b PhpdocAlignFixer - fix for whitespace in type
  • Loading branch information
keradus committed Aug 29, 2021
2 parents 5262dfd + 9711c2b commit 810c3c0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/Fixer/Phpdoc/PhpdocAlignFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\DocBlock\TypeExpression;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
Expand Down Expand Up @@ -96,26 +97,27 @@ public function configure(array $configuration): void
$tagsWithoutNameToAlign = array_diff($this->configuration['tags'], $tagsWithNameToAlign, $tagsWithMethodSignatureToAlign);
$types = [];

$indent = '(?P<indent>(?: {2}|\t)*)';
$indent = '(?P<indent>(?:\ {2}|\t)*)';

// e.g. @param <hint> <$var>
if (!empty($tagsWithNameToAlign)) {
$types[] = '(?P<tag>'.implode('|', $tagsWithNameToAlign).')\s+(?P<hint>[^$]+?)\s+(?P<var>(?:&|\.{3})?\$[^\s]+)';
if ([] !== $tagsWithNameToAlign) {
$types[] = '(?P<tag>'.implode('|', $tagsWithNameToAlign).')\s+(?P<hint>(?:'.TypeExpression::REGEX_TYPES.')?)\s+(?P<var>(?:&|\.{3})?\$\S+)';
}

// e.g. @return <hint>
if (!empty($tagsWithoutNameToAlign)) {
$types[] = '(?P<tag2>'.implode('|', $tagsWithoutNameToAlign).')\s+(?P<hint2>[^\s]+?)';
if ([] !== $tagsWithoutNameToAlign) {
$types[] = '(?P<tag2>'.implode('|', $tagsWithoutNameToAlign).')\s+(?P<hint2>(?:'.TypeExpression::REGEX_TYPES.')?)';
}

// e.g. @method <hint> <signature>
if (!empty($tagsWithMethodSignatureToAlign)) {
if ([] !== $tagsWithMethodSignatureToAlign) {
$types[] = '(?P<tag3>'.implode('|', $tagsWithMethodSignatureToAlign).')(\s+(?P<hint3>[^\s(]+)|)\s+(?P<signature>.+\))';
}

// optional <desc>
$desc = '(?:\s+(?P<desc>\V*))';

$this->regex = '/^'.$indent.' \* @(?:'.implode('|', $types).')'.$desc.'\s*$/u';
$this->regex = '/^'.$indent.'\ \*\ @(?J)(?:'.implode('|', $types).')'.$desc.'\s*$/ux';
$this->regexCommentLine = '/^'.$indent.' \*(?! @)(?:\s+(?P<desc>\V+))(?<!\*\/)\r?$/u';
$this->align = $this->configuration['align'];
}
Expand Down
32 changes: 32 additions & 0 deletions tests/Fixer/Phpdoc/PhpdocAlignFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,4 +1286,36 @@ public function provideInvalidPhpdocCases()
],
];
}

public function testTypesContainingCallables(): void
{
$this->doTest(
'<?php
/**
* @param callable(Foo): Bar $x Description
* @param callable(FooFoo): BarBar $yy Description
*/
',
'<?php
/**
* @param callable(Foo): Bar $x Description
* @param callable(FooFoo): BarBar $yy Description
*/
'
);
}

public function testTypesContainingWhitespace(): void
{
$this->doTest('<?php
/**
* @var int $key
* @var iterable<int, string> $value
*/
/**
* @param array<int, $this> $arrayOfIntegers
* @param array<string, $this> $arrayOfStrings
*/
');
}
}

0 comments on commit 810c3c0

Please sign in to comment.