Skip to content

Commit

Permalink
GroupImportFixer - fix breaking code when fixing root classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Leprechaunz authored and keradus committed Apr 6, 2021
1 parent 9bf2760 commit 486b598
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 486b598

Please sign in to comment.