Skip to content

Commit 735a373

Browse files
authored
Merge pull request #25 from greg0ire/null-highlighter
Use null object pattern to decrease complexity
2 parents 94fedae + 7de1594 commit 735a373

File tree

8 files changed

+62
-35
lines changed

8 files changed

+62
-35
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ Output:
4949
### Formatting Only
5050

5151
If you don't want syntax highlighting and only want the indentations and
52-
line breaks, pass in `false` as the second parameter.
52+
line breaks, pass in a `NullHighlighter` instance as the second parameter.
5353

5454
This is useful for outputting to error logs or other non-html formats.
5555

5656
```php
5757
<?php
58-
echo (new SqlFormatter())->format($query, false);
58+
59+
use Doctrine\SqlFormatter\NullHighlighter;
60+
use Doctrine\SqlFormatter\SqlFormatter;
61+
62+
echo (new SqlFormatter(new NullHighlighter()))->format($query);
5963
```
6064

6165
Output:

examples/examples.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
require_once __DIR__ . '/../vendor/autoload.php';
66

7+
use Doctrine\SqlFormatter\NullHighlighter;
78
use Doctrine\SqlFormatter\SqlFormatter;
9+
use Doctrine\SqlFormatter\Tokenizer;
810

911
$formatter = new SqlFormatter();
1012

@@ -177,9 +179,9 @@
177179
<div>
178180
Usage:
179181
<pre>
180-
<?php highlight_string(
181-
'<?php' . "\n" . '$formatted = (new SqlFormatter())->format($sql, false);' . "\n" . '?>'
182-
); ?>
182+
<?php highlight_string('<?php' . "\n" .
183+
'$formatted = (new SqlFormatter(mew Tokenizer(), new NullHighlighter()))->format($sql);' .
184+
"\n" . '?>'); ?>
183185
</pre>
184186
</div>
185187
<table>
@@ -192,7 +194,10 @@
192194
<td>
193195
<pre><?= $sql; ?></pre>
194196
</td>
195-
<td><pre><?= htmlentities($formatter->format($sql, false)); ?></pre></td>
197+
<td><pre><?= htmlentities((new SqlFormatter(
198+
new Tokenizer(),
199+
new NullHighlighter()
200+
))->format($sql)); ?></pre></td>
196201
</tr>
197202
<?php endforeach ?>
198203
</table>

src/CliHighlighter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Doctrine\SqlFormatter;
66

