Complex classes like RecordSelector 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 RecordSelector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class RecordSelector extends Component implements \IteratorAggregate, \Countable, PaginatorAwareInterface |
||
47 | { |
||
48 | use SaturateTrait; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $class; |
||
54 | |||
55 | /** |
||
56 | * @invisible |
||
57 | * @var ORMInterface |
||
58 | */ |
||
59 | private $orm; |
||
60 | |||
61 | /** |
||
62 | * @var RootLoader |
||
63 | */ |
||
64 | private $loader; |
||
65 | |||
66 | /** |
||
67 | * @param string $class |
||
68 | * @param ORMInterface $orm |
||
69 | */ |
||
70 | public function __construct(string $class, ORMInterface $orm) |
||
81 | |||
82 | /** |
||
83 | * Get associated ORM instance, can be used to create separate query/selection using same |
||
84 | * (nested) memory scope for ORM cache. |
||
85 | * |
||
86 | * @see ORM::selector() |
||
87 | * @return ORMInterface |
||
88 | */ |
||
89 | public function getORM(): ORMInterface |
||
93 | |||
94 | /** |
||
95 | * Get associated class. |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getClass(): string |
||
103 | |||
104 | /** |
||
105 | * Get alias used for primary table. |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getAlias(): string |
||
113 | |||
114 | /** |
||
115 | * Columns to be selected, please note, primary will always be included, DO not include |
||
116 | * column aliases in here, aliases will be added automatically. Creates selector as response. |
||
117 | * |
||
118 | * @param array $columns |
||
119 | * |
||
120 | * @return RecordSelector |
||
121 | */ |
||
122 | public function withColumns(array $columns): self |
||
129 | |||
130 | /** |
||
131 | * Request primary selector loader to pre-load relation name. Any type of loader can be used |
||
132 | * for |
||
133 | * data preloading. ORM loaders by default will select the most efficient way to load related |
||
134 | * data which might include additional select query or left join. Loaded data will |
||
135 | * automatically pre-populate record relations. You can specify nested relations using "." |
||
136 | * separator. |
||
137 | * |
||
138 | * Examples: |
||
139 | * |
||
140 | * //Select users and load their comments (will cast 2 queries, HAS_MANY comments) |
||
141 | * User::find()->with('comments'); |
||
142 | * |
||
143 | * //You can load chain of relations - select user and load their comments and post related to |
||
144 | * //comment |
||
145 | * User::find()->with('comments.post'); |
||
146 | * |
||
147 | * //We can also specify custom where conditions on data loading, let's load only public |
||
148 | * comments. User::find()->load('comments', [ |
||
149 | * 'where' => ['{@}.status' => 'public'] |
||
150 | * ]); |
||
151 | * |
||
152 | * Please note using "{@}" column name, this placeholder is required to prevent collisions and |
||
153 | * it will be automatically replaced with valid table alias of pre-loaded comments table. |
||
154 | * |
||
155 | * //In case where your loaded relation is MANY_TO_MANY you can also specify pivot table |
||
156 | * conditions, |
||
157 | * //let's pre-load all approved user tags, we can use same placeholder for pivot table alias |
||
158 | * User::find()->load('tags', [ |
||
159 | * 'wherePivot' => ['{@}.approved' => true] |
||
160 | * ]); |
||
161 | * |
||
162 | * //In most of cases you don't need to worry about how data was loaded, using external query |
||
163 | * or |
||
164 | * //left join, however if you want to change such behaviour you can force load method to |
||
165 | * INLOAD |
||
166 | * User::find()->load('tags', [ |
||
167 | * 'method' => Loader::INLOAD, |
||
168 | * 'wherePivot' => ['{@}.approved' => true] |
||
169 | * ]); |
||
170 | * |
||
171 | * Attention, you will not be able to correctly paginate in this case and only ORM loaders |
||
172 | * support different loading types. |
||
173 | * |
||
174 | * You can specify multiple loaders using array as first argument. |
||
175 | * |
||
176 | * Example: |
||
177 | * User::find()->load(['posts', 'comments', 'profile']); |
||
178 | * |
||
179 | * Attention, consider disabling entity map if you want to use recursive loading (i.e. |
||
180 | * post.tags.posts), but first think why you even need recursive relation loading. |
||
181 | * |
||
182 | * @see with() |
||
183 | * |
||
184 | * @param string|array $relation |
||
185 | * @param array $options |
||
186 | * |
||
187 | * @return $this|RecordSelector |
||
188 | */ |
||
189 | public function load($relation, array $options = []): self |
||
210 | |||
211 | /** |
||
212 | * With method is very similar to load() one, except it will always include related data to |
||
213 | * parent query using INNER JOIN, this method can be applied only to ORM loaders and relations |
||
214 | * using same database as parent record. |
||
215 | * |
||
216 | * Method generally used to filter data based on some relation condition. |
||
217 | * Attention, with() method WILL NOT load relation data, it will only make it accessible in |
||
218 | * query. |
||
219 | * |
||
220 | * By default joined tables will be available in query based on relation name, you can change |
||
221 | * joined table alias using relation option "alias". |
||
222 | * |
||
223 | * Do not forget to set DISTINCT flag while including HAS_MANY and MANY_TO_MANY relations. In |
||
224 | * other scenario you will not able to paginate data well. |
||
225 | * |
||
226 | * Examples: |
||
227 | * |
||
228 | * //Find all users who have comments comments |
||
229 | * User::find()->with('comments'); |
||
230 | * |
||
231 | * //Find all users who have approved comments (we can use comments table alias in where |
||
232 | * statement). |
||
233 | * User::find()->with('comments')->where('comments.approved', true); |
||
234 | * |
||
235 | * //Find all users who have posts which have approved comments |
||
236 | * User::find()->with('posts.comments')->where('posts_comments.approved', true); |
||
237 | * |
||
238 | * //Custom join alias for post comments relation |
||
239 | * $user->with('posts.comments', [ |
||
240 | * 'alias' => 'comments' |
||
241 | * ])->where('comments.approved', true); |
||
242 | * |
||
243 | * //If you joining MANY_TO_MANY relation you will be able to use pivot table used as relation |
||
244 | * name |
||
245 | * //plus "_pivot" postfix. Let's load all users with approved tags. |
||
246 | * $user->with('tags')->where('tags_pivot.approved', true); |
||
247 | * |
||
248 | * //You can also use custom alias for pivot table as well |
||
249 | * User::find()->with('tags', [ |
||
250 | * 'pivotAlias' => 'tags_connection' |
||
251 | * ]) |
||
252 | * ->where('tags_connection.approved', false); |
||
253 | * |
||
254 | * You can safely combine with() and load() methods. |
||
255 | * |
||
256 | * //Load all users with approved comments and pre-load all their comments |
||
257 | * User::find()->with('comments')->where('comments.approved', true) |
||
258 | * ->load('comments'); |
||
259 | * |
||
260 | * //You can also use custom conditions in this case, let's find all users with approved |
||
261 | * comments |
||
262 | * //and pre-load such approved comments |
||
263 | * User::find()->with('comments')->where('comments.approved', true) |
||
264 | * ->load('comments', [ |
||
265 | * 'where' => ['{@}.approved' => true] |
||
266 | * ]); |
||
267 | * |
||
268 | * //As you might notice previous construction will create 2 queries, however we can simplify |
||
269 | * //this construction to use already joined table as source of data for relation via "using" |
||
270 | * //keyword |
||
271 | * User::find()->with('comments') |
||
272 | * ->where('comments.approved', true) |
||
273 | * ->load('comments', ['using' => 'comments']); |
||
274 | * |
||
275 | * //You will get only one query with INNER JOIN, to better understand this example let's use |
||
276 | * //custom alias for comments in with() method. |
||
277 | * User::find()->with('comments', ['alias' => 'commentsR']) |
||
278 | * ->where('commentsR.approved', true) |
||
279 | * ->load('comments', ['using' => 'commentsR']); |
||
280 | * |
||
281 | * @see load() |
||
282 | * |
||
283 | * @param string|array $relation |
||
284 | * @param array $options |
||
285 | * |
||
286 | * @return $this|RecordSelector |
||
287 | */ |
||
288 | public function with($relation, array $options = []): self |
||
309 | |||
310 | /** |
||
311 | * Shortcut to where method to set AND condition for parent record primary key. |
||
312 | * |
||
313 | * @param string|int $id |
||
314 | * |
||
315 | * @return RecordSelector |
||
316 | * |
||
317 | * @throws SelectorException |
||
318 | */ |
||
319 | public function wherePK($id): self |
||
334 | |||
335 | /** |
||
336 | * Find one entity or return null. |
||
337 | * |
||
338 | * @param array|null $query |
||
339 | * |
||
340 | * @return EntityInterface|null |
||
341 | */ |
||
342 | public function findOne(array $query = null) |
||
352 | |||
353 | /** |
||
354 | * Fetch all records in a form of array. |
||
355 | * |
||
356 | * @param string $cacheKey |
||
357 | * @param int|\DateInterval $ttl |
||
358 | * @param CacheInterface|null $cache Can be automatically resoled via ORM container scope. |
||
359 | * |
||
360 | * @return RecordInterface[] |
||
361 | */ |
||
362 | public function fetchAll( |
||
369 | |||
370 | /** |
||
371 | * Get RecordIterator (entity iterator) for a requested data. Provide cache key and lifetime in |
||
372 | * order to cache request data. |
||
373 | * |
||
374 | * @param string $cacheKey |
||
375 | * @param int|\DateInterval $ttl |
||
376 | * @param CacheInterface|null $cache Can be automatically resoled via ORM container scope. |
||
377 | * |
||
378 | * @return RecordIterator|RecordInterface[] |
||
379 | */ |
||
380 | public function getIterator( |
||
405 | |||
406 | /** |
||
407 | * Attention, column will be quoted by driver! |
||
408 | * |
||
409 | * @param string|null $column When column is null DISTINCT(PK) will be generated. |
||
410 | * |
||
411 | * @return int |
||
412 | */ |
||
413 | public function count(string $column = null): int |
||
426 | |||
427 | /** |
||
428 | * Query used as basement for relation. |
||
429 | * |
||
430 | * @return SelectQuery |
||
431 | */ |
||
432 | public function initialQuery(): SelectQuery |
||
436 | |||
437 | /** |
||
438 | * Get compiled version of SelectQuery, attentionly only first level query access is allowed. |
||
439 | * |
||
440 | * @return SelectQuery |
||
441 | */ |
||
442 | public function compiledQuery(): SelectQuery |
||
446 | |||
447 | /** |
||
448 | * Compiled SQL statement. |
||
449 | * |
||
450 | * @return string |
||
451 | */ |
||
452 | public function sqlStatement(): string |
||
456 | |||
457 | /** |
||
458 | * Load data tree from databases and linked loaders in a form of array. |
||
459 | * |
||
460 | * @param OutputNode $node When empty node will be created automatically by root relation |
||
461 | * loader. |
||
462 | * |
||
463 | * @return array |
||
464 | */ |
||
465 | public function fetchData(OutputNode $node = null): array |
||
475 | |||
476 | /** |
||
477 | * {@inheritdoc} |
||
478 | */ |
||
479 | public function hasPaginator(): bool |
||
483 | |||
484 | /** |
||
485 | * {@inheritdoc} |
||
486 | */ |
||
487 | public function setPaginator(PaginatorInterface $paginator) |
||
491 | |||
492 | /** |
||
493 | * {@inheritdoc} |
||
494 | */ |
||
495 | public function getPaginator(bool $prepare = true): PaginatorInterface |
||
505 | |||
506 | /** |
||
507 | * Bypassing call to primary select query. |
||
508 | * |
||
509 | * @param string $name |
||
510 | * @param $arguments |
||
511 | * |
||
512 | * @return $this|mixed |
||
513 | */ |
||
514 | public function __call(string $name, array $arguments) |
||
530 | |||
531 | /** |
||
532 | * Cloning with loader tree cloning. |
||
533 | * |
||
534 | * @attention at this moment binded query parameters would't be cloned! |
||
535 | */ |
||
536 | public function __clone() |
||
540 | |||
541 | /** |
||
542 | * Remove nested loaders and clean ORM link. |
||
543 | */ |
||
544 | public function __destruct() |
||
549 | |||
550 | /** |
||
551 | * @return \Interop\Container\ContainerInterface|null |
||
552 | */ |
||
553 | protected function iocContainer() |
||
562 | } |
||
563 |