Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle unions in phpdoc_types_order #6248

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DocBlock/Annotation.php
Expand Up @@ -204,11 +204,11 @@ public function getTypes(): array
*
* @param string[] $types
*/
public function setTypes(array $types): void
public function setTypes(array $types, string $unionType = '|'): void
{
$pattern = '/'.preg_quote($this->getTypesContent(), '/').'/';

$this->lines[0]->setContent(Preg::replace($pattern, implode('|', $types), $this->lines[0]->getContent(), 1));
$this->lines[0]->setContent(Preg::replace($pattern, implode($unionType, $types), $this->lines[0]->getContent(), 1));

$this->clearCache();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Phpdoc/PhpdocTypesOrderFixer.php
Expand Up @@ -142,7 +142,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$types = $annotation->getTypes();

// fix main types
$annotation->setTypes($this->sortTypes($types));
$annotation->setTypes($this->sortTypes($types), str_contains($annotation->getContent(), '&') ? '&' : '|');

// fix @method parameters types
$line = $doc->getLine($annotation->getStart());
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixer/Phpdoc/PhpdocTypesOrderFixerTest.php
Expand Up @@ -159,6 +159,9 @@ public function provideFixCases(): array
[
'<?php /** @return array<int, callable(array<string, null|string> , DateTime): bool> */',
],
[
'<?php /** @return ContainerInterface&SymfonyContainerInterface */',
],
];
}

Expand Down