Skip to content

Commit

Permalink
bug: FullyQualifiedStrictTypesFixer - fix shortening when namespace…
Browse files Browse the repository at this point in the history
… is not empty and import exists (#7027)

Co-authored-by: Kuba Werłos <werlos@gmail.com>
  • Loading branch information
Wirone and kubawerlos committed Jun 16, 2023
1 parent 22e1953 commit 5f2e4a5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/Fixer/Import/FullyQualifiedStrictTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,13 @@ private function replaceByShortType(Tokens $tokens, TypeAnalysis $type, array $u
$namespaceNameLength = \strlen($namespaceName);
$types = $this->getTypes($tokens, $typeStartIndex, $type->getEndIndex());

$prefix = $namespaceName ? '\\'.$namespaceName.'\\' : '\\';
foreach ($types as $typeName => [$startIndex, $endIndex]) {
$typeNameLower = strtolower($typeName);
if (!str_starts_with($typeNameLower, $prefix)) {
continue; // no shorter type possible
if (!str_starts_with($typeName, '\\')) {
continue; // Not a FQCN, no shorter type possible
}

$typeName = substr($typeName, 1);
$typeNameLower = substr($typeNameLower, 1);
$typeNameLower = strtolower($typeName);

if (isset($uses[$typeNameLower])) {
// if the type without leading "\" equals any of the full "uses" long names, it can be replaced with the short one
Expand All @@ -163,7 +161,11 @@ private function replaceByShortType(Tokens $tokens, TypeAnalysis $type, array $u
}

$tokens->overrideRange($startIndex, $endIndex, $this->namespacedStringToTokens($typeName));
} elseif ($typeNameLower !== $namespaceName && str_starts_with($typeNameLower, $namespaceName)) {
} elseif (!str_contains($typeName, '\\')) {
// If we're NOT in the global namespace, there's no related import,
// AND used type is from global namespace, then it can't be shortened.
continue;
} elseif ($typeNameLower !== $namespaceName && str_starts_with($typeNameLower, $namespaceName.'\\')) {
// if the type starts with namespace and the type is not the same as the namespace it can be shortened
$typeNameShort = substr($typeName, $namespaceNameLength + 1);

Expand Down
27 changes: 25 additions & 2 deletions tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class FullyQualifiedStrictTypesFixerTest extends AbstractFixerTestCase
/**
* @dataProvider provideNewLogicCases
*/
public function testNewLogic(string $expected, ?string $input): void
public function testNewLogic(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
Expand Down Expand Up @@ -104,8 +104,31 @@ function A(\A\B\C\D\Foo $fix, \X\B\C\D\E\Bar $doNotFix) {}
'<?php use \A\Exception; function foo(\A\Exception $e) {}',
];

yield 'common prefix' => [
yield 'common prefix 1' => [
'<?php namespace Foo; function foo(\FooBar $v): \FooBar {}',
];

yield 'common prefix 2' => [
'<?php namespace Foo; function foo(\FooBar\Baz $v): \FooBar {}',
];

yield 'issue #7025 - non-empty namespace, import and FQCN in argument' => [
'<?php namespace foo\bar\baz;
use foo\baz\buzz;
class A {
public function b(buzz $buzz): void {
}
}',
'<?php namespace foo\bar\baz;
use foo\baz\buzz;
class A {
public function b(\foo\baz\buzz $buzz): void {
}
}',
null,
];
}
Expand Down

0 comments on commit 5f2e4a5

Please sign in to comment.