Skip to content

Commit

Permalink
bug #5598 GroupImportFixer - fix breaking code when fixing root class…
Browse files Browse the repository at this point in the history
…es (Leprechaunz)

This PR was squashed before being merged into the 2.18 branch.

Discussion
----------

GroupImportFixer - fix breaking code when fixing root classes

Fixes #5574

Commits
-------

486b598 GroupImportFixer - fix breaking code when fixing root classes
  • Loading branch information
keradus committed Apr 6, 2021
2 parents 02675fc + 486b598 commit 0d87d97
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Fixer/Import/GroupImportFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ private function addGroupUseStatements(array $statements, Tokens $tokens)
*/
private function getNamespaceNameWithSlash(NamespaceUseAnalysis $useDeclaration)
{
return substr($useDeclaration->getFullName(), 0, strripos($useDeclaration->getFullName(), '\\') + 1);
$position = strrpos($useDeclaration->getFullName(), '\\');
if (false === $position || 0 === $position) {
return $useDeclaration->getFullName();
}

return substr($useDeclaration->getFullName(), 0, $position + 1);
}

/**
Expand Down
30 changes: 30 additions & 0 deletions tests/Fixer/Import/GroupImportFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,36 @@ public function provideFixCases()
',
'<?php
use Foo\Bar;use Foo\Baz;\DontTouch::me();
',
],
[
'<?php
use Foo\{Bar, Baz};
use ReflectionClass;
use ReflectionMethod;
',
'<?php
use Foo\Bar;
use Foo\Baz;
use ReflectionClass;
use ReflectionMethod;
',
],
[
'<?php
use Foo\{Bar, Baz};
use \ReflectionClass;
use \ReflectionMethod;
',
'<?php
use Foo\Bar;
use Foo\Baz;
use \ReflectionClass;
use \ReflectionMethod;
',
],
];
Expand Down

0 comments on commit 0d87d97

Please sign in to comment.