Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Commit fd92ce6

Browse files
committed
[OptimizeUse] fix issue #32
1 parent 81bc374 commit fd92ce6

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/main/QafooLabs/Refactoring/Application/OptimizeUse.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ public function refactor(File $file)
3737
$occurances = $this->phpNameScanner->findNames($file);
3838
$class = $classes[0];
3939

40+
$appendNewLine = 0 === $class->namespaceDeclarationLine();
4041
$lastUseStatementLine = $class->namespaceDeclarationLine() + 2;
4142
$usedNames = array();
4243
$fqcns = array();
4344

4445
foreach ($occurances as $occurance) {
4546
$name = $occurance->name();
4647

47-
if ($name->type() === PhpName::TYPE_NAMESPACE) {
48+
if ($name->type() === PhpName::TYPE_NAMESPACE || $name->type() === PhpName::TYPE_CLASS) {
4849
continue;
4950
}
5051

@@ -67,7 +68,13 @@ public function refactor(File $file)
6768
$buffer->replaceString($occurance->declarationLine(), '\\'.$name->fullyQualifiedName(), $name->shortname());
6869

6970
if (!in_array($name->fullyQualifiedName(), $usedNames)) {
70-
$buffer->append($lastUseStatementLine, array(sprintf('use %s;', $name->fullyQualifiedName())));
71+
$lines = array(sprintf('use %s;', $name->fullyQualifiedName()));
72+
if ($appendNewLine) {
73+
$appendNewLine = FALSE;
74+
$lines[] = '';
75+
}
76+
77+
$buffer->append($lastUseStatementLine, $lines);
7178
$lastUseStatementLine++;
7279
}
7380
}

0 commit comments

Comments
 (0)