From acb59a3e585b8e9e0b58190932d0308493fc8296 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 8 Apr 2021 19:17:53 +0100 Subject: [PATCH 1/4] A few quick formatting tweaks --- src/SqlFormatter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SqlFormatter.php b/src/SqlFormatter.php index 624c3bb..4fe4c46 100644 --- a/src/SqlFormatter.php +++ b/src/SqlFormatter.php @@ -6,7 +6,7 @@ * SQL Formatter is a collection of utilities for debugging SQL queries. * It includes methods for formatting, syntax highlighting, removing comments, etc. * - * @link http://github.com/jdorn/sql-formatter + * @see http://github.com/jdorn/sql-formatter */ namespace Doctrine\SqlFormatter; @@ -36,7 +36,7 @@ final class SqlFormatter public function __construct(?Highlighter $highlighter = null) { - $this->tokenizer = new Tokenizer(); + $this->tokenizer = new Tokenizer(); $this->highlighter = $highlighter ?? (PHP_SAPI === 'cli' ? new CliHighlighter() : new HtmlHighlighter()); } From b309a3161b0367bb879bbbab707e58afb63b7f81 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 8 Apr 2021 20:44:42 +0100 Subject: [PATCH 2/4] Add uppercase auto-formatting as a passable option to the format method, ensuring the implementation is backwards compatible. Abstracted from the fork source PR https://github.com/jdorn/sql-formatter/pull/86 --- src/SqlFormatter.php | 18 ++++++++++++++++-- tests/SqlFormatterTest.php | 10 ++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/SqlFormatter.php b/src/SqlFormatter.php index 4fe4c46..009cdc3 100644 --- a/src/SqlFormatter.php +++ b/src/SqlFormatter.php @@ -47,7 +47,7 @@ public function __construct(?Highlighter $highlighter = null) * * @return string The SQL string with HTML styles and formatting wrapped in a
 tag
      */
-    public function format(string $string, string $indentString = '  '): string
+    public function format(string $string, string $indentString = '  ', bool $forceUppercase = false): string
     {
         // This variable will be populated with formatted html
         $return = '';
@@ -71,9 +71,23 @@ public function format(string $string, string $indentString = '  '): string
 
         // Format token by token
         while ($token = $cursor->next(Token::TOKEN_TYPE_WHITESPACE)) {
+            // Uppercase reserved words
+            $uppercaseTypes = [
+                Token::TOKEN_TYPE_RESERVED,
+                Token::TOKEN_TYPE_RESERVED_NEWLINE,
+                Token::TOKEN_TYPE_RESERVED_TOPLEVEL
+            ];
+
+            // Uppercase transformation if desired
+            if ($forceUppercase && in_array($token->type(), $uppercaseTypes)) {                
+                $tokenValue = strtoupper($token->value());
+            } else {
+                $tokenValue = $token->value();
+            }
+
             $highlighted = $this->highlighter->highlightToken(
                 $token->type(),
-                $token->value()
+                $tokenValue
             );
 
             // If we are increasing the special indent level now
diff --git a/tests/SqlFormatterTest.php b/tests/SqlFormatterTest.php
index 68d90cd..4edb9d9 100644
--- a/tests/SqlFormatterTest.php
+++ b/tests/SqlFormatterTest.php
@@ -60,6 +60,16 @@ public function testFormat(string $sql, string $html): void
         $this->assertEquals(trim($html), trim($formatter->format($sql)));
     }
 
+    public function testFormatUpperCase(): void
+    {
+        $formatter = new SqlFormatter(new NullHighlighter());
+
+        $actual = $formatter->format('select a from b where c = d;', ' ', true);
+        $expected = "SELECT\n a\nFROM\n b\nWHERE\n c = d;";
+
+        $this->assertEquals(trim($expected), trim($actual));
+    }
+
     /**
      * @dataProvider highlightData
      */

From abba52b917920930e5287ec1735a95576a4c5f60 Mon Sep 17 00:00:00 2001
From: Ben 
Date: Thu, 8 Apr 2021 20:54:19 +0100
Subject: [PATCH 3/4] Fix code standards outlined by CONTRIBUTING.md: $ echo ''
 | vendor/bin/phpcs

---
 src/SqlFormatter.php       | 8 +++++---
 tests/SqlFormatterTest.php | 2 +-
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/SqlFormatter.php b/src/SqlFormatter.php
index 009cdc3..b0440e8 100644
--- a/src/SqlFormatter.php
+++ b/src/SqlFormatter.php
@@ -16,12 +16,14 @@
 use function array_unshift;
 use function assert;
 use function current;
+use function in_array;
 use function preg_replace;
 use function reset;
 use function rtrim;
 use function str_repeat;
 use function str_replace;
 use function strlen;
+use function strtoupper;
 use function trim;
 
 use const PHP_SAPI;
@@ -36,7 +38,7 @@ final class SqlFormatter
 
     public function __construct(?Highlighter $highlighter = null)
     {
-        $this->tokenizer = new Tokenizer();
+        $this->tokenizer   = new Tokenizer();
         $this->highlighter = $highlighter ?? (PHP_SAPI === 'cli' ? new CliHighlighter() : new HtmlHighlighter());
     }
 
@@ -75,11 +77,11 @@ public function format(string $string, string $indentString = '  ', bool $forceU
             $uppercaseTypes = [
                 Token::TOKEN_TYPE_RESERVED,
                 Token::TOKEN_TYPE_RESERVED_NEWLINE,
-                Token::TOKEN_TYPE_RESERVED_TOPLEVEL
+                Token::TOKEN_TYPE_RESERVED_TOPLEVEL,
             ];
 
             // Uppercase transformation if desired
-            if ($forceUppercase && in_array($token->type(), $uppercaseTypes)) {                
+            if ($forceUppercase && in_array($token->type(), $uppercaseTypes)) {
                 $tokenValue = strtoupper($token->value());
             } else {
                 $tokenValue = $token->value();
diff --git a/tests/SqlFormatterTest.php b/tests/SqlFormatterTest.php
index 4edb9d9..f5c8aed 100644
--- a/tests/SqlFormatterTest.php
+++ b/tests/SqlFormatterTest.php
@@ -64,7 +64,7 @@ public function testFormatUpperCase(): void
     {
         $formatter = new SqlFormatter(new NullHighlighter());
 
-        $actual = $formatter->format('select a from b where c = d;', ' ', true);
+        $actual   = $formatter->format('select a from b where c = d;', ' ', true);
         $expected = "SELECT\n a\nFROM\n b\nWHERE\n c = d;";
 
         $this->assertEquals(trim($expected), trim($actual));

From 996bd56b374a3e5c8cf82b61cdc7107701470a20 Mon Sep 17 00:00:00 2001
From: Ben T 
Date: Thu, 8 Apr 2021 21:58:01 +0100
Subject: [PATCH 4/4] Document the uppercase behaviour in the readme

---
 README.md | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/README.md b/README.md
index 767d6de..7ca5041 100644
--- a/README.md
+++ b/README.md
@@ -62,6 +62,19 @@ use Doctrine\SqlFormatter\SqlFormatter;
 echo (new SqlFormatter(new NullHighlighter()))->format($query);
 ```
 
+#### Forcing uppercase for SQL keywords
+
+If you wish to also force SQL keywords to be uppercase, you can do the following:
+
+```php
+format($query, '  ', true);
+```
+
 Output:
 
 ![](http://jdorn.github.com/sql-formatter/format.png)