Skip to content

Commit

Permalink
fix: Do not override short name with relative reference (#7752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wirone authored Jan 16, 2024
1 parent 66d2e15 commit 8c041c8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Fixer/Import/FullyQualifiedStrictTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private function shortenSymbol(string $fqcn, array $uses, string $namespaceName)
$tmpRes = substr($fqcn, \strlen($namespaceName) + 1);
if (!isset($this->cacheUseNameByShortNameLower[strtolower(explode('\\', $tmpRes, 2)[0])])) {
$res = $tmpRes;
$iMin = substr_count($namespaceName, '\\') - 1;
$iMin = substr_count($namespaceName, '\\') + 1;
}
}

Expand Down
81 changes: 81 additions & 0 deletions tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,29 @@ class Cl {}
['import_symbols' => true],
];

yield 'shorten relative reference to already imported, direct short name' => [
<<<'EOD'
<?php
namespace Foo\Bar\Baz;
use Foo\Bar;
use Foo\Bar\A\B;
final class Buzz extends Bar implements B {}
final class Fuzz extends Bar implements B {}
EOD,
<<<'EOD'
<?php
namespace Foo\Bar\Baz;
use Foo\Bar;
use Foo\Bar\A\B;
final class Buzz extends Bar implements Bar\A\B {}
final class Fuzz extends Bar implements B {}
EOD,
];

yield 'fix to longest imported name' => [
<<<'EOD'
<?php
Expand Down Expand Up @@ -876,6 +899,64 @@ class Cl {}
EOD,
];

yield 'shortening - namespace with shorter import' => [
<<<'EOD'
<?php
namespace U\V\W;
use U\V;
new \U();
new V();
new V\W();
new X();
new X\Y();
new X\Y\Z();
EOD,
];

yield 'shortening - namespace with same import' => [
<<<'EOD'
<?php
namespace U\V\W;
use U\V\W;
new \U();
new \U\V();
new W();
new X();
new X\Y();
new X\Y\Z();
EOD,
];

yield 'shortening - namespace with useless import' => [
<<<'EOD'
<?php
namespace U\V\W;
use U\V\W\X;
new \U();
new \U\V();
new \U\V\W();
new X();
new X\Y();
new X\Y\Z();
EOD,
];

yield 'shortening - namespace with longer import' => [
<<<'EOD'
<?php
namespace U\V\W;
use U\V\W\X\Y;
new \U();
new \U\V();
new \U\V\W();
new X();
new Y();
new Y\Z();
new Y\Z\e();
new Y\Z\e\f();
EOD,
];

yield 'do not fix class named the same as imported function' => [
<<<'EOD'
<?php
Expand Down

0 comments on commit 8c041c8

Please sign in to comment.