@@ -1458,4 +1458,84 @@ public static Set<Variable> visit(@NotNull PsiElement scope, @NotNull String nam
1458
1458
return visitor .variables ;
1459
1459
}
1460
1460
}
1461
+
1462
+
1463
+ /**
1464
+ * Find string values based on a given PsiElement and its references
1465
+ *
1466
+ * - render(true === true ? 'foo.twig.html' : 'foobar.twig.html')
1467
+ * - foo(self::foo), foo($var), foo($this->foo), ...
1468
+ * - render($foo ?? 'foo.twig.html')
1469
+ */
1470
+ public static class StringResolver {
1471
+ public static Collection <String > findStringValues (@ NotNull PsiElement psiElement ) {
1472
+ Collection <String > strings = new HashSet <>();
1473
+
1474
+ if (psiElement instanceof StringLiteralExpression ) {
1475
+ strings .add (resolveString ((StringLiteralExpression ) psiElement ));
1476
+ } else if (psiElement instanceof TernaryExpression ) {
1477
+ // render(true === true ? 'foo.twig.html' : 'foobar.twig.html')
1478
+ for (PhpPsiElement phpPsiElement : new PhpPsiElement []{((TernaryExpression ) psiElement ).getTrueVariant (), ((TernaryExpression ) psiElement ).getFalseVariant ()}) {
1479
+ if (phpPsiElement == null ) {
1480
+ continue ;
1481
+ }
1482
+
1483
+ if (phpPsiElement instanceof StringLiteralExpression ) {
1484
+ strings .add (resolveString ((StringLiteralExpression ) phpPsiElement ));
1485
+ } else if (phpPsiElement instanceof PhpReference ) {
1486
+ strings .add (resolvePhpReference ((PhpReference ) phpPsiElement ));
1487
+ }
1488
+ }
1489
+ } else if (psiElement instanceof PhpReference ) {
1490
+ // foo(self::foo)
1491
+ // foo($this->foo)
1492
+ // foo($var)
1493
+ strings .add (resolvePhpReference ((PhpReference ) psiElement ));
1494
+ } else if (psiElement instanceof BinaryExpression ) {
1495
+ // render($foo ?? 'foo.twig.html')
1496
+ PsiElement phpPsiElement = ((BinaryExpression ) psiElement ).getRightOperand ();
1497
+
1498
+ if (phpPsiElement instanceof StringLiteralExpression ) {
1499
+ strings .add (resolveString ((StringLiteralExpression ) phpPsiElement ));
1500
+ } else if (phpPsiElement instanceof PhpReference ) {
1501
+ strings .add (resolvePhpReference ((PhpReference ) phpPsiElement ));
1502
+ }
1503
+ }
1504
+
1505
+ return strings ;
1506
+ }
1507
+
1508
+ @ Nullable
1509
+ private static String resolveString (@ NotNull StringLiteralExpression parameter ) {
1510
+ String contents = parameter .getContents ();
1511
+ return StringUtils .isBlank (contents ) ? null : contents ;
1512
+ }
1513
+
1514
+ @ Nullable
1515
+ private static String resolvePhpReference (@ NotNull PhpReference parameter ) {
1516
+ for (PhpNamedElement phpNamedElement : ((PhpReference ) parameter ).resolveLocal ()) {
1517
+ // foo(self::foo)
1518
+ // foo($this->foo)
1519
+ if (phpNamedElement instanceof Field ) {
1520
+ PsiElement defaultValue = ((Field ) phpNamedElement ).getDefaultValue ();
1521
+ if (defaultValue instanceof StringLiteralExpression ) {
1522
+ return resolveString ((StringLiteralExpression ) defaultValue );
1523
+ }
1524
+ }
1525
+
1526
+ // foo($var) => $var = 'test.html.twig'
1527
+ if (phpNamedElement instanceof Variable ) {
1528
+ PsiElement assignmentExpression = phpNamedElement .getParent ();
1529
+ if (assignmentExpression instanceof AssignmentExpression ) {
1530
+ PhpPsiElement value = ((AssignmentExpression ) assignmentExpression ).getValue ();
1531
+ if (value instanceof StringLiteralExpression ) {
1532
+ return resolveString ((StringLiteralExpression ) value );
1533
+ }
1534
+ }
1535
+ }
1536
+ }
1537
+
1538
+ return null ;
1539
+ }
1540
+ }
1461
1541
}
0 commit comments