Skip to content

Commit 583fd8e

Browse files
committed
Allow to extend the phpNodeVisitor #5
1 parent 151ad71 commit 583fd8e

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/PhpFunctionsScanner.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Gettext\Scanner;
55

66
use PhpParser\NodeTraverser;
7+
use PhpParser\NodeVisitor;
78
use PhpParser\Parser;
89
use PhpParser\ParserFactory;
910

@@ -27,10 +28,15 @@ public function scan(string $code, string $filename): array
2728
}
2829

2930
$traverser = new NodeTraverser();
30-
$visitor = new PhpNodeVisitor($filename, $this->validFunctions);
31+
$visitor = $this->createNodeVisitor($filename);
3132
$traverser->addVisitor($visitor);
3233
$traverser->traverse($ast);
3334

3435
return $visitor->getFunctions();
3536
}
37+
38+
protected function createNodeVisitor(string $filename): NodeVisitor
39+
{
40+
return new PhpNodeVisitor($filename, $this->validFunctions);
41+
}
3642
}

src/PhpNodeVisitor.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class PhpNodeVisitor implements NodeVisitor
1414
protected $validFunctions;
1515
protected $filename;
1616
protected $functions = [];
17+
protected $argumentsHandlers = [];
1718

1819
public function __construct(string $filename, array $validFunctions = null)
1920
{
@@ -76,7 +77,14 @@ protected function createFunction(FuncCall $node): ParsedFunction
7677
$function->addComment(static::getComment($comment));
7778
}
7879

79-
switch ($value->getType()) {
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) {
8088
case 'Scalar_String':
8189
case 'Scalar_LNumber':
8290
case 'Scalar_DNumber':
@@ -90,6 +98,11 @@ protected function createFunction(FuncCall $node): ParsedFunction
9098
return $function;
9199
}
92100

101+
public function setArgumentsHandler(string $type, callable $handler)
102+
{
103+
$this->argumentsHandlers[$type] = $handler;
104+
}
105+
93106
protected static function getComment(Comment $comment): string
94107
{
95108
$text = $comment->getReformattedText();

tests/assets/code.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,5 @@ public function test()
125125
$test();
126126
}
127127
}
128+
129+
t("Translatable string","",["context"=>"Context string"]);

0 commit comments

Comments
 (0)