Skip to content

Commit

Permalink
Reuse the SqlFormatter instance (#1436)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Jun 26, 2024
1 parent 87a7ce0 commit cb93a3c
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Generator/SqlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
class SqlGenerator
{
private SqlFormatter|null $formatter = null;

public function __construct(
private readonly Configuration $configuration,
private readonly AbstractPlatform $platform,
Expand Down Expand Up @@ -55,7 +57,7 @@ public function generate(
$maxLength = $lineLength - 18 - 8; // max - php code length - indentation

if (strlen($query) > $maxLength) {
$query = (new SqlFormatter(new NullHighlighter()))->format($query);
$query = $this->formatQuery($query);
}
}

Expand Down Expand Up @@ -84,4 +86,11 @@ public function generate(

return implode("\n", $code);
}

private function formatQuery(string $query): string
{
$this->formatter ??= new SqlFormatter(new NullHighlighter());

return $this->formatter->format($query);
}
}

0 comments on commit cb93a3c

Please sign in to comment.