Skip to content

Commit

Permalink
RegexpException replaced with CompileException & RuntimeException
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 8, 2023
1 parent bdb37ec commit 3a52ecf
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 21 deletions.
5 changes: 2 additions & 3 deletions src/Latte/Compiler/TagLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Latte;
use Latte\CompileException;
use Latte\RegexpException;


/**
Expand Down Expand Up @@ -185,7 +184,7 @@ private function tokenizeCode(): void
matchRE:
preg_match_all($re, $this->input, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL, $this->offset);
if (preg_last_error()) {
throw new RegexpException;
throw new CompileException(preg_last_error_msg());
}

foreach ($matches as $m) {
Expand Down Expand Up @@ -330,7 +329,7 @@ private function tokenizeString(string $endRe): string
matchRE:
preg_match_all($re, $this->input, $matches, PREG_SET_ORDER | PREG_UNMATCHED_AS_NULL, $this->offset);
if (preg_last_error()) {
throw new RegexpException;
throw new CompileException(preg_last_error_msg());
}

$buffer = '';
Expand Down
3 changes: 1 addition & 2 deletions src/Latte/Compiler/TemplateLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Latte;
use Latte\CompileException;
use Latte\RegexpException;


final class TemplateLexer
Expand Down Expand Up @@ -234,7 +233,7 @@ private function match(string $re): array
{
preg_match($re, $this->input, $matches, PREG_UNMATCHED_AS_NULL, $this->position->offset);
if (preg_last_error()) {
throw new RegexpException;
throw new CompileException(preg_last_error_msg());
}

$tokens = [];
Expand Down
6 changes: 3 additions & 3 deletions src/Latte/Essential/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static function indent(FilterInfo $info, string $s, int $level = 1, strin
} elseif ($info->contentType === ContentType::Html) {
$s = preg_replace_callback('#<(textarea|pre).*?</\1#si', fn($m) => strtr($m[0], " \t\r\n", "\x1F\x1E\x1D\x1A"), $s);
if (preg_last_error()) {
throw new Latte\RegexpException;
throw new Latte\RuntimeException(preg_last_error_msg());
}

$s = preg_replace('#(?:^|[\r\n]+)(?=[^\r\n])#', '$0' . str_repeat($chars, $level), $s);
Expand Down Expand Up @@ -250,7 +250,7 @@ public static function replaceRe(string $subject, string $pattern, string $repla
{
$res = preg_replace($pattern, $replacement, $subject);
if (preg_last_error()) {
throw new Latte\RegexpException;
throw new Latte\RuntimeException(preg_last_error_msg());
}

return $res;
Expand Down Expand Up @@ -388,7 +388,7 @@ public static function trim(FilterInfo $info, string $s, string $charlist = " \t
$charlist = preg_quote($charlist, '#');
$s = preg_replace('#^[' . $charlist . ']+|[' . $charlist . ']+$#Du', '', (string) $s);
if (preg_last_error()) {
throw new Latte\RegexpException;
throw new Latte\RuntimeException(preg_last_error_msg());
}

return $s;
Expand Down
2 changes: 1 addition & 1 deletion src/Latte/Runtime/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static function escapeJs(mixed $s): string

$json = json_encode($s, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE);
if ($error = json_last_error()) {
throw new Latte\RuntimeException(json_last_error_msg(), $error);
throw new Latte\RuntimeException(json_last_error_msg());
}

return str_replace([']]>', '<!', '</'], [']]\u003E', '\u003C!', '<\/'], $json);
Expand Down
13 changes: 2 additions & 11 deletions src/Latte/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ public function __construct(string $message, ?Compiler\Position $position = null


/**
* The exception that indicates error of the last Regexp execution.
* The exception occurred during template rendering.
*/
class RegexpException extends \Exception implements Exception
class RuntimeException extends \RuntimeException implements Exception
{
public function __construct()
{
parent::__construct(preg_last_error_msg(), preg_last_error());
}
}


Expand All @@ -62,8 +58,3 @@ public function __construct(string $message, ?Compiler\Position $position = null
$this->generateMessage();
}
}


class RuntimeException extends \RuntimeException implements Exception
{
}
2 changes: 1 addition & 1 deletion tests/filters/trim.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ Assert::same('e', Filters::trim($info, "\u{158}e-", "\u{158}-")); // Ře-

Assert::exception(
fn() => Filters::trim($info, "\xC2x\xA0"),
Latte\RegexpException::class,
Latte\RuntimeException::class,
null,
);

0 comments on commit 3a52ecf

Please sign in to comment.