Skip to content

Commit

Permalink
Make compatible with php-parser:^4
Browse files Browse the repository at this point in the history
  • Loading branch information
artursvonda committed Apr 11, 2021
1 parent c2bc4de commit 09bdf52
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Tests/Translation/Extractor/File/Fixture/MyEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public function validateConstraintWithDefaultDomain(ExecutionContext $context)
->addViolation();
}

public function validateFullyQualifiedConstraintWithDefaultDomain(\Symfony\Component\Validator\Context\ExecutionContext $context)
{
$context
->buildViolation('entity.fully-qualified')
->addViolation();
}

public function validateConstraintWithCustomDomain(ExecutionContext $context)
{
$context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ public function testExtractValidationMessages()
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 15));
$expected->add($message);

$message = new Message('entity.custom-domain', 'custom-domain');
$message = new Message('entity.fully-qualified');
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 22));
$expected->add($message);

$message = new Message('entity.custom-domain', 'custom-domain');
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 29));
$expected->add($message);

$this->assertEquals($expected, $this->extract('MyEntity.php'));
}

Expand Down
16 changes: 11 additions & 5 deletions Translation/Extractor/File/ValidationContextExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public function enterNode(Node $node)

if ($node instanceof Node\Stmt\Use_) {
foreach ($node->uses as $use) {
$this->aliases[$use->alias] = (string) $use->name;
$alias = $use->alias ?: $use->getAlias()->name;
$this->aliases[$alias] = (string) $use->name;
}

return;
Expand All @@ -149,7 +150,9 @@ public function enterNode(Node $node)
$param1 = $params[0];
$paramClass = $this->resolveAlias((string) $param1->type);
if (is_subclass_of($paramClass, '\Symfony\Component\Validator\Context\ExecutionContextInterface')) {
$this->contextVariable = $param1->name;
// For BC, we try first $name property and then newer $var->name prop
// This can be changed to just $param1->var->name when we support only php-parser:^4
$this->contextVariable = !empty($param1->name) ? $param1->name : $param1->var->name;
}

return;
Expand All @@ -173,7 +176,10 @@ private function parseMethodCall(Node\Expr\MethodCall $node)
$this->parseMethodCall($node->var);
}

if ($node->name === 'buildViolation') {
// Cast to string to make compatible with both pre-v4 and post-v4 php-parser
$name = (string) $node->name;

if ($name === 'buildViolation') {
$this->id = null;
$this->domain = null;

Expand All @@ -184,14 +190,14 @@ private function parseMethodCall(Node\Expr\MethodCall $node)
$this->source = $this->fileSourceFactory->create($this->file, $arg1->value->getLine());
}
}
} elseif ($node->name === 'setTranslationDomain') {
} elseif ($name === 'setTranslationDomain') {
if ($node->args) {
$arg1 = $node->args[0];
if ($arg1->value instanceof Node\Scalar\String_) {
$this->domain = $arg1->value->value;
}
}
} elseif ($node->name === 'addViolation') {
} elseif ($name === 'addViolation') {
if ($this->id and $this->source) {
$this->messages[] = array(
'id' => $this->id,
Expand Down

0 comments on commit 09bdf52

Please sign in to comment.