🔧
This commit is contained in:
@@ -39,13 +39,18 @@ final class PhpAstExtractor extends AbstractFileExtractor implements ExtractorIn
|
||||
throw new \LogicException(sprintf('You cannot use "%s" as the "nikic/php-parser" package is not installed. Try running "composer require nikic/php-parser".', static::class));
|
||||
}
|
||||
|
||||
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
|
||||
$this->parser = (new ParserFactory())->createForHostVersion();
|
||||
}
|
||||
|
||||
public function extract(iterable|string $resource, MessageCatalogue $catalogue): void
|
||||
{
|
||||
foreach ($this->extractFiles($resource) as $file) {
|
||||
$traverser = new NodeTraverser();
|
||||
|
||||
// This is needed to resolve namespaces in class methods/constants.
|
||||
$nameResolver = new NodeVisitor\NameResolver();
|
||||
$traverser->addVisitor($nameResolver);
|
||||
|
||||
/** @var AbstractVisitor&NodeVisitor $visitor */
|
||||
foreach ($this->visitors as $visitor) {
|
||||
$visitor->initialize($catalogue, $file, $this->prefix);
|
||||
|
||||
@@ -94,7 +94,7 @@ class PhpStringTokenParser
|
||||
* @param string $str String without quotes
|
||||
* @param string|null $quote Quote type
|
||||
*/
|
||||
public static function parseEscapeSequences(string $str, string $quote = null): string
|
||||
public static function parseEscapeSequences(string $str, ?string $quote = null): string
|
||||
{
|
||||
if (null !== $quote) {
|
||||
$str = str_replace('\\'.$quote, $quote, $str);
|
||||
|
||||
@@ -81,7 +81,7 @@ abstract class AbstractVisitor
|
||||
return \PHP_INT_MAX;
|
||||
}
|
||||
|
||||
private function getStringNamedArguments(Node\Expr\CallLike|Node\Attribute $node, string $argumentName = null, bool $isArgumentNamePattern = false): array
|
||||
private function getStringNamedArguments(Node\Expr\CallLike|Node\Attribute $node, ?string $argumentName = null, bool $isArgumentNamePattern = false): array
|
||||
{
|
||||
$args = $node instanceof Node\Expr\CallLike ? $node->getArgs() : $node->args;
|
||||
$argumentValues = [];
|
||||
@@ -119,6 +119,17 @@ abstract class AbstractVisitor
|
||||
return $node->expr->value;
|
||||
}
|
||||
|
||||
if ($node instanceof Node\Expr\ClassConstFetch) {
|
||||
try {
|
||||
$reflection = new \ReflectionClass($node->class->toString());
|
||||
$constant = $reflection->getReflectionConstant($node->name->toString());
|
||||
if (false !== $constant && \is_string($constant->getValue())) {
|
||||
return $constant->getValue();
|
||||
}
|
||||
} catch (\ReflectionException) {
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,11 @@ final class ConstraintVisitor extends AbstractVisitor implements NodeVisitor
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): ?Node
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function leaveNode(Node $node): ?Node
|
||||
{
|
||||
if (!$node instanceof Node\Expr\New_ && !$node instanceof Node\Attribute) {
|
||||
return null;
|
||||
@@ -42,7 +47,7 @@ final class ConstraintVisitor extends AbstractVisitor implements NodeVisitor
|
||||
return null;
|
||||
}
|
||||
|
||||
$parts = $className->parts;
|
||||
$parts = $className->getParts();
|
||||
$isConstraintClass = false;
|
||||
|
||||
foreach ($parts as $part) {
|
||||
@@ -100,11 +105,6 @@ final class ConstraintVisitor extends AbstractVisitor implements NodeVisitor
|
||||
return null;
|
||||
}
|
||||
|
||||
public function leaveNode(Node $node): ?Node
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function afterTraverse(array $nodes): ?Node
|
||||
{
|
||||
return null;
|
||||
|
||||
@@ -25,6 +25,11 @@ final class TransMethodVisitor extends AbstractVisitor implements NodeVisitor
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): ?Node
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function leaveNode(Node $node): ?Node
|
||||
{
|
||||
if (!$node instanceof Node\Expr\MethodCall && !$node instanceof Node\Expr\FuncCall) {
|
||||
return null;
|
||||
@@ -34,12 +39,12 @@ final class TransMethodVisitor extends AbstractVisitor implements NodeVisitor
|
||||
return null;
|
||||
}
|
||||
|
||||
$name = (string) $node->name;
|
||||
$name = $node->name instanceof Node\Name ? $node->name->getLast() : (string) $node->name;
|
||||
|
||||
if ('trans' === $name || 't' === $name) {
|
||||
$firstNamedArgumentIndex = $this->nodeFirstNamedArgumentIndex($node);
|
||||
|
||||
if (!$messages = $this->getStringArguments($node, 0 < $firstNamedArgumentIndex ? 0 : 'message')) {
|
||||
if (!$messages = $this->getStringArguments($node, 0 < $firstNamedArgumentIndex ? 0 : 'id')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -53,11 +58,6 @@ final class TransMethodVisitor extends AbstractVisitor implements NodeVisitor
|
||||
return null;
|
||||
}
|
||||
|
||||
public function leaveNode(Node $node): ?Node
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function afterTraverse(array $nodes): ?Node
|
||||
{
|
||||
return null;
|
||||
|
||||
@@ -25,6 +25,11 @@ final class TranslatableMessageVisitor extends AbstractVisitor implements NodeVi
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): ?Node
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function leaveNode(Node $node): ?Node
|
||||
{
|
||||
if (!$node instanceof Node\Expr\New_) {
|
||||
return null;
|
||||
@@ -34,7 +39,7 @@ final class TranslatableMessageVisitor extends AbstractVisitor implements NodeVi
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!\in_array('TranslatableMessage', $className->parts, true)) {
|
||||
if (!\in_array('TranslatableMessage', $className->getParts(), true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -53,11 +58,6 @@ final class TranslatableMessageVisitor extends AbstractVisitor implements NodeVi
|
||||
return null;
|
||||
}
|
||||
|
||||
public function leaveNode(Node $node): ?Node
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function afterTraverse(array $nodes): ?Node
|
||||
{
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user