Skip to content

Commit

Permalink
#2260 support "targetEntity" extraction from property type for Doctri…
Browse files Browse the repository at this point in the history
…ne relations
  • Loading branch information
Haehnchen committed Apr 17, 2024
1 parent 7a38bc3 commit 2938134
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.intellij.psi.PsiFile;
import com.jetbrains.php.lang.psi.PhpFile;
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.elements.*;
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 @@ -93,6 +91,22 @@ public DoctrineMetadataModel getMetadata(@NotNull DoctrineMappingDriverArguments
String targetEntity = PhpElementsUtil.getAttributeArgumentStringByName(attribute, "targetEntity");
if (StringUtils.isNotBlank(targetEntity)) {
doctrineModelField.setRelation("\\" + StringUtils.stripStart(targetEntity, "\\"));
} else {
// #[ORM\ManyToOne]
// private ?MyBike $myBike;
PhpTypeDeclaration typeDeclaration = field.getTypeDeclaration();
if (typeDeclaration != null) {
Collection<ClassReference> classReferences = typeDeclaration.getClassReferences().stream()
.filter(classReference -> !"null".equals(classReference.getCanonicalText()))
.toList();

if (!classReferences.isEmpty()) {
String fqnClass = classReferences.iterator().next().getFQN();
if (fqnClass != null) {
doctrineModelField.setRelation(fqnClass);
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public void testPhpAttributesMetadata() {

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

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

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

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

#[ORM\ManyToMany]
public null|Egg $eggTargetEntity;
};
}

0 comments on commit 2938134

Please sign in to comment.