classFormatter
final class Formatter
Public API: explicitly declared with
@visibility public.Formats SQL with the dialect, release, and lexical settings of a supplied parser.
Compact canonicalizes optional syntax and keywords; other styles preserve tokens. Output is parsed again to verify the selected style's preservation contract.
$formatter = new \SqlFormatter\Facade\Formatter(new \SqlParser\Sqlite\SqliteParser());
$formatter->format('SELECT id,name FROM users') // => "SELECT\n id,\n name\nFROM\n users"$formatter = new \SqlFormatter\Facade\Formatter(new \SqlParser\MySql\MySqlParser(), new \SqlFormatter\Core\FormatOptions(\SqlFormatter\Core\Style::Compact));
$formatter->format("SELECT id, name\nFROM users") // => 'SELECT id,name FROM users'Methods§
public function __construct(
SqlParser $parser,
FormatOptions $options = new \SqlFormatter\Core\FormatOptions(),
?Dialect $dialect = null,
)Selects built-in formatting rules or accepts caller-supplied rules.
Parameters
$parser | SqlParser | |
$options | FormatOptions | |
$dialect | ?Dialect |
Calls 3
- new FormatOptions line 30
- new Formatter line 33
- static-call DialectFactory::forParser() line 33
public function format(string $sql): stringFormats SQL using the configured parser, grammar rules, and layout.
Parameters
$sql | string |
Returns
stringThrows
SourceException When the input is not acceptedFormattingException When output verification failsTest cases 21
FormatterBench::benchCompactcallsFormatterBench::benchFormatcallsFormatTarget::verifycallsMySqlEquivalence::verifycallsPgEquivalence::verifycallsRoundTripTarget::verifycallsSchemaTarget::verifycallsSqliteEquivalence::verifycallsFormatterTest::testFormatEveryGrammarReleasecallsFormatterTest::testFormatExpandedStructurescallsFormatterTest::testFormatFourLayoutscallsFormatterTest::testFormatGeneratedStatementscallsFormatterTest::testFormatKeepsHostnamesAdjacentToAtSignscallsFormatterTest::testFormatNestedCteWithConfiguredIndentcallsFormatterTest::testFormatPreservesMysqlSqlModecallsFormatterTest::testFormatPreservesTokensAndIsIdempotentcallsFormatterTest::testFormatRejectsInvalidInputcallsFormatterTest::testFormatRemainsCanonicalAcrossLayoutscallsPlatformTest::testAnalyzeFakerRegressionscallsPlatformTest::testAnalyzePreservesKeywordNamesBeforeDotscallsPlatformTest::testAnalyzeRoundTripsCompleteStatementscalls
Calls 1
- method-call Formatter::format() line 44
Private surface 1§
Implementation details, listed for orientation only.
private Formatter $formatterTest cases 23§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 10
FormatterTest::testFormatEveryGrammarReleasecallsFormatterTest::testFormatExpandedStructurescallsFormatterTest::testFormatFourLayoutscallsFormatterTest::testFormatGeneratedStatementscallsFormatterTest::testFormatKeepsHostnamesAdjacentToAtSignscallsFormatterTest::testFormatNestedCteWithConfiguredIndentcallsFormatterTest::testFormatPreservesMysqlSqlModecallsFormatterTest::testFormatPreservesTokensAndIsIdempotentcallsFormatterTest::testFormatRejectsInvalidInputcallsFormatterTest::testFormatRemainsCanonicalAcrossLayoutscalls
Other tests reaching this symbol 13
FormatterBench::benchCompactcallsFormatterBench::benchFormatcallsFormatterBench::setUpcallsFormatterBench::setUpCompactcallsFormatTarget::verifycallsMySqlEquivalence::verifycallsPgEquivalence::verifycallsRoundTripTarget::verifycallsSchemaTarget::verifycallsSqliteEquivalence::verifycallsPlatformTest::testAnalyzeFakerRegressionscallsPlatformTest::testAnalyzePreservesKeywordNamesBeforeDotscallsPlatformTest::testAnalyzeRoundTripsCompleteStatementscalls