Complex classes like CreateQuery often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CreateQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class CreateQuery implements QueryInterface |
||
31 | { |
||
32 | use Attributes; |
||
33 | |||
34 | /** @var string */ |
||
35 | protected $table; |
||
36 | |||
37 | /** @var array|\Wandu\Database\Contracts\ExpressionInterface[] */ |
||
38 | protected $columns = []; |
||
39 | |||
40 | /** @var array|\Wandu\Database\Contracts\ExpressionInterface[] */ |
||
41 | protected $constraints = []; |
||
42 | |||
43 | /** @var array */ |
||
44 | protected $options; |
||
45 | |||
46 | /** |
||
47 | * @param string $table |
||
48 | * @param callable $defineHandler |
||
49 | * @param array $options |
||
50 | */ |
||
51 | 2 | public function __construct($table, callable $defineHandler = null, array $options = []) |
|
56 | |||
57 | /** |
||
58 | * @param string $name |
||
59 | * @param int $length |
||
60 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
61 | */ |
||
62 | public function bit($name, $length = 1) |
||
68 | |||
69 | /** |
||
70 | * @param string $name |
||
71 | * @param int $length |
||
72 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
73 | */ |
||
74 | public function tinyInteger($name, $length = 4) |
||
80 | |||
81 | /** |
||
82 | * @param string $name |
||
83 | * @param int $length |
||
84 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
85 | */ |
||
86 | public function smallInteger($name, $length = 6) |
||
92 | |||
93 | /** |
||
94 | * @param string $name |
||
95 | * @param int $length |
||
96 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
97 | */ |
||
98 | public function mediumInteger($name, $length = 9) |
||
104 | |||
105 | /** |
||
106 | * @param string $name |
||
107 | * @param int $length |
||
108 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
109 | */ |
||
110 | public function integer($name, $length = 11) |
||
116 | |||
117 | /** |
||
118 | * @param string $name |
||
119 | * @param int $length |
||
120 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
121 | */ |
||
122 | 2 | public function bigInteger($name, $length = 20) |
|
128 | |||
129 | /** |
||
130 | * @param string $name |
||
131 | * @param int $length |
||
132 | * @param int $decimal |
||
133 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
134 | */ |
||
135 | public function double($name, $length = null, $decimal = null) |
||
142 | |||
143 | /** |
||
144 | * @param string $name |
||
145 | * @param int $length |
||
146 | * @param int $decimal |
||
147 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
148 | */ |
||
149 | public function float($name, $length = null, $decimal = null) |
||
156 | |||
157 | /** |
||
158 | * @param string $name |
||
159 | * @param int $fsp |
||
160 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
161 | */ |
||
162 | public function date($name, $fsp = null) |
||
168 | |||
169 | /** |
||
170 | * @param string $name |
||
171 | * @param int $fsp |
||
172 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
173 | */ |
||
174 | public function time($name, $fsp = null) |
||
180 | |||
181 | /** |
||
182 | * @param string $name |
||
183 | * @param int $fsp |
||
184 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
185 | */ |
||
186 | 2 | public function timestamp($name, $fsp = null) |
|
192 | |||
193 | /** |
||
194 | * @param string $name |
||
195 | * @param int $fsp |
||
196 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
197 | */ |
||
198 | public function datetime($name, $fsp = null) |
||
204 | |||
205 | /** |
||
206 | * @param string $name |
||
207 | * @param int $length |
||
208 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
209 | */ |
||
210 | 2 | public function string($name, $length = 255) |
|
216 | |||
217 | /** |
||
218 | * @param string $name |
||
219 | * @param int $length |
||
220 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
221 | */ |
||
222 | public function char($name, $length = 1) |
||
228 | |||
229 | /** |
||
230 | * @param string $name |
||
231 | * @param int $length |
||
232 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
233 | */ |
||
234 | 1 | public function varchar($name, $length) |
|
240 | |||
241 | /** |
||
242 | * @param string $name |
||
243 | * @param int $length |
||
244 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
245 | */ |
||
246 | public function binary($name, $length = 1) |
||
252 | |||
253 | /** |
||
254 | * @param string $name |
||
255 | * @param int $length |
||
256 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
257 | */ |
||
258 | 1 | public function varbinary($name, $length) |
|
264 | |||
265 | /** |
||
266 | * @param string $name |
||
267 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
268 | */ |
||
269 | public function tinyBlob($name) |
||
273 | |||
274 | /** |
||
275 | * @param string $name |
||
276 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
277 | */ |
||
278 | 1 | public function blob($name) |
|
282 | |||
283 | /** |
||
284 | * @param string $name |
||
285 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
286 | */ |
||
287 | public function mediumBlob($name) |
||
291 | |||
292 | /** |
||
293 | * @param string $name |
||
294 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
295 | */ |
||
296 | public function longBlob($name) |
||
300 | |||
301 | /** |
||
302 | * @param string $name |
||
303 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
304 | */ |
||
305 | public function tinyText($name) |
||
309 | |||
310 | /** |
||
311 | * @param string $name |
||
312 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
313 | */ |
||
314 | public function text($name) |
||
318 | |||
319 | /** |
||
320 | * @param string $name |
||
321 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
322 | */ |
||
323 | public function mediumText($name) |
||
327 | |||
328 | /** |
||
329 | * @param string $name |
||
330 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
331 | */ |
||
332 | 1 | public function longText($name) |
|
336 | |||
337 | /** |
||
338 | * @param string $name |
||
339 | * @param array $values |
||
340 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
341 | */ |
||
342 | 1 | public function enum($name, array $values = []) |
|
348 | |||
349 | /** |
||
350 | * @param string $name |
||
351 | * @param array $values |
||
352 | * @return \Wandu\Database\Query\Expression\ColumnExpression |
||
353 | */ |
||
354 | public function set($name, array $values = []) |
||
360 | |||
361 | /** |
||
362 | * @param string|\Wandu\Database\Contracts\ExpressionInterface $name |
||
363 | * @param string $type |
||
364 | * @param array $attributes |
||
365 | * @return \Wandu\Database\Query\Expression\ColumnExpression|\Wandu\Database\Contracts\ExpressionInterface |
||
366 | */ |
||
367 | 2 | public function addColumn($name, $type = null, array $attributes = []) |
|
374 | |||
375 | /** |
||
376 | * @param array|string $column |
||
377 | * @return \Wandu\Database\Query\Expression\ConstraintExpression |
||
378 | */ |
||
379 | 1 | public function primaryKey($column) |
|
385 | |||
386 | /** |
||
387 | * @param array|string $column |
||
388 | * @param string $name |
||
389 | * @return \Wandu\Database\Query\Expression\ConstraintExpression |
||
390 | */ |
||
391 | 1 | public function uniqueKey($column, $name = null) |
|
397 | |||
398 | /** |
||
399 | * @param array|string $column |
||
400 | * @param string $name |
||
401 | * @return \Wandu\Database\Query\Expression\ConstraintExpression |
||
402 | */ |
||
403 | 1 | public function foreignKey($column, $name = null) |
|
409 | |||
410 | /** |
||
411 | * @param array|string $column |
||
412 | * @param string $name |
||
413 | * @return \Wandu\Database\Query\Expression\ConstraintExpression |
||
414 | */ |
||
415 | 1 | public function index($column, $name = null) |
|
419 | |||
420 | /** |
||
421 | * @param array|string|\Wandu\Database\Contracts\ExpressionInterface $column |
||
422 | * @param string $name |
||
423 | * @param array $attributes |
||
424 | * @return \Wandu\Database\Query\Expression\ConstraintExpression|\Wandu\Database\Contracts\ExpressionInterface |
||
425 | */ |
||
426 | 1 | public function addConstraint($column, $name = null, array $attributes = []) |
|
437 | |||
438 | 2 | public function toSql() |
|
464 | |||
465 | /** |
||
466 | * {@inheritdoc} |
||
467 | */ |
||
468 | public function getBindings() |
||
472 | } |
||
473 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.