WhiteSpace   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 17
c 1
b 1
f 1
lcom 0
cbo 0
dl 0
loc 83
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A tokenHasExtraWhiteSpaceLeft() 0 7 3
A tokenHasExtraWhiteSpaceRight() 0 6 2
A tokenIsNumberAndHasExtraWhiteSpaceRight() 0 8 4
A tokenHasExtraWhiteSpaces() 0 6 3
A isPrecedingCurrentTokenOfTokenTypeWhiteSpace() 0 5 2
A removeTokenWhitespace() 0 12 3
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 12/22/14
5
 * Time: 1:19 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\Helper;
12
13
use NilPortugues\Sql\QueryFormatter\Tokenizer\Tokenizer;
14
15
/**
16
 * Class WhiteSpace.
17
 */
18
class WhiteSpace
19
{
20
    /**
21
     * @param $token
22
     *
23
     * @return bool
24
     */
25
    public static function tokenHasExtraWhiteSpaceLeft($token)
26
    {
27
        return
28
            $token[Tokenizer::TOKEN_VALUE] === '.'
29
            || $token[Tokenizer::TOKEN_VALUE] === ','
30
            || $token[Tokenizer::TOKEN_VALUE] === ';';
31
    }
32
33
    /**
34
     * @param $token
35
     *
36
     * @return bool
37
     */
38
    public static function tokenHasExtraWhiteSpaceRight($token)
39
    {
40
        return
41
            $token[Tokenizer::TOKEN_VALUE] === '('
42
            || $token[Tokenizer::TOKEN_VALUE] === '.';
43
    }
44
45
    /**
46
     * @param $tokenType
47
     *
48
     * @return bool
49
     */
50
    public static function tokenIsNumberAndHasExtraWhiteSpaceRight($tokenType)
51
    {
52
        return
53
            $tokenType !== Tokenizer::TOKEN_TYPE_QUOTE
54
            && $tokenType !== Tokenizer::TOKEN_TYPE_BACK_TICK_QUOTE
55
            && $tokenType !== Tokenizer::TOKEN_TYPE_WORD
56
            && $tokenType !== Tokenizer::TOKEN_TYPE_NUMBER;
57
    }
58
59
    /**
60
     * @param $token
61
     *
62
     * @return bool
63
     */
64
    public static function tokenHasExtraWhiteSpaces($token)
65
    {
66
        return \strpos($token[Tokenizer::TOKEN_VALUE], ' ') !== false
67
        || \strpos($token[Tokenizer::TOKEN_VALUE], "\n") !== false
68
        || \strpos($token[Tokenizer::TOKEN_VALUE], "\t") !== false;
69
    }
70
71
    /**
72
     * @param $originalTokens
73
     * @param $token
74
     *
75
     * @return bool
76
     */
77
    public static function isPrecedingCurrentTokenOfTokenTypeWhiteSpace($originalTokens, $token)
78
    {
79
        return isset($originalTokens[$token['i'] - 1])
80
        && $originalTokens[$token['i'] - 1][Tokenizer::TOKEN_TYPE] !== Tokenizer::TOKEN_TYPE_WHITESPACE;
81
    }
82
83
    /**
84
     * @param $originalTokens
85
     *
86
     * @return array
87
     */
88
    public static function removeTokenWhitespace(array &$originalTokens)
89
    {
90
        $tokens = [];
91
        foreach ($originalTokens as $i => &$token) {
92
            if ($token[Tokenizer::TOKEN_TYPE] !== Tokenizer::TOKEN_TYPE_WHITESPACE) {
93
                $token['i'] = $i;
94
                $tokens[] = $token;
95
            }
96
        }
97
98
        return $tokens;
99
    }
100
}
101