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

Commit 0b9a950

Browse files
committed
[GH-28] Finally fix all the known nasty issues with renaming (hopefully), closes GH-28
1 parent 746cfec commit 0b9a950

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,17 @@ private function overlaps(PhpName $other)
8585
return false;
8686
}
8787

88+
private function shareNamespace(PhpName $other)
89+
{
90+
$otherName = array();
91+
$otherParts = $this->stringToParts($other->fullyQualifiedName);
92+
93+
return strpos($this->fullyQualifiedName, $otherParts[0]) !== false;
94+
}
95+
8896
public function change(PhpName $from, PhpName $to)
8997
{
90-
if ( ! $this->isAffectedByChangesTo($from)) {
98+
if ( ! $this->isAffectedByChangesTo($from) && ! $this->shareNamespace($from)) {
9199
return $this;
92100
}
93101

@@ -101,12 +109,18 @@ public function change(PhpName $from, PhpName $to)
101109
if ($this->fullyQualifiedName === $this->relativeName) {
102110
$relativeNewParts = $newParts;
103111
} else {
104-
$relativeNewParts = array_slice($newParts, -1 * count(explode('\\', $this->relativeName)));
112+
$diff = count($newParts) - $this->numParts();
113+
$relativeNewParts = array_slice($newParts, -1 * (count(explode('\\', $this->relativeName))+$diff));
105114
}
106115

107116
return new PhpName(self::partsToString($newParts), self::partsToString($relativeNewParts), $this->type);
108117
}
109118

119+
private function numParts()
120+
{
121+
return substr_count($this->fullyQualifiedName, '\\') + 1;
122+
}
123+
110124
private function adjustSize($from, $newParts)
111125
{
112126
$fromParts = self::stringToParts($from->fullyQualifiedName);

src/test/QafooLabs/Refactoring/Domain/Model/PhpNameTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function testRegression5()
109109
$changed = $name->change($from, $to);
110110

111111
$this->assertEquals('Qafoo\ChangeTrack\Analyzer\ChangeFeed', $changed->fullyQualifiedName());
112-
$this->assertEQuals('ChangeFeed', $changed->relativeName());
112+
$this->assertEQuals('Analyzer\ChangeFeed', $changed->relativeName());
113113
}
114114

115115
public function testRegression6()
@@ -158,4 +158,17 @@ public function testChangeKeepsType()
158158

159159
$this->assertEquals(PhpName::TYPE_NAMESPACE, $changed->type());
160160
}
161+
162+
public function testAddRelativeNameWhenNamespaceExpands()
163+
{
164+
$from = new PhpName('Foo', 'Foo');
165+
$to = new PhpName('Foo\Bar', 'Foo\Bar');
166+
167+
$name = new PhpName('Foo\Foo', 'Foo');
168+
$changed = $name->change($from, $to);
169+
170+
$this->assertFalse($name->isAffectedByChangesTo($from));
171+
$this->assertEquals('Foo\Bar\Foo', $changed->fullyQualifiedName());
172+
$this->assertEquals('Bar\Foo', $changed->relativeName());
173+
}
161174
}

0 commit comments

Comments
 (0)