packages/sql-faker/src/MySql/Generation/Spacing/FunctionSpacingRule.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlFaker\MySql\Generation\Spacing;
6
7use Override;
8use SqlFaker\Generation\Lexeme\LexemeBoundary;
9use SqlFaker\Generation\Lexeme\LexemeInput;
10use SqlFaker\Generation\Lexeme\SpacingConstraint;
11use SqlFaker\Generation\Lexeme\SpacingRule;
12
13/**
14 * sql/sql_lex.cc MY_LEX_IDENT: SYM_FN lookup requires a directly following parenthesis.
15 * @see https://github.com/mysql/mysql-server/blob/mysql-8.4.7/sql/sql_lex.cc
16 */
17final class FunctionSpacingRule implements SpacingRule
18{
19    /**
20     * Applies the chosen lexeme's use rather than inferring function names from their text.
21     */
22    #[Override]
23    public function apply(LexemeBoundary $boundary, LexemeInput $input): ?SpacingConstraint
24    {
25        if ($boundary->right->text !== '(') {
26            return null;
27        }
28        if ($boundary->left->kind === 'function') {
29            return new SpacingConstraint(SpacingConstraint::JOIN, ['mysql.function-parenthesis']);
30        }
31        return $boundary->left->kind === 'identifier'
32            ? new SpacingConstraint(SpacingConstraint::SPACE, ['mysql.identifier-parenthesis']) : null;
33    }
34}
35