8000 supporting class constants for form getParent · Ma27/idea-php-symfony2-plugin@cb422db · GitHub
[go: up one dir, main page]

Skip to content

Commit cb422db

Browse files
committed
supporting class constants for form getParent
1 parent f023e75 commit cb422db

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

src/fr/adrienbrault/idea/symfony2plugin/form/util/FormUtil.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,7 @@ public static PhpClass getFormTypeClassOnParameter(@NotNull PsiElement psiElemen
188188
}
189189

190190
if(psiElement instanceof ClassConstantReference) {
191-
PhpExpression classReference = ((ClassConstantReference) psiElement).getClassReference();
192-
if(classReference instanceof PhpReference) {
193-
String typeName = ((PhpReference) classReference).getFQN();
194-
if(typeName != null && StringUtils.isNotBlank(typeName)) {
195-
return PhpElementsUtil.getClassInterface(psiElement.getProject(), typeName);
196-
}
197-
}
191+
return PhpElementsUtil.getClassConstantPhpClass((ClassConstantReference) psiElement);
198192
}
199193

200194
return null;
@@ -506,6 +500,11 @@ public static String getFormParentOfPhpClass(@NotNull PhpClass phpClass) {
506500
continue;
507501
}
508502

503+
// Foo::class
504+
if(firstPsiChild instanceof ClassConstantReference) {
505+
return PhpElementsUtil.getClassConstantPhpFqn((ClassConstantReference) firstPsiChild);
506+
}
507+
509508
if(!(firstPsiChild instanceof BinaryExpression) || !PsiElementAssertUtil.isNotNullAndIsElementType(firstPsiChild, PhpElementTypes.CONCATENATION_EXPRESSION)) {
510509
continue;
511510
}
@@ -531,4 +530,22 @@ public static String getFormParentOfPhpClass(@NotNull PhpClass phpClass) {
531530

532531
return null;
533532
}
533+
534+
/**
535+
* Finds form name by "getName" method
536+
*
537+
* Symfony < 2.8
538+
* 'foo_bar'
539+
*
540+
* Symfony 2.8
541+
* "$this->getName()" -> "$this->getBlockPrefix()" -> return 'datetime';
542+
*
543+
* Symfony 3.0
544+
* "UserProfileType" => "user_profile"
545+
*
546+
*/
547+
@Nullable
548+
public static String getFormNameOfPhpClass(@NotNull PhpClass phpClass) {
549+
return null;
550+
}
534551
}

src/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,4 +1318,25 @@ public static MethodReference findMethodReferenceOnClassConstant(PsiElement psiE
13181318

13191319
return null;
13201320
}
1321+
1322+
/**
1323+
* Foo::class to its PhpClass
1324+
*/
1325+
public static PhpClass getClassConstantPhpClass(@NotNull ClassConstantReference classConstant) {
1326+
String typeName = getClassConstantPhpFqn(classConstant);
1327+
return typeName != null ? PhpElementsUtil.getClassInterface(classConstant.getProject(), typeName) : null;
1328+
}
1329+
1330+
/**
1331+
* Foo::class to its class fqn include namespace
1332+
*/
1333+
public static String getClassConstantPhpFqn(@NotNull ClassConstantReference classConstant) {
1334+
PhpExpression classReference = classConstant.getClassReference();
1335+
if(!(classReference instanceof PhpReference)) {
1336+
return null;
1337+
}
1338+
1339+
String typeName = ((PhpReference) classReference).getFQN();
1340+
return typeName != null && StringUtils.isNotBlank(typeName) ? StringUtils.stripStart(typeName, "\\") : null;
1341+
}
13211342
}

tests/fr/adrienbrault/idea/symfony2plugin/tests/form/util/FormUtilTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ public void testGetFormParentOfPhpClass() {
109109
);
110110

111111
assertEquals("My\\Bar\\Foo", FormUtil.getFormParentOfPhpClass(phpClass));
112+
113+
phpClass = PhpPsiElementFactory.createPhpPsiFromText(getProject(), PhpClass.class, "<?php\n" +
114+
"namespace My\\Bar {\n" +
115+
" class Bar() {}\n" +
116+
" class Foo {\n" +
117+
" public function getParent()" +
118+
" {\n" +
119+
" return Bar::class';\n" +
120+
" }\n" +
121+
" }\n" +
122+
"}"
123+
);
124+
125+
assertEquals("My\\Bar\\Bar", FormUtil.getFormParentOfPhpClass(phpClass));
112126
}
113127

114128
public void testGetFormParentOfPhpClassShouldOnlyUseOwnMethod() {

0 commit comments

Comments
 (0)
0