Skip to content

Commit

Permalink
minor #4592 Simplify Error implementation (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Simplify Error implementation

Commits
-------

d10bcad Simplify Error implementation
  • Loading branch information
fabpot committed Feb 21, 2025
2 parents 4effb66 + d10bcad commit 9b4db2e
Showing 1 changed file with 17 additions and 43 deletions.
60 changes: 17 additions & 43 deletions src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@
class Error extends \Exception
{
private $lineno;
private $name;
private $rawMessage;
private $sourcePath;
private $sourceCode;
private ?Source $source;

/**
* Constructor.
Expand All @@ -57,16 +55,8 @@ public function __construct(string $message, int $lineno = -1, ?Source $source =
{
parent::__construct('', 0, $previous);

if (null === $source) {
$name = null;
} else {
$name = $source->getName();
$this->sourceCode = $source->getCode();
$this->sourcePath = $source->getPath();
}

$this->lineno = $lineno;
$this->name = $name;
$this->source = $source;
$this->rawMessage = $message;
$this->updateRepr();
}
Expand All @@ -84,25 +74,17 @@ public function getTemplateLine(): int
public function setTemplateLine(int $lineno): void
{
$this->lineno = $lineno;

$this->updateRepr();
}

public function getSourceContext(): ?Source
{
return $this->name ? new Source($this->sourceCode, $this->name, $this->sourcePath) : null;
return $this->source;
}

public function setSourceContext(?Source $source = null): void
{
if (null === $source) {
$this->sourceCode = $this->name = $this->sourcePath = null;
} else {
$this->sourceCode = $source->getCode();
$this->name = $source->getName();
$this->sourcePath = $source->getPath();
}

$this->source = $source;
$this->updateRepr();
}

Expand All @@ -122,8 +104,8 @@ private function updateRepr(): void
{
$this->message = $this->rawMessage;

if ($this->sourcePath && $this->lineno > 0) {
$this->file = $this->sourcePath;
if ($this->source && $this->source->getPath() && $this->lineno > 0) {
$this->file = $this->source->getPath();
$this->line = $this->lineno;

return;
Expand All @@ -141,11 +123,12 @@ private function updateRepr(): void
$questionMark = true;
}

if ($this->name) {
if (\is_string($this->name) || $this->name instanceof \Stringable) {
$name = \sprintf('"%s"', $this->name);
if ($this->source && $this->source->getName()) {
$name = $this->source->getName();
if (\is_string($name) || $name instanceof \Stringable) {
$name = \sprintf('"%s"', $name);
} else {
$name = json_encode($this->name);
$name = json_encode($name);
}
$this->message .= \sprintf(' in %s', $name);
}
Expand All @@ -165,34 +148,25 @@ private function updateRepr(): void

private function guessTemplateInfo(): void
{
// $this->source is never null here (see guess() usage in Template)

$template = null;
$templateClass = null;

$backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT);
foreach ($backtrace as $trace) {
if (isset($trace['object']) && $trace['object'] instanceof Template) {
$currentClass = \get_class($trace['object']);
$isEmbedContainer = null === $templateClass ? false : str_starts_with($templateClass, $currentClass);
if (null === $this->name || ($this->name == $trace['object']->getTemplateName() && !$isEmbedContainer)) {
if ($this->source->getName() === $trace['object']->getTemplateName() && !$isEmbedContainer) {
$template = $trace['object'];
$templateClass = \get_class($trace['object']);
}
}
}

// update template name
if (null !== $template && null === $this->name) {
$this->name = $template->getTemplateName();
}

// update template path if any
if (null !== $template && null === $this->sourcePath) {
$src = $template->getSourceContext();
$this->sourceCode = $src->getCode();
$this->sourcePath = $src->getPath();
}

if (null === $template || $this->lineno > -1) {
if ($template) {
$this->source = $template->getSourceContext();
} elseif ($this->lineno > -1) {
return;
}

Expand Down

0 comments on commit 9b4db2e

Please sign in to comment.