Skip to content

Commit

Permalink
refactor FluentBundle::reportError() method
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmajor committed May 29, 2021
1 parent 1789256 commit 07656a9
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions src/Bundle/FluentBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ public function hasFunction(string $id): bool
return array_key_exists($id, $this->functions);
}

protected function reportError(ResolverException $error): void
protected function reportError(ResolverException $error, string $value = '???'): FluentNone
{
$this->errors[] = $this->strict ? throw $error : $error;

return new FluentNone($value);
}

/**
Expand Down Expand Up @@ -192,15 +194,11 @@ protected function resolvePattern(
ResolutionScope $scope,
): string {
if (! $pattern) {
$this->reportError(new NullPatternException());

return (string) new FluentNone();
return (string) $this->reportError(new NullPatternException());
}

if (isset($scope->dirty[$pattern])) {
$this->reportError(new CyclicReferenceException());

return (string) new FluentNone();
return (string) $this->reportError(new CyclicReferenceException());
}

$scope->dirty[$pattern] = true;
Expand Down Expand Up @@ -287,9 +285,7 @@ protected function resolveVariableReference(
// Missing variables references produce ReferenceExceptions.
$argument = $scope->arguments[$id];
} else {
$this->reportError(new ReferenceException("Unknown variable: \${$id}."));

return new FluentNone("\${$id}");
return $this->reportError(new ReferenceException("Unknown variable: \${$id}."), "\${$id}");
}

// Return early if the argument already is an instance of Stringable.
Expand All @@ -302,9 +298,7 @@ protected function resolveVariableReference(
->setFluentLocale($this->locale);
}

$this->reportError(new TypeException($id, gettype($argument)));

return new FluentNone("\${$id}");
return $this->reportError(new TypeException($id, gettype($argument)), "\${$id}");
}

protected function resolveMessageReference(
Expand All @@ -316,9 +310,7 @@ protected function resolveMessageReference(
$message = $this->messages[$id] ?? null;

if (! $message) {
$this->reportError(new ReferenceException("Unknown message: {$id}."));

return new FluentNone($id);
return $this->reportError(new ReferenceException("Unknown message: {$id}."), $id);
}

if ($attributeName = $reference->attribute?->name) {
Expand All @@ -332,18 +324,17 @@ protected function resolveMessageReference(
return $this->resolvePattern($attribute->value, $scope);
}

$this->reportError(new ReferenceException("Unknown attribute: {$id}.{$attributeName}."));

return new FluentNone("{$id}.{$attributeName}");
return $this->reportError(
new ReferenceException("Unknown attribute: {$id}.{$attributeName}."),
"{$id}.{$attributeName}",
);
}

if ($message->value) {
return $this->resolvePattern($message->value, $scope);
}

$this->reportError(new ReferenceException("No value: {$id}."));

return new FluentNone($id);
return $this->reportError(new ReferenceException("No value: {$id}."), $id);
}

protected function resolveTermReference(
Expand All @@ -355,9 +346,7 @@ protected function resolveTermReference(
$term = $this->terms[$id] ?? null;

if (! $term) {
$this->reportError(new ReferenceException("Unknown term: -{$id}."));

return new FluentNone("-{$id}");
return $this->reportError(new ReferenceException("Unknown term: -{$id}."), "-{$id}");
}

if ($attributeName = $reference->attribute?->name) {
Expand All @@ -378,9 +367,10 @@ protected function resolveTermReference(
return $resolved;
}

$this->reportError(new ReferenceException("Unknown attribute: -{$id}.{$attributeName}."));

return new FluentNone("-{$id}.{$attributeName}");
return $this->reportError(
new ReferenceException("Unknown attribute: -{$id}.{$attributeName}."),
"-{$id}.{$attributeName}",
);
}

$scope->parameters = $reference->arguments?->named ?? [];
Expand All @@ -401,9 +391,7 @@ protected function resolveFunctionReference(
$function = $this->functions[$name] ?? null;

if (! $function) {
$this->reportError(new ReferenceException("Unknown function: {$name}()."));

return new FluentNone("{$name}()");
return $this->reportError(new ReferenceException("Unknown function: {$name}()."), "{$name}()");
}

$arguments = $this->getFunctionArguments($reference->arguments, $scope);
Expand Down

0 comments on commit 07656a9

Please sign in to comment.