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 |
||
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) |
||
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) |
||
72 | |||
73 | /** |
||
74 | * @param $token |
||
75 | */ |
||
76 | public function writeNewLineForLongCommaInlineValues($token) |
||
85 | |||
86 | /** |
||
87 | * @param int $length |
||
88 | */ |
||
89 | View Code Duplication | public function writeNewLineForLongInlineValues($length) |
|
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() |
|
108 | |||
109 | /** |
||
110 | * @param bool $addedNewline |
||
111 | * @param string $tab |
||
112 | */ |
||
113 | public function addNewLineBeforeToken($addedNewline, $tab) |
||
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) |
||
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() |
||
153 | |||
154 | /** |
||
155 | * @param $token |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function isTokenTypeReservedNewLine($token) |
||
163 | |||
164 | /** |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function getNewline() |
||
171 | |||
172 | /** |
||
173 | * @param bool $newline |
||
174 | * |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function setNewline($newline) |
||
183 | } |
||
184 |
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.