Conditions | 13 |
Paths | 15 |
Total Lines | 93 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
144 | public function buildQuery(): string |
||
145 | { |
||
146 | if (empty($this->command)) { |
||
147 | throw new QueryBuilderException('Could not build query, as there is no command provided!'); |
||
148 | } |
||
149 | |||
150 | switch ($this->command) { |
||
151 | case CommandEnum::INSERT_IGNORE_INTO: |
||
152 | case CommandEnum::INSERT_INTO: |
||
153 | case CommandEnum::REPLACE_INTO: |
||
154 | $queryParts = [ |
||
155 | $this->buildCommandQueryPart(), |
||
156 | $this->buildTableQueryPart(), |
||
157 | $this->buildValueQueryPart(), |
||
158 | $this->buildOnDuplicateKeyUpdateQueryPart(), |
||
159 | ]; |
||
160 | |||
161 | break; |
||
162 | |||
163 | case CommandEnum::SELECT: |
||
164 | if (!empty($this->unionAll)) { |
||
165 | $queryParts = [ |
||
166 | $this->buildUnionAllQueryPart(), |
||
167 | $this->buildOrderByQueryPart(), |
||
168 | $this->buildLimitQueryPart(), |
||
169 | $this->buildOffsetQueryPart(), |
||
170 | ]; |
||
171 | |||
172 | break; |
||
173 | } |
||
174 | |||
175 | $queryParts = [ |
||
176 | $this->buildCommandQueryPart(), |
||
177 | $this->buildColumnQueryPart(), |
||
178 | $this->buildFromQueryPart(), |
||
179 | $this->buildJoinQueryPart(), |
||
180 | $this->buildWhereQueryPart(), |
||
181 | $this->buildGroupByQueryPart(), |
||
182 | $this->buildHavingQueryPart(), |
||
183 | $this->buildOrderByQueryPart(), |
||
184 | $this->buildLimitQueryPart(), |
||
185 | $this->buildOffsetQueryPart(), |
||
186 | ]; |
||
187 | |||
188 | break; |
||
189 | |||
190 | case CommandEnum::UPDATE: |
||
191 | case CommandEnum::UPDATE_IGNORE: |
||
192 | if (empty($this->set)) { |
||
193 | throw new QueryBuilderException('Cannot perform UPDATE action without SET condition!'); |
||
194 | } |
||
195 | |||
196 | if (empty($this->where)) { |
||
197 | throw new QueryBuilderException('Cannot perform UPDATE action without WHERE condition!'); |
||
198 | } |
||
199 | |||
200 | $queryParts = [ |
||
201 | $this->buildCommandQueryPart(), |
||
202 | $this->buildTableQueryPart(), |
||
203 | $this->buildJoinQueryPart(), |
||
204 | $this->buildSetQueryPart(), |
||
205 | $this->buildWhereQueryPart(), |
||
206 | $this->buildLimitQueryPart(), |
||
207 | $this->buildOffsetQueryPart(), |
||
208 | ]; |
||
209 | |||
210 | break; |
||
211 | |||
212 | case CommandEnum::DELETE: |
||
213 | if (empty($this->where)) { |
||
214 | throw new QueryBuilderException('Cannot perform DELETE action without WHERE condition!'); |
||
215 | } |
||
216 | |||
217 | $queryParts = [ |
||
218 | $this->buildCommandQueryPart(), |
||
219 | $this->buildTableQueryPart(), |
||
220 | $this->buildFromQueryPart(), |
||
221 | $this->buildJoinQueryPart(), |
||
222 | $this->buildWhereQueryPart(), |
||
223 | $this->buildLimitQueryPart(), |
||
224 | $this->buildOffsetQueryPart(), |
||
225 | ]; |
||
226 | |||
227 | break; |
||
228 | |||
229 | default: |
||
230 | throw new QueryBuilderException(\sprintf( |
||
231 | 'Could not build query, as there is no valid(%s) command provided!', |
||
232 | $this->command |
||
233 | )); |
||
234 | } |
||
235 | |||
236 | return \trim(\implode(' ', \array_filter($queryParts))); |
||
237 | } |
||
281 |