33
44namespace Gettext \Scanner ;
55
6+ use InvalidArgumentException ;
67use Peast \Syntax \Node \CallExpression ;
78use Peast \Syntax \Node \Comment ;
9+ use Peast \Syntax \Node \Identifier ;
10+ use Peast \Syntax \Node \Literal ;
11+ use Peast \Syntax \Node \MemberExpression ;
812use Peast \Syntax \Node \Node ;
13+ use Peast \Syntax \Node \TemplateLiteral ;
914
1015class JsNodeVisitor
1116{
1217 protected $ validFunctions ;
1318 protected $ filename ;
1419 protected $ functions = [];
1520
16- public function __construct (string $ filename , array $ validFunctions = null )
21+ public function __construct (string $ filename , ? array $ validFunctions = null )
1722 {
1823 $ this ->filename = $ filename ;
1924 $ this ->validFunctions = $ validFunctions ;
2025 }
2126
22- public function __invoke (Node $ node )
27+ public function __invoke (Node $ node ): void
2328 {
24- if ($ node-> getType () === ' CallExpression ' ) {
29+ if ($ node instanceof CallExpression) {
2530 $ function = $ this ->createFunction ($ node );
2631
2732 if ($ function ) {
@@ -55,24 +60,20 @@ protected function createFunction(CallExpression $node): ?ParsedFunction
5560 static ::addComments ($ function , $ node ->getCallee ());
5661
5762 foreach ($ node ->getArguments () as $ argument ) {
58- switch ($ argument ->getType ()) {
59- case 'Literal ' :
60- $ function ->addArgument ($ argument ->getValue ());
61- static ::addComments ($ function , $ argument );
62- break ;
63- case 'TemplateLiteral ' :
64- if ($ argument ->getExpressions ()) {
65- $ function ->addArgument ();
66- break ;
67- }
68-
63+ if ($ argument instanceof Literal) {
64+ $ function ->addArgument ($ argument ->getValue ());
65+ static ::addComments ($ function , $ argument );
66+ } elseif ($ argument instanceof TemplateLiteral) {
67+ if ($ argument ->getExpressions ()) {
68+ $ function ->addArgument ();
69+ } else {
6970 $ quasis = $ argument ->getQuasis ();
7071 $ quasis = array_shift ($ quasis );
7172 $ function ->addArgument ($ quasis ->getValue ());
7273 static ::addComments ($ function , $ argument );
73- break ;
74- default :
75- $ function ->addArgument ();
74+ }
75+ } else {
76+ $ function ->addArgument ();
7677 }
7778 }
7879
@@ -83,19 +84,15 @@ protected static function getFunctionName(CallExpression $node): ?string
8384 {
8485 $ callee = $ node ->getCallee ();
8586
86- switch ($ callee ->getType ()) {
87- case 'Identifier ' :
88- return $ callee ->getName ();
89- case 'MemberExpression ' :
90- $ property = $ callee ->getProperty ();
91-
92- if ($ property ->getType () === 'Identifier ' ) {
93- return $ property ->getName ();
94- }
95- return null ;
96- default :
97- return null ;
87+ if ($ callee instanceof Identifier) {
88+ return $ callee ->getName ();
89+ } elseif ($ callee instanceof MemberExpression) {
90+ $ property = $ callee ->getProperty ();
91+ if ($ property instanceof Identifier) {
92+ return $ property ->getName ();
93+ }
9894 }
95+ return null ;
9996 }
10097
10198 protected static function addComments (ParsedFunction $ function , Node $ node ): void
@@ -109,7 +106,7 @@ protected static function getComment(Comment $comment): string
109106 {
110107 $ text = $ comment ->getText ();
111108
112- $ lines = array_map (function ($ line ) {
109+ $ lines = array_map (function ($ line ): string {
113110 $ line = ltrim ($ line , "#*/ \t" );
114111 $ line = rtrim ($ line , "#*/ \t" );
115112 return trim ($ line );
0 commit comments