7+
use function sprintf;
8+
use const PHP_EOL;
9+
710
final class CliHighlighter implements Highlighter
811
{
912
/** @var string */
@@ -79,7 +82,7 @@ private function prefix(int $type) : ?string
7982

8083
public function highlightError(string $value) : string
8184
{
82-
return $this->cliError . $value . "\x1b[0m";
85+
return sprintf('%s%s%s%s', PHP_EOL, $this->cliError, $value, "\x1b[0m");
8386
}
8487

8588
public function output(string $string) : string

src/HtmlHighlighter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
namespace Doctrine\SqlFormatter;
66

77
use function htmlentities;
8+
use function sprintf;
89
use function trim;
910
use const ENT_COMPAT;
1011
use const ENT_IGNORE;
12+
use const PHP_EOL;
1113

1214
final class HtmlHighlighter implements Highlighter
1315
{
@@ -94,7 +96,7 @@ public function attributes(int $type) : ?string
9496

9597
public function highlightError(string $value) : string
9698
{
97-
return '<span ' . $this->errorAttributes . '>' . $value . '</span>';
99+
return sprintf('%s<span %s>%s</span>', PHP_EOL, $this->errorAttributes, $value);
98100
}
99101

100102
public function output(string $string) : string

src/NullHighlighter.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\SqlFormatter;
6+
7+
final class NullHighlighter implements Highlighter
8+
{
9+
public function highlightToken(int $type, string $value) : string
10+
{
11+
return $value;
12+
}
13+
14+
public function highlightError(string $value) : string
15+
{
16+
return $value;
17+
}
18+
19+
public function output(string $string) : string
20+
{
21+
return $string;
22+
}
23+
}

src/SqlFormatter.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,11 @@ public function __construct(
6060
/**
6161
* Format the whitespace in a SQL string to make it easier to read.
6262
*
63-
* @param string $string The SQL string
64-
* @param bool $highlight If true, syntax highlighting will also be performed
63+
* @param string $string The SQL string
6564
*
6665
* @return string The SQL string with HTML styles and formatting wrapped in a <pre> tag
6766
*/
68-
public function format(string $string, bool $highlight = true) : string
67+
public function format(string $string) : string
6968
{
7069
// This variable will be populated with formatted html
7170
$return = '';
@@ -99,13 +98,11 @@ public function format(string $string, bool $highlight = true) : string
9998

10099
// Format token by token
101100
foreach ($indexedTokens as $i => $indexedToken) {
102-
$token = $indexedToken['token'];
103-
// Get highlighted token if doing syntax highlighting
104-
if ($highlight) {
105-
$highlighted = $this->highlighter->highlightToken($token->type(), $token->value());
106-
} else { // If returning raw text
107-
$highlighted = $token->value();
108-
}
101+
$token = $indexedToken['token'];
102+
$highlighted = $this->highlighter->highlightToken(
103+
$token->type(),
104+
$token->value()
105+
);
109106

110107
// If we are increasing the special indent level now
111108
if ($increaseSpecialIndent) {
@@ -247,12 +244,8 @@ public function format(string $string, bool $highlight = true) : string
247244
// This is an error
248245
$indentLevel = 0;
249246

250-
if ($highlight) {
251-
$return .= "\n" . $this->highlighter->highlightError(
252-
$token->value()
253-
);
254-
continue;
255-
}
247+
$return .= $this->highlighter->highlightError($token->value());
248+
continue;
256249
}
257250

258251
// Add a newline before the closing parentheses (if not already added)
@@ -360,18 +353,14 @@ public function format(string $string, bool $highlight = true) : string
360353
}
361354

362355
// If there are unmatched parentheses
363-
if ($highlight && array_search('block', $indentTypes) !== false) {
364-
$return .= "\n" . $this->highlighter->highlightError('WARNING: unclosed parentheses or section');
356+
if (array_search('block', $indentTypes) !== false) {
357+
$return .= $this->highlighter->highlightError('WARNING: unclosed parentheses or section');
365358
}
366359

367360
// Replace tab characters with the configuration tab character
368361
$return = trim(str_replace("\t", $this->tab, $return));
369362

370-
if ($highlight) {
371-
$return = $this->highlighter->output($return);
372-
}
373-
374-
return $return;
363+
return $this->highlighter->output($return);
375364
}
376365

377366
/**

tests/SqlFormatterTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\SqlFormatter\CliHighlighter;
88
use Doctrine\SqlFormatter\HtmlHighlighter;
9+
use Doctrine\SqlFormatter\NullHighlighter;
910
use Doctrine\SqlFormatter\SqlFormatter;
1011
use Doctrine\SqlFormatter\Tokenizer;
1112
use PHPUnit\Framework\TestCase;
@@ -59,7 +60,8 @@ public function testFormatHighlight(string $sql, string $html) : void
5960
*/
6061
public function testFormat(string $sql, string $html) : void
6162
{
62-
$this->assertEquals(trim($html), trim($this->formatter->format($sql, false)));
63+
$formatter = new SqlFormatter(new Tokenizer(), new NullHighlighter());
64+
$this->assertEquals(trim($html), trim($formatter->format($sql)));
6365
}
6466

6567
/**

tests/format.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,11 +750,10 @@
750750
FROM
751751
Test
752752
WHERE
753-
(MyColumn = 1)
754-
)
753+
(MyColumn = 1))
755754
AND (
756755
(
757-
(SomeOtherColumn = 2);
756+
(SomeOtherColumn = 2); WARNING: unclosed parentheses or section
758757

759758
SELECT
760759
*

0 commit comments

Comments
 (0)