-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/extract concat strings #379
base: master
Are you sure you want to change the base?
Changes from all commits
1f06b7a
fa7bd90
918cee9
71c6628
02ab8ad
487d6ec
adbdc3b
d44483c
fd8a177
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<a href="#"><?php echo $view['translator']->trans('foo.bar') ?></a> | ||
|
||
<a href="#"><?php echo /** @Desc("Foo Bar") */ $view['translator']->trans('baz', array(), 'moo') ?></a> | ||
<a href="#"><?php echo /** @Desc("Foo Bar") */ $view['translator']->trans('baz', array(), 'moo') ?></a> | ||
|
||
<a href="#"><?php echo $view['test']->trans('Foo' . ' Bar' . ' Moo') ?></a> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
use PhpParser\NodeTraverser; | ||
use PhpParser\NodeVisitor; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\Node\Expr\BinaryOp\Concat; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
|
@@ -133,12 +134,12 @@ public function enterNode(Node $node) | |
} | ||
} | ||
|
||
if (!$node->args[0]->value instanceof String_) { | ||
if (!$node->args[0]->value instanceof String_ and !$node->args[0]->value instanceof Concat) { | ||
if ($ignore) { | ||
return; | ||
} | ||
|
||
$message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine()); | ||
$message = sprintf('Can only extract the translation id from a scalar string or a Concat, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine()); | ||
|
||
if ($this->logger) { | ||
$this->logger->error($message); | ||
|
@@ -148,16 +149,20 @@ public function enterNode(Node $node) | |
throw new RuntimeException($message); | ||
} | ||
|
||
$id = $node->args[0]->value->value; | ||
if ($node->args[0]->value instanceof Concat) { | ||
$id = $this->concatToString($node->args[0]->value); | ||
} else { | ||
$id = $node->args[0]->value->value; | ||
} | ||
|
||
$index = $this->methodsToExtractFrom[strtolower($node->name)]; | ||
if (isset($node->args[$index])) { | ||
if (!$node->args[$index]->value instanceof String_) { | ||
if (!$node->args[$index]->value instanceof String_ and !$node->args[$index]->value instanceof Concat) { | ||
if ($ignore) { | ||
return; | ||
} | ||
|
||
$message = sprintf('Can only extract the translation domain from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine()); | ||
$message = sprintf('Can only extract the translation domain from a scalar string or a Concat, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine()); | ||
|
||
if ($this->logger) { | ||
$this->logger->error($message); | ||
|
@@ -167,7 +172,11 @@ public function enterNode(Node $node) | |
throw new RuntimeException($message); | ||
} | ||
|
||
$domain = $node->args[$index]->value->value; | ||
if ($node->args[$index]->value instanceof Concat) { | ||
$domain = $this->concatToString($node->args[$index]->value); | ||
} else { | ||
$domain = $node->args[$index]->value->value; | ||
} | ||
} else { | ||
$domain = 'messages'; | ||
} | ||
|
@@ -180,6 +189,19 @@ public function enterNode(Node $node) | |
$this->catalogue->add($message); | ||
} | ||
|
||
/** | ||
* @param $node | ||
* @return string | ||
*/ | ||
private function concatToString($node) | ||
{ | ||
if ($node instanceof String_) { | ||
return $node->value; | ||
} else { | ||
return self::concatToString($node->left) . self::concatToString($node->right); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why a static call? |
||
} | ||
} | ||
|
||
/** | ||
* @param \SplFileInfo $file | ||
* @param MessageCatalogue $catalogue | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,7 +216,7 @@ public function extract() | |
if (false !== $pos = strrpos($file, '.')) { | ||
$extension = substr($file, $pos + 1); | ||
|
||
if ('php' === $extension) { | ||
if ('php' === $extension or 'phtml' === $extension) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? |
||
try { | ||
$ast = $this->phpParser->parse(file_get_contents($file)); | ||
} catch (Error $ex) { | ||
|
@@ -227,7 +227,7 @@ public function extract() | |
$visitingArgs[] = $ast; | ||
} elseif ('twig' === $extension) { | ||
$visitingMethod = 'visitTwigFile'; | ||
$visitingArgs[] = $this->twig->parse($this->twig->tokenize(file_get_contents($file), (string) $file)); | ||
$visitingArgs[] = $this->twig->parse($this->twig->tokenize(file_get_contents($file), (string)$file)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this. It violates the code style. Also, it is good practice not to make CS changes mixed with a feature. CS changes should be in a separate PR. |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider the following:
$translation->trans('foo'.$bar);
This will fail. You need to add tests for this and also check if it is an instance of Concat. If not we need to throw an Exception