Skip to content

Multiuse patch #500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,33 @@ public function addUseStatementIfNecessary(string $class): string
$targetIndex = $index;
}

$lastUseStmtIndex = $index;
} elseif ($stmt instanceof Node\Stmt\GroupUse) {
// cezar77: this if-case should handle group use statements
// TO-DO: it is repeating a lot of parts of the previous if-statement
// to be refactored later
foreach ($stmt->uses as $use) {
$alias = $use->alias ? $use->alias->name : $use->name->getLast();

$fqcn = $stmt->prefix.'\\'.$use->name;
if ($class === $fqcn) {
return $alias;
}

if ($alias === $shortClassName) {
// we have a conflicting alias!
// to be safe, use the fully-qualified class name
// everywhere and do not add another use statement
return '\\'.$class;
}
}

// if $class is alphabetically before this use statement, place it before
// only set $targetIndex the first time you find it
if (null === $targetIndex && Str::areClassesAlphabetical($class, (string) $stmt->prefix)) {
$targetIndex = $index;
}

$lastUseStmtIndex = $index;
} elseif ($stmt instanceof Node\Stmt\Class_) {
if (null !== $targetIndex) {
Expand Down
12 changes: 12 additions & 0 deletions tests/Util/ClassSourceManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,18 @@ public function getAddOneToManyRelationTests()
->setOrphanRemoval(false),
];

// interesting also becaue the source file has
// multiple use statements
yield 'one_to_many_simple_multiple_use' => [
'User_with_multiple_use_statements.php',
'User_with_multiple_use_statements.php',
(new RelationOneToMany())
->setPropertyName('avatarPhotos')
->setTargetClassName('App\Entity\UserAvatarPhoto')
->setTargetPropertyName('user')
->setOrphanRemoval(false),
];

yield 'one_to_many_orphan_removal' => [
'User_simple.php',
'User_simple_orphan_removal.php',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\{
ArrayCollection,
Collection
};
use Some\Other\{
UserProfile,
FooCategory as Category
};

/**
* @ORM\Entity()
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\OneToMany(targetEntity="App\Entity\UserAvatarPhoto", mappedBy="user")
*/
private $avatarPhotos;

public function __construct()
{
$this->avatarPhotos = new ArrayCollection();
}

public function getId(): ?int
{
return $this->id;
}

/**
* @return Collection|UserAvatarPhoto[]
*/
public function getAvatarPhotos(): Collection
{
return $this->avatarPhotos;
}

public function addAvatarPhoto(UserAvatarPhoto $avatarPhoto): self
{
if (!$this->avatarPhotos->contains($avatarPhoto)) {
$this->avatarPhotos[] = $avatarPhoto;
$avatarPhoto->setUser($this);
}

return $this;
}

public function removeAvatarPhoto(UserAvatarPhoto $avatarPhoto): self
{
if ($this->avatarPhotos->contains($avatarPhoto)) {
$this->avatarPhotos->removeElement($avatarPhoto);
// set the owning side to null (unless already changed)
if ($avatarPhoto->getUser() === $this) {
$avatarPhoto->setUser(null);
}
}

return $this;
}
}
31 changes: 31 additions & 0 deletions tests/Util/fixtures/source/User_with_multiple_use_statements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\{
ArrayCollection,
Collection
};
use Some\Other\{
UserProfile,
FooCategory as Category
};

/**
* @ORM\Entity()
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

public function getId(): ?int
{
return $this->id;
}
}