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

Commit 9db4ce7

Browse files
committed
[GH-31] Fix problems with renaming classes.
1 parent d7e352f commit 9db4ce7

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use QafooLabs\Refactoring\Domain\Model\File;
1919
use QafooLabs\Refactoring\Domain\Model\PhpName;
2020
use QafooLabs\Refactoring\Domain\Model\PhpNameChange;
21+
use QafooLabs\Refactoring\Domain\Model\PhpNames\NoImportedUsagesFilter;
2122

2223
class FixClassNames
2324
{
@@ -38,10 +39,15 @@ public function refactor(Directory $directory)
3839

3940
$renames = new Set();
4041
$occurances = array();
42+
$noImportedUsages = new NoImportedUsagesFilter();
4143

4244
foreach ($phpFiles as $phpFile) {
4345
$classes = $this->codeAnalysis->findClasses($phpFile);
44-
$occurances = array_merge($this->nameScanner->findNames($phpFile), $occurances);
46+
47+
$occurances = array_merge(
48+
$noImportedUsages->filter($this->nameScanner->findNames($phpFile)),
49+
$occurances
50+
);
4551

4652
if (count($classes) !== 1) {
4753
continue;

src/main/QafooLabs/Refactoring/Domain/Model/PhpName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function change(PhpName $from, PhpName $to)
106106
$newParts = self::stringToParts($to->fullyQualifiedName);
107107
$newParts = $this->adjustSize($from, $newParts);
108108

109-
if ($this->fullyQualifiedName === $this->relativeName) {
109+
if ($this->isFullyQualified()) {
110110
$relativeNewParts = $newParts;
111111
} else {
112112
$diff = ($this->type === self::TYPE_CLASS)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace QafooLabs\Refactoring\Domain\Model\PhpNames;
4+
5+
use QafooLabs\Refactoring\Domain\Model\PhpName;
6+
7+
/**
8+
* Filters a list of php occurances by all items expect the imported relative usages.
9+
*/
10+
class NoImportedUsagesFilter
11+
{
12+
/**
13+
* @param array<PhpNameOccurance>
14+
*
15+
* @return array<PhpNameOccurance>
16+
*/
17+
public function filter(array $phpNames)
18+
{
19+
$fileUseOccurances = array_flip(
20+
array_map(
21+
function ($useOccurance) {
22+
return $useOccurance->name()->fullyQualifiedName();
23+
},
24+
array_filter(
25+
$phpNames,
26+
function ($occurance) {
27+
return $occurance->name()->type() === PhpName::TYPE_USE;
28+
}
29+
)
30+
)
31+
);
32+
33+
return array_filter(
34+
$phpNames,
35+
function ($occurance) use ($fileUseOccurances) {
36+
return (
37+
$occurance->name()->type() !== PhpName::TYPE_USAGE ||
38+
!isset($fileUseOccurances[$occurance->name()->fullyQualifiedName()])
39+
);
40+
}
41+
);
42+
}
43+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace QafooLabs\Refactoring\Domain\Model\PhpNames;
4+
5+
use QafooLabs\Refactoring\Domain\Model\PhpName;
6+
use QafooLabs\Refactoring\Domain\Model\PhpNameOccurance;
7+
use QafooLabs\Refactoring\Domain\Model\File;
8+
9+
class NoImportedUsagesFilterTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @group GH-31
13+
* @test
14+
*/
15+
public function it_filters_imported_php_name_usages()
16+
{
17+
$file = new File('foo.php', 'code');
18+
$filter = new NoImportedUsagesFilter();
19+
$names = $filter->filter(array(
20+
new PhpNameOccurance(new PhpName('Foo\Bar', 'Foo\Bar', PhpName::TYPE_USE), $file, 12),
21+
new PhpNameOccurance(new PhpName('Foo\Bar', 'Bar', PhpName::TYPE_USAGE), $file, 12),
22+
));
23+
24+
$this->assertEquals(1, count($names));
25+
}
26+
}

0 commit comments

Comments
 (0)