Reserved::getReservedString()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4286
cc 3
eloc 9
nc 2
nop 4
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 12/23/14
5
 * Time: 1:18 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 Reserved.
17
 */
18
final class Reserved
19
{
20
    /**
21
     * @var array
22
     */
23
    protected static $regex = [
24
        Tokenizer::TOKEN_TYPE_RESERVED_TOP_LEVEL => 'getRegexReservedTopLevel',
25
        Tokenizer::TOKEN_TYPE_RESERVED_NEWLINE => 'getRegexReservedNewLine',
26
        Tokenizer::TOKEN_TYPE_RESERVED => 'getRegexReserved',
27
    ];
28
29
    /**
30
     * @param Tokenizer  $tokenizer
31
     * @param string     $string
32
     * @param array|null $previous
33
     *
34
     * @return array
35
     */
36
    public static function isReserved(Tokenizer $tokenizer, $string, $previous)
37
    {
38
        $tokenData = [];
39
40
        if (!$tokenizer->getNextToken() && self::isReservedPrecededByDotCharacter($previous)) {
41
            $upperCase = \strtoupper($string);
42
43
            self::getReservedString($tokenData, Tokenizer::TOKEN_TYPE_RESERVED_TOP_LEVEL, $string, $tokenizer);
44
            self::getReservedString($tokenData, Tokenizer::TOKEN_TYPE_RESERVED_NEWLINE, $upperCase, $tokenizer);
45
            self::getReservedString($tokenData, Tokenizer::TOKEN_TYPE_RESERVED, $string, $tokenizer);
46
47
            $tokenizer->setNextToken($tokenData);
48
        }
49
    }
50
51
    /**
52
     * A reserved word cannot be preceded by a "." in order to differentiate "mytable.from" from the token "from".
53
     *
54
     * @param $previous
55
     *
56
     * @return bool
57
     */
58
    protected static function isReservedPrecededByDotCharacter($previous)
59
    {
60
        return !$previous || !isset($previous[Tokenizer::TOKEN_VALUE]) || $previous[Tokenizer::TOKEN_VALUE] !== '.';
61
    }
62
63
    /**
64
     * @param array     $tokenData
65
     * @param           $type
66
     * @param string    $string
67
     * @param Tokenizer $tokenizer
68
     */
69
    protected static function getReservedString(array &$tokenData, $type, $string, Tokenizer $tokenizer)
70
    {
71
        $matches = [];
72
        $method = self::$regex[$type];
73
74
        if (empty($tokenData) && self::isReservedString(
75
                $string,
76
                $matches,
77
                $tokenizer->$method(),
78
                $tokenizer->getRegexBoundaries()
79
            )
80
        ) {
81
            $tokenData = self::getStringTypeArray($type, $string, $matches);
82
        }
83
    }
84
85
    /**
86
     * @param string $upper
87
     * @param array  $matches
88
     * @param string $regexReserved
89
     * @param string $regexBoundaries
90
     *
91
     * @return bool
92
     */
93
    protected static function isReservedString($upper, array &$matches, $regexReserved, $regexBoundaries)
94
    {
95
        return 1 == \preg_match(
96
            '/^('.$regexReserved.')($|\s|'.$regexBoundaries.')/',
97
            \strtoupper($upper),
98
            $matches
99
        );
100
    }
101
102
    /**
103
     * @param string $type
104
     * @param string $string
105
     * @param array  $matches
106
     *
107
     * @return array
108
     */
109 View Code Duplication
    protected static function getStringTypeArray($type, $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...
110
    {
111
        return [
112
            Tokenizer::TOKEN_TYPE => $type,
113
            Tokenizer::TOKEN_VALUE => \substr($string, 0, \strlen($matches[1])),
114
        ];
115
    }
116
}
117