Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
import com.jetbrains.php.lang.psi.PhpFile;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
import com.jetbrains.php.lang.psi.stubs.indexes.expectedArguments.PhpExpectedFunctionArgument;
import com.jetbrains.php.lang.psi.stubs.indexes.expectedArguments.PhpExpectedFunctionClassConstantArgument;
import de.espend.idea.php.annotation.util.AnnotationUtil;
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.visitor.AnnotationElementWalkingVisitor;
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.visitor.AttributeElementWalkingVisitor;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PhpPsiAttributesUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
import org.apache.commons.lang.ArrayUtils;
Expand Down Expand Up @@ -141,15 +139,7 @@ public static Collection<Pair<String, String>> getClassRepositoryPair(@NotNull P
// Attributes:
// #[Entity(repositoryClass: UserRepository::class)]
phpFile.acceptChildren(new AttributeElementWalkingVisitor(pair -> {
String repositoryClass = null;

PhpExpectedFunctionArgument argument = PhpElementsUtil.findAttributeArgumentByName("repositoryClass", pair.getFirst());
if (argument instanceof PhpExpectedFunctionClassConstantArgument) {
String repositoryClassRaw = ((PhpExpectedFunctionClassConstantArgument) argument).getClassFqn();
if (StringUtils.isNotBlank(repositoryClassRaw)) {
repositoryClass = repositoryClassRaw;
}
}
String repositoryClass = PhpPsiAttributesUtil.getAttributeValueByNameAsString(pair.getFirst(), "repositoryClass");

pairs.add(Pair.create(
StringUtils.stripStart(pair.getSecond().getFQN(), "\\"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,29 +180,36 @@ private static PsiElementPattern.Capture<PsiElement> getAttributeColonPattern(St
@Nullable
private static String resolveLocalValue(@NotNull PhpAttribute attribute, @NotNull ClassConstantReference nextSibling) {
PhpExpression classReference = nextSibling.getClassReference();
if (classReference != null) {
if (classReference instanceof ClassReference) {
String name = classReference.getName();
if (name != null && (name.equals("self") || name.equals("static"))) {
PsiElement phpAttributesList = attribute.getParent();
if (phpAttributesList instanceof PhpAttributesList) {
PsiElement method = phpAttributesList.getParent();
if (method instanceof Method) {
PsiElement phpClass = method.getParent();
if (phpClass instanceof PhpClass) {
String fieldName = nextSibling.getName();
Field ownFieldByName = ((PhpClass) phpClass).findOwnFieldByName(fieldName, true);
if (ownFieldByName != null) {
PsiElement defaultValue = ownFieldByName.getDefaultValue();
if (defaultValue instanceof StringLiteralExpression) {
String contents = ((StringLiteralExpression) defaultValue).getContents();
if (StringUtils.isNotBlank(contents)) {
return contents;
}

PsiElement phpClass = method instanceof Method ? ((Method) method).getContainingClass() : method;
if (phpClass instanceof PhpClass) {
String fieldName = nextSibling.getName();
Field ownFieldByName = ((PhpClass) phpClass).findOwnFieldByName(fieldName, true);
if (ownFieldByName != null) {
PsiElement defaultValue = ownFieldByName.getDefaultValue();
if (defaultValue instanceof StringLiteralExpression) {
String contents = ((StringLiteralExpression) defaultValue).getContents();
if (StringUtils.isNotBlank(contents)) {
return contents;
}
}
}
}
}
} else {
String s = nextSibling.getText().toLowerCase();
if (s.endsWith("::class")) {
String fqn = ((ClassReference) classReference).getFQN();
if (StringUtils.isNotBlank(fqn)) {
return fqn;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public void testPhpOrmAnnotationMetadata() {
assertIndexContainsKeyWithValue(DoctrineMetadataFileStubIndex.KEY, "Doctrine\\Orm\\Annotation", new IndexValueRepositoryClassEquals("Doctrine\\Orm\\Foo"));
}

public void testPhpOrmAttributeMetadata() {
assertIndexContains(DoctrineMetadataFileStubIndex.KEY, "Doctrine\\Orm\\AttributeEntity");
assertIndexContainsKeyWithValue(DoctrineMetadataFileStubIndex.KEY, "Doctrine\\Orm\\AttributeEntity", new IndexValueRepositoryClassEquals("Doctrine\\OrmRepository\\AttributeEntityRepository"));
}

/**
* @see fr.adrienbrault.idea.symfony2plugin.doctrine.DoctrineUtil#getClassRepositoryPair
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ class Document {};
namespace Doctrine\Orm {

use Doctrine\ORM\Mapping AS ORM;
use Doctrine\OrmRepository\AttributeEntityRepository;

/**
* @ORM\Entity(repositoryClass="Foo")
*/
class Annotation {};
class Annotation {}

#[ORM\Entity(repositoryClass: AttributeEntityRepository::class)]
class AttributeEntity {}
}

namespace Doctrine\OrmRepository {
class AttributeEntityRepository {};
}

namespace Doctrine\Flow\Orm {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package fr.adrienbrault.idea.symfony2plugin.tests.util;

import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
import com.jetbrains.php.lang.psi.elements.PhpAttribute;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
import fr.adrienbrault.idea.symfony2plugin.util.PhpPsiAttributesUtil;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class PhpPsiAttributesUtilTest extends SymfonyLightCodeInsightFixtureTestCase {
public void testGetAttributeValueByNameAsStringForDirectResolve() {
PhpAttribute phpAttribute1 = PhpPsiElementFactory.createFromText(getProject(), PhpAttribute.class, "<?php\n" +
"#[Foobar(test: 'foobar')]" +
"class Foo {}"
);

assertEquals("foobar", PhpPsiAttributesUtil.getAttributeValueByNameAsString(phpAttribute1, "test"));

PhpAttribute phpAttribute2 = PhpPsiElementFactory.createFromText(getProject(), PhpAttribute.class, "<?php\n" +
"class Foobar {}" +
"\n" +
"#[Foobar(test: Foobar::class)]\n" +
"class Foo {}"
);

assertEquals("\\Foobar", PhpPsiAttributesUtil.getAttributeValueByNameAsString(phpAttribute2, "test"));
}

public void testGetAttributeValueByNameAsStringForLocalResolve() {
PhpAttribute phpAttribute1 = PhpPsiElementFactory.createFromText(getProject(), PhpAttribute.class, "<?php\n" +
"\n" +
"#[Foobar(test: self::FOO)]\n" +
"class Foo {\n" +
" const FOO = 'test2';" +
"}"
);

assertEquals("test2", PhpPsiAttributesUtil.getAttributeValueByNameAsString(phpAttribute1, "test"));

PhpAttribute phpAttribute2 = PhpPsiElementFactory.createFromText(getProject(), PhpAttribute.class, "<?php\n" +
"\n" +
"class Foo {\n" +
" const FOO = 'test2';\n" +
"\n" +
"#[Foobar(test: self::FOO)]\n" +
" public function foo() {}\n" +
"}"
);

assertEquals("test2", PhpPsiAttributesUtil.getAttributeValueByNameAsString(phpAttribute2, "test"));

PhpAttribute phpAttribute3 = PhpPsiElementFactory.createFromText(getProject(), PhpAttribute.class, "<?php\n" +
"\n" +
"class Foo {\n" +
" const FOO = 'test2';\n" +
"\n" +
"#[Foobar(test: static::FOO)]\n" +
" public function foo() {}\n" +
"}"
);

assertEquals("test2", PhpPsiAttributesUtil.getAttributeValueByNameAsString(phpAttribute3, "test"));
}

public void testResolveForNoLocalValue() {
PhpAttribute phpAttribute = PhpPsiElementFactory.createFromText(getProject(), PhpAttribute.class, "<?php\n" +
"class FooBar {\n" +
" const BAR = 'test2';\n" +
"}\n" +
"\n" +
"class Foo {\n" +
" const FOO = 'test2';\n" +
"\n" +
"#[Foobar(test: FooBar::BAR)]\n" +
" public function foo() {}\n" +
"}"
);

assertNull(PhpPsiAttributesUtil.getAttributeValueByNameAsString(phpAttribute, "test"));
}
}