From 0049836287ff808822496e491a16f92af5ce74bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Dobro=C5=88?= Date: Fri, 12 Jan 2024 11:40:34 +0100 Subject: [PATCH] Improve fbt:param collection --- src/fbt/Runtime/Shared/FbtHooks.php | 4 +++- src/fbt/Services/CollectFbtsService.php | 6 +++--- src/fbt/Transform/NodeVisitor.php | 4 +++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/fbt/Runtime/Shared/FbtHooks.php b/src/fbt/Runtime/Shared/FbtHooks.php index 77e5be9..c6256a9 100644 --- a/src/fbt/Runtime/Shared/FbtHooks.php +++ b/src/fbt/Runtime/Shared/FbtHooks.php @@ -16,7 +16,9 @@ class FbtHooks /* @var array */ private static $actions = []; /* @var array */ - public static $sourceStrings = []; + public static $sourceStrings = [ + 'phrases' => [], + ]; /* @var array */ public static $sourceHashes = []; /* @var array */ diff --git a/src/fbt/Services/CollectFbtsService.php b/src/fbt/Services/CollectFbtsService.php index bd7231c..d36162f 100644 --- a/src/fbt/Services/CollectFbtsService.php +++ b/src/fbt/Services/CollectFbtsService.php @@ -63,7 +63,7 @@ public function __construct() * @throws \fbt\Exceptions\FbtInvalidConfigurationException * @throws \fbt\Exceptions\FbtParserException */ - public function collectFromFiles(string $path, string $src, string $fbtCommonPath, bool $cleanCache) + public function collectFromFiles(string $path, string $src, ?string $fbtCommonPath, bool $cleanCache) { $fbtDir = $path . '/'; $file = $fbtDir . '.source_strings.json'; @@ -93,7 +93,7 @@ protected function compileCode(Expr $fbtFunctionClassCall): string return preg_replace('/(\\\*|\b)(fbt\\\+)fbt/', 'fbt', $code); } - protected function matchFbtCalls(Node $node): bool + public static function matchFbtCalls(Node $node): bool { return ($node instanceof FuncCall && $node->name instanceof Name @@ -126,7 +126,7 @@ protected function collectFromOneFile(string $source, string $path): bool /** @var StaticCall[] $fbtFunctionCalls */ $fbtFunctionCalls = $this->nodeFinder->find($ast, function (Node $node) { - return $this->matchFbtCalls($node); + return self::matchFbtCalls($node); }); echo "\033[15m$path \033[0m" . PHP_EOL; diff --git a/src/fbt/Transform/NodeVisitor.php b/src/fbt/Transform/NodeVisitor.php index 0f09ae9..016b8ce 100644 --- a/src/fbt/Transform/NodeVisitor.php +++ b/src/fbt/Transform/NodeVisitor.php @@ -2,6 +2,7 @@ namespace fbt\Transform; +use fbt\Services\CollectFbtsService; use PhpParser\Node; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Name; @@ -18,7 +19,8 @@ public function enterNode(Node $node) && in_array($node->class->toString(), ['fbt', 'fbt\fbt'])) { switch ($node->name->toString()) { case "param": - if (! ($node->args[1]->value instanceof String_)) { + if (! CollectFbtsService::matchFbtCalls($node->args[1]->value) + && ! ($node->args[1]->value instanceof String_)) { $node->args[1] = new String_('value'); }