writeNewLineBecauseOfTopLevelReservedWord()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 11
rs 9.4286
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 12/22/14
5
 * Time: 11:37 AM.
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\Formatter;
14
use NilPortugues\Sql\QueryFormatter\Tokenizer\Tokenizer;
15
16
/**
17
 * Class NewLine.
18
 */
19
class NewLine
20
{
21
    /**
22
     * @var bool
23
     */
24
    protected $newline = false;
25
26
    /**
27
     * @var \NilPortugues\Sql\QueryFormatter\Formatter
28
     */
29
    protected $formatter;
30
31
    /**
32
     * @var Indent
33
     */
34
    protected $indentation;
35
36
    /**
37
     * @var Parentheses
38
     */
39
    protected $parentheses;
40
41
    /**
42
     * @param Formatter   $formatter
43
     * @param Indent      $indentation
44
     * @param Parentheses $parentheses
45
     */
46
    public function __construct(Formatter $formatter, Indent $indentation, Parentheses $parentheses)
47
    {
48
        $this->formatter = $formatter;
49
        $this->indentation = $indentation;
50
        $this->parentheses = $parentheses;
51
    }
52
53
    /**
54
     * Adds a new line break if needed.
55
     *
56
     * @param string $tab
57
     *
58
     * @return bool
59
     */
60
    public function addNewLineBreak($tab)
61
    {
62
        $addedNewline = false;
63
64
        if (true === $this->newline) {
65
            $this->formatter->appendToFormattedSql("\n".str_repeat($tab, $this->indentation->getIndentLvl()));
66
            $this->newline = false;
67
            $addedNewline = true;
68
        }
69
70
        return $addedNewline;
71
    }
72
73
    /**
74
     * @param $token
75
     */
76
    public function writeNewLineForLongCommaInlineValues($token)
77
    {
78
        if (',' === $token[Tokenizer::TOKEN_VALUE]) {
79
            if ($this->formatter->getInlineCount() >= 30) {
80
                $this->formatter->setInlineCount(0);
81
                $this->newline = true;
82
            }
83
        }
84
    }
85
86
    /**
87
     * @param int $length
88
     */
89 View Code Duplication
    public function writeNewLineForLongInlineValues($length)
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...
90
    {
91
        if ($this->parentheses->getInlineParentheses() && $length > 30) {
92
            $this->indentation->setIncreaseBlockIndent(true);
93
            $this->indentation->setInlineIndented(true);
94
            $this->newline = true;
95
        }
96
    }
97
98
    /**
99
     * Adds a new line break for an opening parentheses for a non-inline expression.
100
     */
101 View Code Duplication
    public function addNewLineAfterOpeningParentheses()
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...
102
    {
103
        if (false === $this->parentheses->getInlineParentheses()) {
104
            $this->indentation->setIncreaseBlockIndent(true);
105
            $this->newline = true;
106
        }
107
    }
108
109
    /**
110
     * @param bool   $addedNewline
111
     * @param string $tab
112
     */
113
    public function addNewLineBeforeToken($addedNewline, $tab)
114
    {
115
        if (false === $addedNewline) {
116
            $this->formatter->appendToFormattedSql(
117
                "\n".str_repeat($tab, $this->indentation->getIndentLvl())
118
            );
119
        }
120
    }
121
122
    /**
123
     * Add a newline before the top level reserved word if necessary and indent.
124
     *
125
     * @param bool   $addedNewline
126
     * @param string $tab
127
     */
128
    public function writeNewLineBecauseOfTopLevelReservedWord($addedNewline, $tab)
129
    {
130
        if (false === $addedNewline) {
131
            $this->formatter->appendToFormattedSql("\n");
132
        } else {
133
            $this->formatter->setFormattedSql(\rtrim($this->formatter->getFormattedSql(), $tab));
134
        }
135
        $this->formatter->appendToFormattedSql(\str_repeat($tab, $this->indentation->getIndentLvl()));
136
137
        $this->newline = true;
138
    }
139
140
    /**
141
     * Commas start a new line unless they are found within inline parentheses or SQL 'LIMIT' clause.
142
     * If the previous TOKEN_VALUE is 'LIMIT', undo new line.
143
     */
144
    public function writeNewLineBecauseOfComma()
145
    {
146
        $this->newline = true;
147
148
        if (true === $this->formatter->getClauseLimit()) {
149
            $this->newline = false;
150
            $this->formatter->setClauseLimit(false);
151
        }
152
    }
153
154
    /**
155
     * @param $token
156
     *
157
     * @return bool
158
     */
159
    public function isTokenTypeReservedNewLine($token)
160
    {
161
        return $token[Tokenizer::TOKEN_TYPE] === Tokenizer::TOKEN_TYPE_RESERVED_NEWLINE;
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function getNewline()
168
    {
169
        return $this->newline;
170
    }
171
172
    /**
173
     * @param bool $newline
174
     *
175
     * @return $this
176
     */
177
    public function setNewline($newline)
178
    {
179
        $this->newline = $newline;
180
181
        return $this;
182
    }
183
}
184