Skip to content

Commit

Permalink
Merge pull request #2234 from Haehnchen/feature/2077-string-attribute…
Browse files Browse the repository at this point in the history
…-targetEntity

#2077 support resolving "targetEntity" as string for Doctrine attribute metadata
  • Loading branch information
Haehnchen authored Oct 3, 2023
2 parents ec3f2aa + 3914bdf commit d4be5ce
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.jetbrains.php.lang.psi.elements.Field;
import com.jetbrains.php.lang.psi.elements.PhpAttribute;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.stubs.indexes.expectedArguments.PhpExpectedFunctionArgument;
import com.jetbrains.php.lang.psi.stubs.indexes.expectedArguments.PhpExpectedFunctionClassConstantArgument;
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineModelField;
import fr.adrienbrault.idea.symfony2plugin.doctrine.metadata.dict.DoctrineMetadataModel;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
Expand Down Expand Up @@ -90,12 +88,11 @@ public DoctrineMetadataModel getMetadata(@NotNull DoctrineMappingDriverArguments
String substring = fqn.substring(fqn.lastIndexOf("\\") + 1);
doctrineModelField.setRelationType(substring);

PhpExpectedFunctionArgument argument = PhpElementsUtil.findAttributeArgumentByName("targetEntity", attribute);
if (argument instanceof PhpExpectedFunctionClassConstantArgument) {
String repositoryClassRaw = ((PhpExpectedFunctionClassConstantArgument) argument).getClassFqn();
if (StringUtils.isNotBlank(repositoryClassRaw)) {
doctrineModelField.setRelation(repositoryClassRaw);
}
// not resolving same entity namespace prefix: EntityHelper.resolveDoctrineLikePropertyClass
// possible not a wide range usages for attributes
String targetEntity = PhpElementsUtil.getAttributeArgumentStringByName(attribute, "targetEntity");
if (StringUtils.isNotBlank(targetEntity)) {
doctrineModelField.setRelation("\\" + StringUtils.stripStart(targetEntity, "\\"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,7 @@ public static PhpExpectedFunctionArgument findAttributeArgumentByName(@NotNull S
return null;
}

@Nullable
public static String getAttributeArgumentStringByName(@NotNull PhpAttribute phpAttribute, @NotNull String attributeName) {
for (PhpAttribute.PhpAttributeArgument argument : phpAttribute.getArguments()) {
if (!attributeName.equals(argument.getName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public void testPhpAttributesMetadata() {

assertEquals("\\Doctrine\\Orm\\MyTrait\\Egg", metadata.getField("appleTrait").getRelation());
assertEquals("ManyToOne", metadata.getField("appleTrait").getRelationType());

assertEquals("\\ORM\\Foobar\\Egg", metadata.getField("eggClassString").getRelation());
assertEquals("ManyToMany", metadata.getField("eggClassString").getRelationType());

assertEquals("\\ORM\\Foobar\\Egg", metadata.getField("eggClassString").getRelation());
assertEquals("ManyToMany", metadata.getField("eggClassString").getRelationType());
}

private DoctrineMetadataModel createOrmMetadata() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,11 @@ class AttributeEntity {

#[ORM\ManyToMany(targetEntity:Egg::class)]
public $egg;

#[ORM\ManyToMany(targetEntity: '\ORM\Foobar\Egg')]
public $eggClassString;

#[ORM\ManyToMany(targetEntity: 'ORM\Foobar\Egg')]
public $eggClassStringBackslashless;
};
}

0 comments on commit d4be5ce

Please sign in to comment.