Skip to content

Commit

Permalink
Output processed files during fbt collecting
Browse files Browse the repository at this point in the history
  • Loading branch information
richardDobron committed Jun 24, 2023
1 parent f7c5559 commit 44bde9d
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions src/fbt/Services/CollectFbtsService.php
Expand Up @@ -76,37 +76,47 @@ public function collectFromFiles(string $path, string $src, string $fbtCommonPat
FbtHooks::storePhrases();
}

protected function matchFbtCalls(Node $node): bool
{
return ($node instanceof FuncCall
&& $node->name instanceof Name
&& $node->name->toString() === 'fbt')
|| ($node instanceof StaticCall
&& $node->class instanceof Name
&& $node->name instanceof Identifier
&& in_array($node->class->toString(), ['fbt', 'fbt\\fbt'])
&& $node->name->toString() === 'c'
);
}

/**
* @throws \Throwable
* @throws \fbt\Exceptions\FbtParserException
*/
protected function collectFromOneFile(string $source, string $path)
protected function collectFromOneFile(string $source, string $path): bool
{
if (! preg_match('/fbt(::c)?\s*\(/', $source)) {
return;
echo "\033[0;37m$path \033[0m" . PHP_EOL;

return false;
}

$ast = $this->parser->parse($source);
$ast = $this->traverser->traverse($ast);

/** @var StaticCall[] $translateFunctionCalls */
$translateFunctionCalls = $this->nodeFinder->find($ast, function (Node $node) {
return ($node instanceof FuncCall
&& $node->name instanceof Name
&& $node->name->toString() === 'fbt')
|| ($node instanceof StaticCall
&& $node->class instanceof Name
&& $node->name instanceof Identifier
&& in_array($node->class->toString(), ['fbt', 'fbt\\fbt'])
&& $node->name->toString() === 'c');
/** @var StaticCall[] $fbtFunctionCalls */
$fbtFunctionCalls = $this->nodeFinder->find($ast, function (Node $node) {
return $this->matchFbtCalls($node);
});

foreach ($translateFunctionCalls as $translateFunctionCall) {
$code = $this->printer->prettyPrintExpr($translateFunctionCall);
$line = $translateFunctionCall->getLine();
echo "\033[15m$path \033[0m" . PHP_EOL;

foreach ($fbtFunctionCalls as $fbtFunctionCall) {
$code = $this->printer->prettyPrintExpr($fbtFunctionCall);
$line = $fbtFunctionCall->getLine();

try {
if ($translateFunctionCall->args[0]->value instanceof Ternary) {
if ($fbtFunctionCall->args[0]->value instanceof Ternary) {
throw new FbtParserException("Unexpected node type: Ternary. fbt()'s first argument should be a string literal, a construct like fbt::param() or an array of those called in file.php(1).");
}

Expand All @@ -131,5 +141,7 @@ protected function collectFromOneFile(string $source, string $path)
echo PHP_EOL . PHP_EOL;
}
}

return true;
}
}

0 comments on commit 44bde9d

Please sign in to comment.