NewLine   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 165
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 20
c 1
b 1
f 1
lcom 1
cbo 3
dl 15
loc 165
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addNewLineBreak() 0 12 2
A writeNewLineForLongCommaInlineValues() 0 9 3
A writeNewLineForLongInlineValues() 8 8 3
A addNewLineAfterOpeningParentheses() 7 7 2
A addNewLineBeforeToken() 0 8 2
A writeNewLineBecauseOfTopLevelReservedWord() 0 11 2
A writeNewLineBecauseOfComma() 0 9 2
A isTokenTypeReservedNewLine() 0 4 1
A getNewline() 0 4 1
A setNewline() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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