Skip to content

Commit 4bde1dc

Browse files
committed
With --overwrite, put updated methods in same location as original
This just makes for a cleaner diff, and maintains the order the user seems to have originally wanted
1 parent ed15a2b commit 4bde1dc

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

src/Util/ClassSourceManipulator.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ private function addMethod(Node\Stmt\ClassMethod $methodNode)
722722
{
723723
$classNode = $this->getClassNode();
724724
$methodName = $methodNode->name;
725+
$existingIndex = null;
725726
if ($this->methodExists($methodName)) {
726727
if (!$this->overwrite) {
727728
$this->writeNote(sprintf(
@@ -733,17 +734,33 @@ private function addMethod(Node\Stmt\ClassMethod $methodNode)
733734
return;
734735
}
735736

736-
// we are overwriting - remove the existing method
737-
unset($classNode->stmts[$this->getMethodIndex($methodName)]);
738-
$classNode->stmts = array_values($classNode->stmts);
737+
// record, so we can overwrite in the same place
738+
$existingIndex = $this->getMethodIndex($methodName);
739739
}
740740

741+
742+
$newStatements = [];
743+
741744
// put new method always at the bottom
742745
if (!empty($classNode->stmts)) {
743-
$classNode->stmts[] = $this->createBlankLineNode(self::CONTEXT_CLASS);
746+
$newStatements[] = $this->createBlankLineNode(self::CONTEXT_CLASS);
747+
}
748+
749+
$newStatements[] = $methodNode;
750+
751+
if (null === $existingIndex) {
752+
// just them on the end!
753+
754+
$classNode->stmts = array_merge($classNode->stmts, $newStatements);
755+
} else {
756+
array_splice(
757+
$classNode->stmts,
758+
$existingIndex,
759+
1,
760+
$newStatements
761+
);
744762
}
745763

746-
$classNode->stmts[] = $methodNode;
747764
$this->updateSourceCodeFromNewStmts();
748765
}
749766

tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public function getId(): ?int
4545
return $this->id;
4646
}
4747

48+
public function customMethod()
49+
{
50+
return '';
51+
}
52+
4853
public function setUserProfile(?UserProfile $userProfile)
4954
{
5055
$this->userProfile = $userProfile;

tests/Doctrine/fixtures/expected_overwrite/src/Entity/User.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ public function getId(): ?int
4444
return $this->id;
4545
}
4646

47+
public function customMethod()
48+
{
49+
return '';
50+
}
51+
52+
public function setUserProfile(?UserProfile $userProfile)
53+
{
54+
$this->userProfile = $userProfile;
55+
56+
// set (or unset) the owning side of the relation if necessary
57+
$newUser = $userProfile === null ? null : $this;
58+
if ($newUser !== $userProfile->getUser()) {
59+
$userProfile->setUser($newUser);
60+
}
61+
}
62+
4763
/**
4864
* @return Collection|UserAvatar[]
4965
*/
@@ -80,17 +96,6 @@ public function getUserProfile(): ?UserProfile
8096
return $this->userProfile;
8197
}
8298

83-
public function setUserProfile(?UserProfile $userProfile)
84-
{
85-
$this->userProfile = $userProfile;
86-
87-
// set (or unset) the owning side of the relation if necessary
88-
$newUser = $userProfile === null ? null : $this;
89-
if ($newUser !== $userProfile->getUser()) {
90-
$userProfile->setUser($newUser);
91-
}
92-
}
93-
9499
/**
95100
* @return Collection|Tag[]
96101
*/

tests/Doctrine/fixtures/source_project/src/Entity/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function getId(): ?int
4343
return $this->id;
4444
}
4545

46+
public function customMethod()
47+
{
48+
return '';
49+
}
50+
4651
public function setUserProfile(?UserProfile $userProfile)
4752
{
4853
$this->userProfile = $userProfile;

0 commit comments

Comments
 (0)