Complex classes like OrmStatement 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 OrmStatement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | trait OrmStatement |
||
12 | { |
||
13 | /** |
||
14 | * Include entity analyzing related functionality |
||
15 | */ |
||
16 | use OrmEntityAnalyzer; |
||
17 | |||
18 | /** |
||
19 | * Create a query for selection |
||
20 | * |
||
21 | * @param string $class |
||
22 | * The class for which the query will be created |
||
23 | * @param array $criteria |
||
24 | * Array of criterias in form of "property" => "value" |
||
25 | * @param array $columns |
||
26 | * The columns to retrieve |
||
27 | * @param string $orderBy |
||
28 | * An order-by statement in form of "property ASC|DESC" |
||
29 | * @param int $limit |
||
30 | * The maximum amount of results |
||
31 | * @param int $startFrom |
||
32 | * The offset where to get results of |
||
33 | * @param string $escapeSign |
||
34 | * The string which represents the escape character |
||
35 | * |
||
36 | * @return string The query as sql statement |
||
37 | * |
||
38 | * @throws OrmException |
||
39 | */ |
||
40 | 28 | private static function createQuery(string $class, string $tableName, array &$criteria, array $columns, string $orderBy = '', int $limit = 0, int $startFrom = 0, string $escapeSign = ""): string |
|
41 | { |
||
42 | 28 | $joins = self::getAnnotatedQuery($class, $tableName, $criteria, $columns, $escapeSign); |
|
43 | |||
44 | 28 | $wheres = self::parseCriteria($criteria, $escapeSign); |
|
45 | |||
46 | 27 | $limits = self::parseLimits($limit, $startFrom); |
|
47 | |||
48 | 27 | if ($orderBy && ! stristr($orderBy, 'ORDER BY ')) { |
|
49 | 2 | $orderBy = sprintf("ORDER BY %s%s%s", $escapeSign, $orderBy, $escapeSign); |
|
50 | } |
||
51 | |||
52 | 27 | $query = sprintf("SELECT %s FROM %s%s%s %s %s %s %s", implode(',', $columns), $escapeSign, $tableName, $escapeSign, $joins, $wheres, $orderBy, $limits); |
|
53 | |||
54 | 27 | return $query; |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Create a insert or update statement |
||
59 | * |
||
60 | * @param string $class |
||
61 | * The class of entity |
||
62 | * @param array $pairs |
||
63 | * The pairs of columns and its corresponding values |
||
64 | * @param string $primaryKeyCol |
||
65 | * The name of column which represents the primary key |
||
66 | * @param mixed $primaryKeyValue |
||
67 | * The primary key value |
||
68 | * @param string $escapeSign |
||
69 | * The string which represents the escape character |
||
70 | * |
||
71 | * @return string The update statement as SQL string |
||
72 | */ |
||
73 | 12 | private static function createUpdateStatement(string $class, array $pairs, string $primaryKeyCol, $primaryKeyValue, string $escapeSign): string |
|
90 | |||
91 | /** |
||
92 | * Escape all parts of a criterion |
||
93 | * |
||
94 | * @param string $criterion |
||
95 | * The criterion pattern |
||
96 | * @param string $escapeSign |
||
97 | * The escape sign |
||
98 | * |
||
99 | * @return string The escaped criterion |
||
100 | */ |
||
101 | 25 | private static function escapeCriterion(string $criterion, string $escapeSign) |
|
113 | |||
114 | /** |
||
115 | * Parse criteria into where conditions |
||
116 | * |
||
117 | * @param array $criteria |
||
118 | * The criteria to parse |
||
119 | * @param string $escapeSign |
||
120 | * The escape sign |
||
121 | * |
||
122 | * @return string The parsed criterias as where condition string |
||
123 | * |
||
124 | * @throws OrmException |
||
125 | */ |
||
126 | 28 | private static function parseCriteria(array &$criteria, string $escapeSign): string |
|
152 | |||
153 | /** |
||
154 | * Loops over all where conditions and create a string of it |
||
155 | * |
||
156 | * @param array $wheres |
||
157 | * |
||
158 | * @return string The where conditions as string |
||
159 | */ |
||
160 | 27 | private static function whereConditionsAsString(array $wheres): string |
|
178 | |||
179 | /** |
||
180 | * Prepare the limit and offset modifier |
||
181 | * |
||
182 | * @param int $limit |
||
183 | * @param int $startFrom |
||
184 | * |
||
185 | * @return string The limit modifier or empty string |
||
186 | */ |
||
187 | 27 | private static function parseLimits(int $limit = 0, int $startFrom = 0): string |
|
203 | |||
204 | /** |
||
205 | * Retrieve the persistence parameters via reflection |
||
206 | * |
||
207 | * @param array $pairs |
||
208 | * The pairs of column names => values |
||
209 | * @param string $primaryKeyCol |
||
210 | * The name of primary key column |
||
211 | * @param bool $insert |
||
212 | * Whether to insert or update the data |
||
213 | * @param string $escapeSign |
||
214 | * The escape sign |
||
215 | * |
||
216 | * @return string The prepared statement parameters for persistence |
||
217 | * |
||
218 | * @throws OrmException |
||
219 | */ |
||
220 | 12 | private static function persistenceQueryParams(array $pairs, string $primaryKeyCol, bool $insert, string $escapeSign): string |
|
248 | |||
249 | /** |
||
250 | * When criterion is a property but annotated column name differs, we take the column name |
||
251 | * |
||
252 | * @param string $className |
||
253 | * The entity class name |
||
254 | * @param string $criterion |
||
255 | * The criterion |
||
256 | * |
||
257 | * @return string The criterion |
||
258 | */ |
||
259 | 26 | private static function getAnnotatedCriterion(string $className, string $criterion): string |
|
281 | |||
282 | /** |
||
283 | * Get the annotated join query |
||
284 | * |
||
285 | * @param string $class |
||
286 | * The name of class to use as left class |
||
287 | * @param string $table |
||
288 | * The name of table |
||
289 | * @param array $criteria |
||
290 | * (by reference) List of criterias |
||
291 | * @param array $columns |
||
292 | * (by reference) List of columns |
||
293 | * @param string $escapeSign |
||
294 | * The character which escapes special literals |
||
295 | * |
||
296 | * @return string The join query sql statment |
||
297 | * |
||
298 | * @throws OrmException |
||
299 | */ |
||
300 | 28 | private static function getAnnotatedQuery(string $class, string $table, array &$criteria, array &$columns, string $escapeSign): string |
|
354 | |||
355 | /** |
||
356 | * Parse a complex crition into simple criterion |
||
357 | * |
||
358 | * @param string $criterion |
||
359 | * The full criterion pattern |
||
360 | * |
||
361 | * @return string The simple criterion name |
||
362 | */ |
||
363 | 26 | private static function getSimpleCriterionName(string $criterion): string |
|
371 | } |
||
372 |