Complex classes like Table 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 Table, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | abstract class Table implements TableInterface |
||
44 | { |
||
45 | use Instance; |
||
46 | use TableRelations; |
||
47 | |||
48 | /** |
||
49 | * @var string the table name |
||
50 | */ |
||
51 | protected $name; |
||
52 | |||
53 | /** |
||
54 | * @var string the model name |
||
55 | */ |
||
56 | protected $model; |
||
57 | |||
58 | /** |
||
59 | * @var array table meta |
||
60 | */ |
||
61 | protected $meta = []; |
||
62 | |||
63 | /** |
||
64 | * @var string default SQL query for select |
||
65 | */ |
||
66 | protected $select = ''; |
||
67 | |||
68 | /** |
||
69 | * @var array the primary key column or columns (only as array). |
||
70 | */ |
||
71 | protected $primary; |
||
72 | |||
73 | /** |
||
74 | * @var string the sequence name, required for PostgreSQL |
||
75 | */ |
||
76 | protected $sequence; |
||
77 | |||
78 | /** |
||
79 | * @var string row class name |
||
80 | */ |
||
81 | protected $rowClass; |
||
82 | |||
83 | /** |
||
84 | * Create and initialize Table instance |
||
85 | */ |
||
86 | 2 | private function __construct() |
|
117 | |||
118 | /** |
||
119 | * Initialization hook. |
||
120 | * Subclasses may override this method |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | 2 | public function init() : void |
|
127 | |||
128 | /** |
||
129 | * Get primary key(s) |
||
130 | * |
||
131 | * @return array |
||
132 | * @throws InvalidPrimaryKeyException if primary key was not set or has wrong format |
||
133 | */ |
||
134 | 39 | public function getPrimaryKey() : array |
|
141 | |||
142 | /** |
||
143 | * Get table name |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 3 | public function getName() : string |
|
151 | |||
152 | /** |
||
153 | * Get model name |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | 2 | public function getModel() : string |
|
161 | |||
162 | /** |
||
163 | * Return information about table columns |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | 8 | public static function getMeta() : array |
|
194 | |||
195 | /** |
||
196 | * Return names of table columns |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | 7 | public static function getColumns() : array |
|
205 | |||
206 | /** |
||
207 | * Filter columns for insert/update queries by table columns definition |
||
208 | * |
||
209 | * @param array $data |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | 6 | public static function filterColumns($data) : array |
|
217 | |||
218 | /** |
||
219 | * Fetching rows by SQL query |
||
220 | * |
||
221 | * @param string $sql SQL query with placeholders |
||
222 | * @param array $params Params for query placeholders |
||
223 | * |
||
224 | * @return RowInterface[] of rows results in FETCH_CLASS mode |
||
225 | */ |
||
226 | 10 | protected static function fetch($sql, $params = []) : array |
|
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | * |
||
235 | * @throws DbException |
||
236 | * @throws InvalidPrimaryKeyException if wrong count of values passed |
||
237 | * @throws \InvalidArgumentException |
||
238 | */ |
||
239 | 10 | public static function find(...$keys) : array |
|
264 | |||
265 | /** |
||
266 | * {@inheritdoc} |
||
267 | * |
||
268 | * @throws \InvalidArgumentException |
||
269 | * @throws Exception\DbException |
||
270 | */ |
||
271 | 10 | public static function findWhere(...$where) : array |
|
272 | { |
||
273 | 10 | $self = static::getInstance(); |
|
274 | |||
275 | 10 | $whereParams = []; |
|
276 | |||
277 | 10 | if (count($where) === 2 && is_string($where[0])) { |
|
278 | $whereClause = $where[0]; |
||
279 | $whereParams = (array)$where[1]; |
||
280 | 10 | } elseif (count($where)) { |
|
281 | 10 | $whereOrTerms = []; |
|
282 | 10 | foreach ($where as $keyValueSets) { |
|
283 | 10 | $whereAndTerms = []; |
|
284 | 10 | foreach ($keyValueSets as $keyName => $keyValue) { |
|
285 | 10 | if (is_array($keyValue)) { |
|
286 | $keyValue = array_map( |
||
287 | function ($value) { |
||
288 | return DbProxy::quote($value); |
||
289 | }, |
||
290 | $keyValue |
||
291 | ); |
||
292 | $keyValue = implode(',', $keyValue); |
||
293 | $whereAndTerms[] = $self->name . '.' . $keyName . ' IN (' . $keyValue . ')'; |
||
294 | 10 | } elseif (null === $keyValue) { |
|
295 | $whereAndTerms[] = $self->name . '.' . $keyName . ' IS NULL'; |
||
296 | 10 | } elseif ('%' === $keyValue{0} || '%' === $keyValue{-1}) { |
|
297 | $whereAndTerms[] = $self->name . '.' . $keyName . ' LIKE ?'; |
||
298 | $whereParams[] = $keyValue; |
||
299 | } else { |
||
300 | 10 | $whereAndTerms[] = $self->name . '.' . $keyName . ' = ?'; |
|
301 | 10 | $whereParams[] = $keyValue; |
|
302 | } |
||
303 | 10 | if (!is_scalar($keyValue) && !is_null($keyValue)) { |
|
304 | throw new \InvalidArgumentException( |
||
305 | "Wrong arguments of method 'findWhere'.\n" . |
||
306 | 10 | "Please use syntax described at https://github.com/bluzphp/framework/wiki/Db-Table" |
|
307 | ); |
||
308 | } |
||
309 | } |
||
310 | 10 | $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')'; |
|
311 | } |
||
312 | 10 | $whereClause = '(' . implode(' OR ', $whereOrTerms) . ')'; |
|
313 | } else { |
||
314 | throw new DbException( |
||
315 | "Method `Table::findWhere()` can't return all records from table" |
||
316 | ); |
||
317 | } |
||
318 | |||
319 | 10 | return self::fetch($self->select . ' WHERE ' . $whereClause, $whereParams); |
|
320 | } |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | * |
||
325 | * @throws DbException |
||
326 | * @throws \InvalidArgumentException |
||
327 | * @throws InvalidPrimaryKeyException |
||
328 | */ |
||
329 | 8 | public static function findRow($primaryKey) : ?RowInterface |
|
334 | |||
335 | /** |
||
336 | * {@inheritdoc} |
||
337 | * |
||
338 | * @throws DbException |
||
339 | * @throws \InvalidArgumentException |
||
340 | */ |
||
341 | 2 | public static function findRowWhere(array $whereList) : ?RowInterface |
|
346 | |||
347 | /** |
||
348 | * Prepare array for WHERE or SET statements |
||
349 | * |
||
350 | * @param array $where |
||
351 | * |
||
352 | * @return array |
||
353 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
354 | */ |
||
355 | 6 | private static function prepareStatement(array $where) : array |
|
363 | |||
364 | /** |
||
365 | * Prepare Db\Query\Select for current table: |
||
366 | * - predefine "select" section as "*" from current table |
||
367 | * - predefine "from" section as current table name and first letter as alias |
||
368 | * - predefine fetch type |
||
369 | * |
||
370 | * <code> |
||
371 | * // use default select "*" |
||
372 | * $select = Users\Table::select(); |
||
373 | * $arrUsers = $select->where('u.id = ?', $id) |
||
374 | * ->execute(); |
||
375 | * |
||
376 | * // setup custom select "u.id, u.login" |
||
377 | * $select = Users\Table::select(); |
||
378 | * $arrUsers = $select->select('u.id, u.login') |
||
379 | * ->where('u.id = ?', $id) |
||
380 | * ->execute(); |
||
381 | * </code> |
||
382 | * |
||
383 | * @return Query\Select |
||
384 | */ |
||
385 | 2 | public static function select() : Query\Select |
|
396 | |||
397 | /** |
||
398 | * {@inheritdoc} |
||
399 | */ |
||
400 | 2 | public static function create(array $data = []) : RowInterface |
|
408 | |||
409 | /** |
||
410 | * {@inheritdoc} |
||
411 | * |
||
412 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
413 | * @throws Exception\DbException |
||
414 | */ |
||
415 | 2 | public static function insert(array $data) |
|
446 | |||
447 | /** |
||
448 | * {@inheritdoc} |
||
449 | * |
||
450 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
451 | * @throws Exception\DbException |
||
452 | */ |
||
453 | 2 | public static function update(array $data, array $where) : int |
|
481 | |||
482 | /** |
||
483 | * {@inheritdoc} |
||
484 | * |
||
485 | * @throws \Bluz\Common\Exception\ConfigurationException |
||
486 | * @throws \Bluz\Db\Exception\DbException |
||
487 | */ |
||
488 | 2 | public static function delete(array $where) : int |
|
513 | } |
||
514 |