Skip to content

Commit bfbffca

Browse files
committed
changed the previous commit. Now the array arguments are processed #5
1 parent 572db9a commit bfbffca

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

src/PhpNodeVisitor.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use PhpParser\Comment;
77
use PhpParser\Node;
8+
use PhpParser\Node\Expr;
89
use PhpParser\Node\Expr\FuncCall;
910
use PhpParser\Node\Name;
1011
use PhpParser\NodeVisitor;
@@ -14,7 +15,6 @@ class PhpNodeVisitor implements NodeVisitor
1415
protected $validFunctions;
1516
protected $filename;
1617
protected $functions = [];
17-
protected $argumentsHandlers = [];
1818

1919
public function __construct(string $filename, array $validFunctions = null)
2020
{
@@ -77,32 +77,12 @@ protected function createFunction(FuncCall $node): ParsedFunction
7777
$function->addComment(static::getComment($comment));
7878
}
7979

80-
$type = $value->getType();
81-
82-
if (isset($this->argumentsHandlers[$type])) {
83-
call_user_func($this->argumentsHandlers[$type], $function, $value);
84-
continue;
85-
}
86-
87-
switch ($type) {
88-
case 'Scalar_String':
89-
case 'Scalar_LNumber':
90-
case 'Scalar_DNumber':
91-
$function->addArgument($value->value);
92-
break;
93-
default:
94-
$function->addArgument();
95-
}
80+
$function->addArgument(static::getValue($value));
9681
}
9782

9883
return $function;
9984
}
10085

101-
public function setArgumentsHandler(string $type, callable $handler)
102-
{
103-
$this->argumentsHandlers[$type] = $handler;
104-
}
105-
10686
protected static function getComment(Comment $comment): string
10787
{
10888
$text = $comment->getReformattedText();
@@ -115,4 +95,31 @@ protected static function getComment(Comment $comment): string
11595

11696
return trim(implode("\n", $lines));
11797
}
98+
99+
protected static function getValue(Expr $value)
100+
{
101+
$type = $value->getType();
102+
103+
switch ($type) {
104+
case 'Scalar_String':
105+
case 'Scalar_LNumber':
106+
case 'Scalar_DNumber':
107+
return $value->value;
108+
case 'Expr_Array':
109+
$arr = [];
110+
111+
foreach ($value->items as $item) {
112+
$value = static::getValue($item->value);
113+
114+
if ($item->key === null) {
115+
$arr[] = $value;
116+
} else {
117+
$key = static::getValue($item->key);
118+
$arr[$key] = $value;
119+
}
120+
}
121+
122+
return $arr;
123+
}
124+
}
118125
}

tests/PhpFunctionsScannerTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testPhpFunctionsExtractor()
2323
$code = file_get_contents($file);
2424
$functions = $scanner->scan($code, $file);
2525

26-
$this->assertCount(11, $functions);
26+
$this->assertCount(12, $functions);
2727

2828
//fn1
2929
$function = array_shift($functions);
@@ -81,7 +81,7 @@ public function testPhpFunctionsExtractor()
8181
$function = array_shift($functions);
8282
$this->assertSame('fn6', $function->getName());
8383
$this->assertSame(1, $function->countArguments());
84-
$this->assertSame([null], $function->getArguments());
84+
$this->assertSame([['arr']], $function->getArguments());
8585
$this->assertSame(7, $function->getLine());
8686
$this->assertSame(7, $function->getLastLine());
8787
$this->assertSame($file, $function->getFilename());
@@ -151,6 +151,20 @@ public function testPhpFunctionsExtractor()
151151
$this->assertSame("Related comment\nnumber one", array_shift($comments));
152152
$this->assertSame('Related comment 2', array_shift($comments));
153153
$this->assertSame('ALLOW: Related comment 3', array_shift($comments));
154+
155+
//fn13
156+
$function = array_shift($functions);
157+
$this->assertSame('fn13', $function->getName());
158+
$this->assertSame(3, $function->countArguments());
159+
$this->assertSame([
160+
'Translatable string',
161+
'',
162+
['context' => 'Context string', 'foo'],
163+
], $function->getArguments());
164+
$this->assertSame(30, $function->getLine());
165+
$this->assertSame(30, $function->getLastLine());
166+
$this->assertSame($file, $function->getFilename());
167+
$this->assertCount(0, $function->getComments());
154168
}
155169

156170
public function _testPhpFunctionsScannerWithDisabledComments()

tests/assets/functions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@
2626
'arg12'
2727
/* No Related comment 4 */
2828
);
29+
30+
fn13("Translatable string","",["context"=>"Context string", 'foo']);

0 commit comments

Comments
 (0)