11package fr .adrienbrault .idea .symfony2plugin .stubs .indexes .visitor ;
22
33import com .intellij .psi .PsiElement ;
4- import com .intellij .psi .PsiRecursiveElementWalkingVisitor ;
54import com .intellij .psi .util .PsiTreeUtil ;
6- import com .jetbrains .php .lang .psi .elements .ArrayCreationExpression ;
7- import com .jetbrains .php .lang .psi .elements .ArrayHashElement ;
8- import com .jetbrains .php .lang .psi .elements .PhpReturn ;
9- import com .jetbrains .php .lang .psi .elements .StringLiteralExpression ;
5+ import com .jetbrains .php .codeInsight .controlFlow .PhpControlFlowUtil ;
6+ import com .jetbrains .php .codeInsight .controlFlow .PhpInstructionProcessor ;
7+ import com .jetbrains .php .codeInsight .controlFlow .instructions .PhpReturnInstruction ;
8+ import com .jetbrains .php .lang .psi .PhpFile ;
9+ import com .jetbrains .php .lang .psi .elements .*;
1010import kotlin .Pair ;
1111import org .apache .commons .lang .StringUtils ;
1212import org .jetbrains .annotations .NotNull ;
1616import java .util .function .Consumer ;
1717
1818/**
19+ * "return ['key' => 'value1', 'key2' => 'value1']"
20+ *
1921 * @author Daniel Espendiller <daniel@espendiller.net>
2022 */
21- public class ArrayReturnPsiRecursiveVisitor extends PsiRecursiveElementWalkingVisitor {
23+ public class TranslationArrayReturnVisitor {
2224
23- @ NotNull
24- private final Consumer <Pair <String , PsiElement >> arrayKeyVisitor ;
25+ public static void visitPhpReturn (@ NotNull PhpFile phpFile , @ NotNull Consumer <Pair <String , PsiElement >> arrayKeyVisitor ) {
2526
26- public ArrayReturnPsiRecursiveVisitor (@ NotNull Consumer <Pair <String , PsiElement >> arrayKeyVisitor ) {
27- this .arrayKeyVisitor = arrayKeyVisitor ;
28- }
27+ PhpInstructionProcessor processor = new PhpInstructionProcessor () {
28+ @ Override
29+ public boolean processReturnInstruction (PhpReturnInstruction instruction ) {
30+ PsiElement argument = instruction .getArgument ();
31+ if (argument instanceof ArrayCreationExpression arrayCreationExpression ) {
32+ collectConfigKeys (arrayCreationExpression , arrayKeyVisitor );
33+ }
2934
30- @ Override
31- public void visitElement (@ NotNull PsiElement element ) {
32- if (element instanceof PhpReturn ) {
33- visitPhpReturn ((PhpReturn ) element );
34- }
35+ return super .processReturnInstruction (instruction );
36+ }
37+ };
3538
36- super . visitElement ( element );
37- }
39+ // toplevel "returns"
40+ PhpControlFlowUtil . processFlow ( phpFile . getControlFlow (), processor );
3841
39- public void visitPhpReturn (PhpReturn phpReturn ) {
40- PsiElement arrayCreation = phpReturn .getFirstPsiChild ();
41- if (arrayCreation instanceof ArrayCreationExpression ) {
42- collectConfigKeys ((ArrayCreationExpression ) arrayCreation , this .arrayKeyVisitor );
42+ // toplevel "return" with namespace in file (for imports)
43+ for (PhpNamedElement value : phpFile .getTopLevelDefs ().values ()) {
44+ if (value instanceof PhpNamespace phpNamespace ) {
45+ PhpControlFlowUtil .processFlow (phpNamespace .getControlFlow (), processor );
46+ }
4347 }
4448 }
4549
@@ -48,9 +52,7 @@ public static void collectConfigKeys(@NotNull ArrayCreationExpression creationEx
4852 }
4953
5054 public static void collectConfigKeys (@ NotNull ArrayCreationExpression creationExpression , @ NotNull Consumer <Pair <String , PsiElement >> arrayKeyVisitor , @ NotNull List <String > context ) {
51-
5255 for (ArrayHashElement hashElement : PsiTreeUtil .getChildrenOfTypeAsList (creationExpression , ArrayHashElement .class )) {
53-
5456 PsiElement arrayKey = hashElement .getKey ();
5557 PsiElement arrayValue = hashElement .getValue ();
5658
0 commit comments