LiteralString::isFunction()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 3
eloc 3
nc 2
nop 3
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 12/23/14
5
 * Time: 1:36 PM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Sql\QueryFormatter\Tokenizer\Parser;
12
13
use NilPortugues\Sql\QueryFormatter\Tokenizer\Tokenizer;
14
15
/**
16
 * Class LiteralString.
17
 */
18
final class LiteralString
19
{
20
    /**
21
     * @param Tokenizer $tokenizer
22
     * @param string    $string
23
     * @param array     $matches
24
     */
25
    public static function isFunction(Tokenizer $tokenizer, $string, array &$matches)
26
    {
27
        if (!$tokenizer->getNextToken() && self::isFunctionString($string, $matches, $tokenizer->getRegexFunction())) {
28
            $tokenizer->setNextToken(self::getFunctionString($string, $matches));
29
        }
30
    }
31
32
    /**
33
     * A function must be succeeded by '('.
34
     * This makes it so that a function such as "COUNT(" is considered a function, but "COUNT" alone is not function.
35
     *
36
     * @param string $string
37
     * @param array  $matches
38
     * @param string $regexFunction
39
     *
40
     * @return bool
41
     */
42
    protected static function isFunctionString($string, array &$matches, $regexFunction)
43
    {
44
        return (1 == \preg_match('/^('.$regexFunction.'[(]|\s|[)])/', \strtoupper($string), $matches));
45
    }
46
47
    /**
48
     * @param string $string
49
     * @param array  $matches
50
     *
51
     * @return array
52
     */
53 View Code Duplication
    protected static function getFunctionString($string, array &$matches)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        return [
56
            Tokenizer::TOKEN_TYPE => Tokenizer::TOKEN_TYPE_RESERVED,
57
            Tokenizer::TOKEN_VALUE => \substr($string, 0, \strlen($matches[1]) - 1),
58
        ];
59
    }
60
61
    /**
62
     * @param Tokenizer $tokenizer
63
     * @param string    $string
64
     * @param array     $matches
65
     */
66
    public static function getNonReservedString(Tokenizer $tokenizer, $string, array &$matches)
67
    {
68
        if (!$tokenizer->getNextToken()) {
69
            $data = [];
70
71
            if (1 == \preg_match('/^(.*?)($|\s|["\'`]|'.$tokenizer->getRegexBoundaries().')/', $string, $matches)) {
72
                $data = [
73
                    Tokenizer::TOKEN_VALUE => $matches[1],
74
                    Tokenizer::TOKEN_TYPE => Tokenizer::TOKEN_TYPE_WORD,
75
                ];
76
            }
77
78
            $tokenizer->setNextToken($data);
79
        }
80
    }
81
}
82