Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/CliHighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\SqlFormatter;

use function sprintf;

use const PHP_EOL;

final class CliHighlighter implements Highlighter
Expand Down Expand Up @@ -33,9 +34,9 @@ public function __construct(array $escapeSequences = [])
];
}

public function highlightToken(int $type, string $value) : string
public function highlightToken(int $type, string $value): string
{
if ($type === Token::TOKEN_TYPE_BOUNDARY && ($value==='(' || $value===')')) {
if ($type === Token::TOKEN_TYPE_BOUNDARY && ($value === '(' || $value === ')')) {
return $value;
}

Expand All @@ -47,7 +48,7 @@ public function highlightToken(int $type, string $value) : string
return $prefix . $value . "\x1b[0m";
}

private function prefix(int $type) : ?string
private function prefix(int $type): ?string
{
if (! isset(self::TOKEN_TYPE_TO_HIGHLIGHT[$type])) {
return null;
Expand All @@ -56,7 +57,7 @@ private function prefix(int $type) : ?string
return $this->escapeSequences[self::TOKEN_TYPE_TO_HIGHLIGHT[$type]];
}

public function highlightError(string $value) : string
public function highlightError(string $value): string
{
return sprintf(
'%s%s%s%s',
Expand All @@ -67,12 +68,12 @@ public function highlightError(string $value) : string
);
}

public function highlightErrorMessage(string $value) : string
public function highlightErrorMessage(string $value): string
{
return $this->highlightError($value);
}

public function output(string $string) : string
public function output(string $string): string
{
return $string . "\n";
}
Expand Down
6 changes: 3 additions & 3 deletions src/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(array $tokens)
$this->tokens = $tokens;
}

public function next(?int $exceptTokenType = null) : ?Token
public function next(?int $exceptTokenType = null): ?Token
{
while ($token = $this->tokens[++$this->position] ?? null) {
if ($exceptTokenType !== null && $token->isOfType($exceptTokenType)) {
Expand All @@ -33,7 +33,7 @@ public function next(?int $exceptTokenType = null) : ?Token
return null;
}

public function previous(?int $exceptTokenType = null) : ?Token
public function previous(?int $exceptTokenType = null): ?Token
{
while ($token = $this->tokens[--$this->position] ?? null) {
if ($exceptTokenType !== null && $token->isOfType($exceptTokenType)) {
Expand All @@ -46,7 +46,7 @@ public function previous(?int $exceptTokenType = null) : ?Token
return null;
}

public function subCursor() : self
public function subCursor(): self
{
$cursor = new self($this->tokens);
$cursor->position = $this->position;
Expand Down
8 changes: 4 additions & 4 deletions src/Highlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ interface Highlighter
/**
* Highlights a token depending on its type.
*/
public function highlightToken(int $type, string $value) : string;
public function highlightToken(int $type, string $value): string;

/**
* Highlights a token which causes an issue
*/
public function highlightError(string $value) : string;
public function highlightError(string $value): string;

/**
* Highlights an error message
*/
public function highlightErrorMessage(string $value) : string;
public function highlightErrorMessage(string $value): string;

/**
* Helper function for building string output
Expand All @@ -52,5 +52,5 @@ public function highlightErrorMessage(string $value) : string;
*
* @return string The quoted string
*/
public function output(string $string) : string;
public function output(string $string): string;
}
15 changes: 8 additions & 7 deletions src/HtmlHighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use function htmlentities;
use function sprintf;
use function trim;

use const ENT_COMPAT;
use const ENT_IGNORE;
use const PHP_EOL;
Expand Down Expand Up @@ -45,11 +46,11 @@ public function __construct(array $htmlAttributes = [], bool $usePre = true)
$this->usePre = $usePre;
}

public function highlightToken(int $type, string $value) : string
public function highlightToken(int $type, string $value): string
{
$value = htmlentities($value, ENT_COMPAT | ENT_IGNORE, 'UTF-8');

if ($type === Token::TOKEN_TYPE_BOUNDARY && ($value==='(' || $value===')')) {
if ($type === Token::TOKEN_TYPE_BOUNDARY && ($value === '(' || $value === ')')) {
return $value;
}

Expand All @@ -61,7 +62,7 @@ public function highlightToken(int $type, string $value) : string
return '<span ' . $attributes . '>' . $value . '</span>';
}

public function attributes(int $type) : ?string
public function attributes(int $type): ?string
{
if (! isset(self::TOKEN_TYPE_TO_HIGHLIGHT[$type])) {
return null;
Expand All @@ -70,7 +71,7 @@ public function attributes(int $type) : ?string
return $this->htmlAttributes[self::TOKEN_TYPE_TO_HIGHLIGHT[$type]];
}

public function highlightError(string $value) : string
public function highlightError(string $value): string
{
return sprintf(
'%s<span %s>%s</span>',
Expand All @@ -80,14 +81,14 @@ public function highlightError(string $value) : string
);
}

public function highlightErrorMessage(string $value) : string
public function highlightErrorMessage(string $value): string
{
return $this->highlightError($value);
}

public function output(string $string) : string
public function output(string $string): string
{
$string =trim($string);
$string = trim($string);
if (! $this->usePre) {
return $string;
}
Expand Down
8 changes: 4 additions & 4 deletions src/NullHighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@

final class NullHighlighter implements Highlighter
{
public function highlightToken(int $type, string $value) : string
public function highlightToken(int $type, string $value): string
{
return $value;
}

public function highlightError(string $value) : string
public function highlightError(string $value): string
{
return $value;
}

public function highlightErrorMessage(string $value) : string
public function highlightErrorMessage(string $value): string
{
return ' ' . $value;
}

public function output(string $string) : string
public function output(string $string): string
{
return $string;
}
Expand Down
69 changes: 40 additions & 29 deletions src/SqlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function str_replace;
use function strlen;
use function trim;

use const PHP_SAPI;

final class SqlFormatter
Expand All @@ -46,7 +47,7 @@ public function __construct(?Highlighter $highlighter = null)
*
* @return string The SQL string with HTML styles and formatting wrapped in a <pre> tag
*/
public function format(string $string, string $indentString = ' ') : string
public function format(string $string, string $indentString = ' '): string
{
// This variable will be populated with formatted html
$return = '';
Expand Down Expand Up @@ -148,7 +149,7 @@ public function format(string $string, string $indentString = ' ') : string
// Allow up to 3 non-whitespace tokens inside inline parentheses
$length = 0;
$subCursor = $cursor->subCursor();
for ($j=1; $j<=250; $j++) {
for ($j = 1; $j <= 250; $j++) {
// Reached end of string
$next = $subCursor->next(Token::TOKEN_TYPE_WHITESPACE);
if (! $next) {
Expand All @@ -164,17 +165,19 @@ public function format(string $string, string $indentString = ' ') : string
}

// Reached an invalid token for inline parentheses
if ($next->value()===';' || $next->value()==='(') {
if ($next->value() === ';' || $next->value() === '(') {
break;
}

// Reached an invalid token type for inline parentheses
if ($next->isOfType(
Token::TOKEN_TYPE_RESERVED_TOPLEVEL,
Token::TOKEN_TYPE_RESERVED_NEWLINE,
Token::TOKEN_TYPE_COMMENT,
Token::TOKEN_TYPE_BLOCK_COMMENT
)) {
if (
$next->isOfType(
Token::TOKEN_TYPE_RESERVED_TOPLEVEL,
Token::TOKEN_TYPE_RESERVED_NEWLINE,
Token::TOKEN_TYPE_COMMENT,
Token::TOKEN_TYPE_BLOCK_COMMENT
)
) {
break;
}

Expand Down Expand Up @@ -206,8 +209,8 @@ public function format(string $string, string $indentString = ' ') : string
$indentLevel--;

// Reset indent level
while ($j=array_shift($indentTypes)) {
if ($j!=='special') {
while ($j = array_shift($indentTypes)) {
if ($j !== 'special') {
break;
}

Expand All @@ -232,7 +235,7 @@ public function format(string $string, string $indentString = ' ') : string

// If the last indent type was 'special', decrease the special indent for this round
reset($indentTypes);
if (current($indentTypes)==='special') {
if (current($indentTypes) === 'special') {
$indentLevel--;
array_shift($indentTypes);
}
Expand All @@ -256,9 +259,11 @@ public function format(string $string, string $indentString = ' ') : string
if ($token->value() === 'LIMIT' && ! $inlineParentheses) {
$clauseLimit = true;
}
} elseif ($clauseLimit &&
} elseif (
$clauseLimit &&
$token->value() !== ',' &&
! $token->isOfType(Token::TOKEN_TYPE_NUMBER, Token::TOKEN_TYPE_WHITESPACE)) {
! $token->isOfType(Token::TOKEN_TYPE_NUMBER, Token::TOKEN_TYPE_WHITESPACE)
) {
// Checks if we are out of the limit clause
$clauseLimit = false;
} elseif ($token->value() === ',' && ! $inlineParentheses) {
Expand Down Expand Up @@ -294,9 +299,11 @@ public function format(string $string, string $indentString = ' ') : string
}

// If the token shouldn't have a space before it
if ($token->value() === '.' ||
if (
$token->value() === '.' ||
$token->value() === ',' ||
$token->value() === ';') {
$token->value() === ';'
) {
$return = rtrim($return, ' ');
}

Expand All @@ -322,12 +329,14 @@ public function format(string $string, string $indentString = ' ') : string
continue;
}

if ($prev->isOfType(
Token::TOKEN_TYPE_QUOTE,
Token::TOKEN_TYPE_BACKTICK_QUOTE,
Token::TOKEN_TYPE_WORD,
Token::TOKEN_TYPE_NUMBER
)) {
if (
$prev->isOfType(
Token::TOKEN_TYPE_QUOTE,
Token::TOKEN_TYPE_BACKTICK_QUOTE,
Token::TOKEN_TYPE_WORD,
Token::TOKEN_TYPE_NUMBER
)
) {
continue;
}

Expand Down Expand Up @@ -355,7 +364,7 @@ public function format(string $string, string $indentString = ' ') : string
*
* @return string The SQL string with HTML styles applied
*/
public function highlight(string $string) : string
public function highlight(string $string): string
{
$cursor = $this->tokenizer->tokenize($string);

Expand All @@ -378,7 +387,7 @@ public function highlight(string $string) : string
*
* @return string The SQL string without comments
*/
public function compress(string $string) : string
public function compress(string $string): string
{
$result = '';
$cursor = $this->tokenizer->tokenize($string);
Expand All @@ -392,11 +401,13 @@ public function compress(string $string) : string

// Remove extra whitespace in reserved words (e.g "OUTER JOIN" becomes "OUTER JOIN")

if ($token->isOfType(
Token::TOKEN_TYPE_RESERVED,
Token::TOKEN_TYPE_RESERVED_NEWLINE,
Token::TOKEN_TYPE_RESERVED_TOPLEVEL
)) {
if (
$token->isOfType(
Token::TOKEN_TYPE_RESERVED,
Token::TOKEN_TYPE_RESERVED_NEWLINE,
Token::TOKEN_TYPE_RESERVED_TOPLEVEL
)
) {
$newValue = preg_replace('/\s+/', ' ', $token->value());
assert($newValue !== null);
$token = $token->withValue($newValue);
Expand Down
12 changes: 6 additions & 6 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,29 @@ public function __construct(int $type, string $value)
$this->value = $value;
}

public function value() : string
public function value(): string
{
return $this->value;
}

public function type() : int
public function type(): int
{
return $this->type;
}

public function isOfType(int ...$types) : bool
public function isOfType(int ...$types): bool
{
return in_array($this->type, $types, true);
}

public function hasExtraWhitespace() : bool
public function hasExtraWhitespace(): bool
{
return strpos($this->value(), ' ')!== false ||
return strpos($this->value(), ' ') !== false ||
strpos($this->value(), "\n") !== false ||
strpos($this->value(), "\t") !== false;
}

public function withValue(string $value) : self
public function withValue(string $value): self
{
return new self($this->type(), $value);
}
Expand Down
Loading