66use Elegantly \Translator \Caches \SearchCodeCache ;
77use Illuminate \Contracts \Filesystem \Filesystem ;
88use Illuminate \Support \Facades \Blade ;
9+ use Illuminate \Support \Facades \Lang ;
910use PhpParser \Node ;
1011use PhpParser \Node \Expr \FuncCall ;
12+ use PhpParser \Node \Expr \MethodCall ;
13+ use PhpParser \Node \Expr \StaticCall ;
1114use PhpParser \Node \Name ;
1215use PhpParser \Node \Scalar \String_ ;
1316use PhpParser \NodeFinder ;
@@ -50,6 +53,32 @@ public function finder(): Finder
5053 ->files ();
5154 }
5255
56+ public static function isFunCallTo (
57+ FuncCall $ node ,
58+ string $ function ,
59+ string $ argName ,
60+ int $ argPosition ,
61+ string $ argValue
62+ ) {
63+ if ($ node ->name instanceof Name && $ node ->name ->name !== $ function ) {
64+ return false ;
65+ }
66+
67+ $ args = collect ($ node ->getArgs ());
68+
69+ if ($ args ->isEmpty ()) {
70+ return false ;
71+ }
72+
73+ $ arg = $ args ->firstWhere ('name.name ' , $ argName ) ?? $ args ->get ($ argPosition );
74+
75+ if ($ arg ->value instanceof String_) {
76+ return $ arg ->value ->value === $ argValue ;
77+ }
78+
79+ return false ;
80+ }
81+
5382 /**
5483 * @return string[] All translations keys used in the code
5584 */
@@ -64,6 +93,19 @@ public static function scanCode(string $code): array
6493
6594 /** @var FuncCall[] $results */
6695 $ results = $ nodeFinder ->find ($ ast , function (Node $ node ) {
96+
97+ if (
98+ $ node instanceof MethodCall &&
99+ $ node ->var instanceof FuncCall &&
100+ static ::isFunCallTo ($ node ->var , 'app ' , 'abstract ' , 0 , 'translator ' )
101+ ) {
102+ return in_array ($ node ->name ->name , ['get ' ]);
103+ }
104+
105+ if ($ node instanceof StaticCall && $ node ->class ->name === Lang::class) {
106+ return in_array ($ node ->name ->name , ['get ' , 'has ' , 'hasForLocale ' , 'choice ' ]);
107+ }
108+
67109 if ($ node instanceof FuncCall && $ node ->name instanceof Name) {
68110 return in_array ($ node ->name ->name , ['__ ' , 'trans ' , 'trans_choice ' ]);
69111 }
@@ -72,8 +114,8 @@ public static function scanCode(string $code): array
72114 });
73115
74116 return collect ($ results )
75- ->map (function (FuncCall $ funcCall ) {
76- $ args = collect ($ funcCall ->getArgs ());
117+ ->map (function (FuncCall | StaticCall | MethodCall $ node ) {
118+ $ args = collect ($ node ->getArgs ());
77119 $ argKey = $ args ->firstWhere ('name.name ' , 'key ' ) ?? $ args ->first ();
78120 $ value = $ argKey ->value ;
79121
0 commit comments