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: Improve how FQCN is handled in phpDoc #7622

Merged
merged 5 commits into from
Dec 28, 2023
Merged
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
16 changes: 8 additions & 8 deletions src/Fixer/Import/FullyQualifiedStrictTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,16 @@ private function fixPhpDoc(Tokens $tokens, int $index, array $uses, string $name
{
$phpDoc = $tokens[$index];
$phpDocContent = $phpDoc->getContent();
Preg::matchAll('#@([^\s]+)\s+([^\s}]+)#', $phpDocContent, $matches);
Preg::matchAll('#@([^\s]+)(\s+)([a-zA-Z0-9_\\\\]+)#', $phpDocContent, $matches);

if ([] !== $matches) {
foreach ($matches[2] as $i => $typeName) {
if ([] !== $matches[0]) {
foreach ($matches[3] as $i => $typeName) {
if (!\in_array($matches[1][$i], ['param', 'return', 'see', 'throws', 'var'], true)) {
Wirone marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

if (true === $this->configuration['import_symbols'] && isset($matches[2][0])) {
$this->registerSymbolForImport('class', $matches[2][0], $uses, $namespaceName);
if (true === $this->configuration['import_symbols'] && isset($matches[3][0])) {
$this->registerSymbolForImport('class', $matches[3][0], $uses, $namespaceName);
}

$shortTokens = $this->determineShortType($typeName, $uses, $namespaceName);
Expand All @@ -280,16 +280,16 @@ private function fixPhpDoc(Tokens $tokens, int $index, array $uses, string $name
// Replace tag+type in order to avoid replacing type multiple times (when same type is used in multiple places)
$phpDocContent = str_replace(
$matches[0][$i],
'@'.$matches[1][$i].' '.implode('', array_map(
'@'.$matches[1][$i].$matches[2][$i].implode('', array_map(
static fn (Token $token) => $token->getContent(),
$shortTokens
)),
$phpDocContent
);

$tokens[$index] = new Token([T_DOC_COMMENT, $phpDocContent]);
keradus marked this conversation as resolved.
Show resolved Hide resolved
}
}

$tokens[$index] = new Token([T_DOC_COMMENT, $phpDocContent]);
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,39 @@ function foo($dateTime) {}',
function foo($dateTime) {}',
['leading_backslash_in_global_namespace' => true],
];

yield 'ignore @see with URL' => [
'<?php
/**
* @see http://example.com
*/
define(\'FOO_BAR\', true);',
];

yield 'Respect whitespace between phpDoc annotation and value' => [
'<?php

namespace Foo\Test;

use Foo\Bar;

/**
* @param Bar $a
* @see Bar
*/
function foo($a) {}',
'<?php

namespace Foo\Test;

use Foo\Bar;

/**
* @param \Foo\Bar $a
* @see \Foo\Bar
*/
function foo($a) {}',
];
}

/**
Expand Down