classSqlHighlighter
final class SqlHighlighter
Renders a reconstructed statement as marked-up SQL.
The statement is rendered from the pattern rather than from the text it displays as, so a gap keeps what is known about it. A gap is a claim about the analysis and not about the statement, and rendering it as an ordinary {$} in the middle of the SQL hides which of the two a reader is looking at. Here it is marked, tinted by where the value came from, and says so when pointed at. The tokens are written in the classes doc-ui highlights.
Constants§
public const TOKENS = '/(?<com>--[^\n]*|\#[^\n]*|\/\*.*?\*\/)|(?<str>\'(?:\'\'|\\\\.|[^\'])*\'|"(?:""…How the tokens of a resolved run are told apart.
Methods§
public function __construct(?HtmlText $text = null)public function render(list<StatementPart> $parts): stringThe whole statement, with its known runs highlighted and its gaps marked.
Parameters
$parts | list<StatementPart> |
Returns
stringTest cases 2
Called from 4
- method-call SqlCatalog\Reporter\Html\Page\StatementPage::body() packages/sql-catalog/src/Reporter/Html/Page/StatementPage.php:145
- method-call SqlCatalog\Reporter\Html\Page\StatementPage::body() packages/sql-catalog/src/Reporter/Html/Page/StatementPage.php:147
- method-call SqlCatalog\Reporter\Html\SqlHighlighter::inline() packages/sql-catalog/src/Reporter/Html/SqlHighlighter.php:85
- method-call SqlCatalog\Reporter\Html\StatementRow::sql() packages/sql-catalog/src/Reporter/Html/StatementRow.php:99
Calls 2
- method-call SqlHighlighter::hole() line 57
- method-call SqlHighlighter::highlight() line 57
public function plain(list<StatementPart> $parts): stringThe displayed SQL without markup, for navigation and search labels.
Parameters
$parts | list<StatementPart> |
Returns
stringTest cases 1
Called from 2
Calls 3
- function-call
implodeline 70 - function-call
array_mapline 70 - method-call SqlHighlighter::holeLabel() line 70
public function inline(list<StatementPart> $parts): stringThe whole statement on one line, for a listing.
Parameters
$parts | list<StatementPart> |
Returns
stringTest cases 1
Calls 3
- new StatementPart line 82
- function-call
preg_replaceline 82 - method-call SqlHighlighter::render() line 85
public function highlight(string $sql): stringOne resolved run of SQL, with its tokens marked.
The run is tokenized before it is escaped, not after. Escaping first turns an apostrophe into an entity, and marking a token inside an entity breaks it, so what a reader sees is the escaping rather than the SQL.
Parameters
$sql | string |
Returns
stringTest cases 2
Called from 1
Calls 7
- function-call
preg_match_allline 97 - class-const SqlHighlighter::TOKENS() line 97
- method-call HtmlText::escape() line 98
- function-call
substrline 105 - method-call SqlHighlighter::token() line 105
- method-call SqlHighlighter::captured() line 105
- function-call
strlenline 106
public function captured(array<array-key, array{string, int}> $match): array<string, string>The named captures of a match, without the offsets they were captured with.
Parameters
$match | array<array-key, array{string, int}> |
Returns
array<string, string>Test cases 1
Called from 1
Calls 1
- function-call
is_stringline 122
public function token(array<string, string> $match): stringOne matched token, wrapped in the class its kind is written with.
Parameters
$match | array<string, string> |
Returns
stringTest cases 2
Called from 1
Calls 3
- class-const SqlHighlighter::TOKEN_CLASSES() line 139
- method-call HtmlText::escape() line 140
- method-call SqlHighlighter::isKeyword() line 145
public function isKeyword(string $word): boolWhether a word is one SQL writes as a keyword.
Parameters
$word | string |
Returns
boolTest cases 1
Called from 1
Calls 4
- function-call
in_arrayline 155 - function-call
strtoupperline 155 - function-call
explodeline 155 - class-const SqlHighlighter::KEYWORDS() line 155
public function hole(StatementPart $gap): stringOne gap, marked with where the value that fills it comes from.
Parameters
$gap | StatementPart |
Returns
stringTest cases 2
Called from 1
Calls 3
- method-call HtmlText::escape() line 168
- method-call SqlHighlighter::holeRole() line 168
- method-call SqlHighlighter::holeLabel() line 169
public function holeLabel(StatementPart $gap): stringA named PHP variable when known, or the anonymous gap marker.
Parameters
$gap | StatementPart |
Returns
stringTest cases 1
Called from 2
Calls 1
- function-call
preg_matchline 179
public function holeRole(string $origin): stringThe tone a gap of that origin is tinted by.
A value from outside the program is the one thing in a statement worth a warning colour; a call the analysis never reached is not a claim about the statement at all and is written in no colour; every other gap is a caution.
Parameters
$origin | string |
Returns
stringTest cases 1
Private surface 3§
Implementation details, listed for orientation only.
private const KEYWORDS = 'SELECT|INSERT|UPDATE|DELETE|REPLACE|MERGE|TRUNCATE|CREATE|ALTER|DROP|RENAME|CA…private const TOKEN_CLASSES = ['com' => 'tok-com', 'str' => 'tok-str', 'qid' => 'tok-id', 'ph' => 'tok-var', …private HtmlText $textTest cases 29§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 13
SqlHighlighterTest::testCapturedKeepsOnlyTheNamedCapturescallsSqlHighlighterTest::testHighlightEscapesWithoutBreakingWhatItEscapedcallsSqlHighlighterTest::testHighlightMarksCommentsNumbersAndPlaceholderscallsSqlHighlighterTest::testHoleLabelUsesOnlyAnIdentifiedVariableNamecallsSqlHighlighterTest::testHoleRoleTintsAGapByWhereItCameFromcallsSqlHighlighterTest::testHoleSaysWhereTheValueThatFillsItComesFromcallsSqlHighlighterTest::testInlineWritesTheStatementOnOneLinecallsSqlHighlighterTest::testIsKeywordRecognisesTheWordsSqlWritesAsKeywordscallsSqlHighlighterTest::testPlainUsesNamedGapsWithoutEscapingKnownTextcallsSqlHighlighterTest::testRenderMarksTheResolvedRunsAndTheGapsBetweenThemcallsSqlHighlighterTest::testRenderSaysSoWhenThereIsNothingToShowcallsSqlHighlighterTest::testTokenWritesAKeywordAsOnecallsSqlHighlighterTest::testTokenWritesAnUnknownWordAsPlainTextcalls
Other tests reaching this symbol 16
ReporterInterfaceTestcallsHtmlReporterTestcallsClassPageTestcallsFileIndexPageTestcallsFilePageTestcallsFindingPageTestcallsNamespacePageTestcallsOverviewPageTestcallsStatementIndexPageTestcallsStatementPageTestcallsTableIndexPageTestcallsTablePageTestcallsPageShellTestcallsSearchIndexTestcallsStatementListTestcallsStatementRowTestcalls
Relations§
Instantiated in 3
- new SqlCatalog\Reporter\Html\Page\StatementPage::__construct() packages/sql-catalog/src/Reporter/Html/Page/StatementPage.php:65
- new SqlCatalog\Reporter\Html\SearchIndex::entryToArray() packages/sql-catalog/src/Reporter/Html/SearchIndex.php:59
- new SqlCatalog\Reporter\Html\StatementRow::__construct() packages/sql-catalog/src/Reporter/Html/StatementRow.php:40
Method calls 5
- method-call SqlCatalog\Reporter\Html\Page\StatementPage::body() packages/sql-catalog/src/Reporter/Html/Page/StatementPage.php:145
- method-call SqlCatalog\Reporter\Html\Page\StatementPage::body() packages/sql-catalog/src/Reporter/Html/Page/StatementPage.php:147
- method-call SqlCatalog\Reporter\Html\Page\StatementPage::context() packages/sql-catalog/src/Reporter/Html/Page/StatementPage.php:292
- method-call SqlCatalog\Reporter\Html\SearchIndex::entryToArray() packages/sql-catalog/src/Reporter/Html/SearchIndex.php:59
- method-call SqlCatalog\Reporter\Html\StatementRow::sql() packages/sql-catalog/src/Reporter/Html/StatementRow.php:99
Constant reads 1
Type declarations 4
- type SqlCatalog\Reporter\Html\Page\StatementPage packages/sql-catalog/src/Reporter/Html/Page/StatementPage.php:46
- type SqlCatalog\Reporter\Html\Page\StatementPage packages/sql-catalog/src/Reporter/Html/Page/StatementPage.php:59
- type SqlCatalog\Reporter\Html\StatementRow packages/sql-catalog/src/Reporter/Html/StatementRow.php:28
- type SqlCatalog\Reporter\Html\StatementRow packages/sql-catalog/src/Reporter/Html/StatementRow.php:37