|
12 | 12 | import com.intellij.psi.xml.XmlTag;
|
13 | 13 | import com.jetbrains.php.PhpIndex;
|
14 | 14 | import com.jetbrains.php.lang.parser.PhpElementTypes;
|
| 15 | +import com.jetbrains.php.lang.psi.PhpPsiUtil; |
15 | 16 | import com.jetbrains.php.lang.psi.elements.*;
|
16 | 17 | import com.jetbrains.php.lang.psi.elements.impl.PhpTypedElementImpl;
|
17 | 18 | import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
|
@@ -537,30 +538,70 @@ public static String getFormNameOfPhpClass(@NotNull PhpClass phpClass) {
|
537 | 538 | }
|
538 | 539 |
|
539 | 540 | /**
|
540 |
| - * Get getExtendedType as string |
| 541 | + * Get getExtendedType and getExtendedTypes (Symfony >= 4.2) as string |
541 | 542 | *
|
542 |
| - * 'Foo::class' and string 'foo' supported |
| 543 | + * "return Foo::class;" |
| 544 | + * "return 'foobar';" |
| 545 | + * "return [Foo::class, FooBar::class];" |
| 546 | + * "return true === true ? FileType::class : Form:class;" |
| 547 | + * "yield Foo::class;" |
543 | 548 | */
|
544 | 549 | @NotNull
|
545 | 550 | public static Collection<String> getFormExtendedType(@NotNull PhpClass phpClass) {
|
546 |
| - Method getParent = phpClass.findMethodByName(FormOptionsUtil.EXTENDED_TYPE_METHOD); |
547 |
| - if(getParent == null) { |
548 |
| - return Collections.emptySet(); |
549 |
| - } |
550 |
| - |
551 | 551 | Collection<String> types = new HashSet<>();
|
552 | 552 |
|
553 |
| - for (PhpReturn phpReturn : PsiTreeUtil.collectElementsOfType(getParent, PhpReturn.class)) { |
554 |
| - PhpPsiElement firstPsiChild = phpReturn.getFirstPsiChild(); |
| 553 | + // public function getExtendedType() { return FileType::class; } |
| 554 | + // public function getExtendedType() { return true === true ? FileType::class : Form:class; } |
| 555 | + Method extendedType = phpClass.findMethodByName("getExtendedType"); |
| 556 | + if(extendedType != null) { |
| 557 | + for (PhpReturn phpReturn : PsiTreeUtil.collectElementsOfType(extendedType, PhpReturn.class)) { |
| 558 | + PhpPsiElement firstPsiChild = phpReturn.getFirstPsiChild(); |
555 | 559 |
|
556 |
| - // true ? 'foo' : 'foo' |
557 |
| - if(firstPsiChild instanceof TernaryExpression) { |
558 |
| - types.addAll(PhpElementsUtil.getTernaryExpressionConditionStrings((TernaryExpression) firstPsiChild)); |
| 560 | + // true ? 'foo' : 'foo' |
| 561 | + if(firstPsiChild instanceof TernaryExpression) { |
| 562 | + types.addAll(PhpElementsUtil.getTernaryExpressionConditionStrings((TernaryExpression) firstPsiChild)); |
| 563 | + } |
| 564 | + |
| 565 | + String stringValue = PhpElementsUtil.getStringValue(firstPsiChild); |
| 566 | + if(stringValue != null) { |
| 567 | + types.add(stringValue); |
| 568 | + } |
| 569 | + } |
| 570 | + } |
| 571 | + |
| 572 | + // Symfony 4.2: Support improved form type extensions: |
| 573 | + // public static function getExtendedTypes(): iterable: |
| 574 | + // https://symfony.com/blog/new-in-symfony-4-2-improved-form-type-extensions |
| 575 | + Method extendedTypes = phpClass.findMethodByName("getExtendedTypes"); |
| 576 | + if (extendedTypes != null) { |
| 577 | + // [Foo::class, FooBar::class] |
| 578 | + for (PhpReturn phpReturn : PsiTreeUtil.collectElementsOfType(extendedTypes, PhpReturn.class)) { |
| 579 | + PhpPsiElement arrayCreationExpression = phpReturn.getFirstPsiChild(); |
| 580 | + if (arrayCreationExpression instanceof ArrayCreationExpression) { |
| 581 | + Collection<PsiElement> arrayValues = PhpPsiUtil.getChildren(arrayCreationExpression, psiElement -> |
| 582 | + psiElement.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE |
| 583 | + ); |
| 584 | + |
| 585 | + for (PsiElement child : arrayValues) { |
| 586 | + String stringValue = PhpElementsUtil.getStringValue(child.getFirstChild()); |
| 587 | + if (stringValue != null) { |
| 588 | + types.add(stringValue); |
| 589 | + } |
| 590 | + } |
| 591 | + } |
559 | 592 | }
|
560 | 593 |
|
561 |
| - String stringValue = PhpElementsUtil.getStringValue(firstPsiChild); |
562 |
| - if(stringValue != null) { |
563 |
| - types.add(stringValue); |
| 594 | + // yield Foo::class |
| 595 | + for (PhpYield phpReturn : PsiTreeUtil.collectElementsOfType(extendedTypes, PhpYield.class)) { |
| 596 | + PsiElement yieldArgument = phpReturn.getArgument(); |
| 597 | + if (yieldArgument == null) { |
| 598 | + continue; |
| 599 | + } |
| 600 | + |
| 601 | + String stringValue = PhpElementsUtil.getStringValue(yieldArgument); |
| 602 | + if (stringValue != null) { |
| 603 | + types.add(stringValue); |
| 604 | + } |
564 | 605 | }
|
565 | 606 | }
|
566 | 607 |
|
|
0 commit comments