Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 42 | class RecordSelector extends AbstractSelect implements LoggerAwareInterface |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * Selector provides set of profiling functionality helps to understand what is going on with |
||
| 46 | * query and data parsing. |
||
| 47 | */ |
||
| 48 | use LoggerTrait, BenchmarkTrait, SaturateTrait; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Class name of record to be loaded. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $class = ''; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Data columns are set of columns automatically created by inner loaders using |
||
| 59 | * generateColumns() method, this is not the same column set as one provided by user using |
||
| 60 | * columns() method. Do not define columns using generateColumns() method outside of loaders. |
||
| 61 | * |
||
| 62 | * @see generateColumns() |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $dataColumns = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * We have to track count of loader columns to define correct offsets. |
||
| 69 | * |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | protected $countColumns = 0; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Primary selection loader. |
||
| 76 | * |
||
| 77 | * @var Loader |
||
| 78 | */ |
||
| 79 | protected $loader = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @invisible |
||
| 83 | * @var ORM |
||
| 84 | */ |
||
| 85 | protected $orm = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $class |
||
| 89 | * @param ORM $orm |
||
| 90 | * @param Loader $loader |
||
| 91 | */ |
||
| 92 | public function __construct($class, ORM $orm = null, Loader $loader = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Primary selection table. |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function primaryTable() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Primary alias points to table related to parent record. |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function primaryAlias() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | public function columns($columns = ['*']) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Automatically generate set of columns for specified table or alias, method used by loaders |
||
| 146 | * in cases where data is joined. |
||
| 147 | * |
||
| 148 | * @param string $table Source table name or alias. |
||
| 149 | * @param array $columns Original set of record columns. |
||
| 150 | * @return int |
||
| 151 | */ |
||
| 152 | public function generateColumns($table, array $columns) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Request primary selector loader to pre-load relation name. Any type of loader can be used |
||
| 165 | * for |
||
| 166 | * data preloading. ORM loaders by default will select the most efficient way to load related |
||
| 167 | * data which might include additional select query or left join. Loaded data will |
||
| 168 | * automatically pre-populate record relations. You can specify nested relations using "." |
||
| 169 | * separator. |
||
| 170 | * |
||
| 171 | * Examples: |
||
| 172 | * |
||
| 173 | * //Select users and load their comments (will cast 2 queries, HAS_MANY comments) |
||
| 174 | * User::find()->with('comments'); |
||
| 175 | * |
||
| 176 | * //You can load chain of relations - select user and load their comments and post related to |
||
| 177 | * //comment |
||
| 178 | * User::find()->with('comments.post'); |
||
| 179 | * |
||
| 180 | * //We can also specify custom where conditions on data loading, let's load only public |
||
| 181 | * comments. User::find()->load('comments', [ |
||
| 182 | * 'where' => ['{@}.status' => 'public'] |
||
| 183 | * ]); |
||
| 184 | * |
||
| 185 | * Please note using "{@}" column name, this placeholder is required to prevent collisions and |
||
| 186 | * it will be automatically replaced with valid table alias of pre-loaded comments table. |
||
| 187 | * |
||
| 188 | * //In case where your loaded relation is MANY_TO_MANY you can also specify pivot table |
||
| 189 | * conditions, |
||
| 190 | * //let's pre-load all approved user tags, we can use same placeholder for pivot table alias |
||
| 191 | * User::find()->load('tags', [ |
||
| 192 | * 'wherePivot' => ['{@}.approved' => true] |
||
| 193 | * ]); |
||
| 194 | * |
||
| 195 | * //In most of cases you don't need to worry about how data was loaded, using external query |
||
| 196 | * or |
||
| 197 | * //left join, however if you want to change such behaviour you can force load method to |
||
| 198 | * INLOAD |
||
| 199 | * User::find()->load('tags', [ |
||
| 200 | * 'method' => Loader::INLOAD, |
||
| 201 | * 'wherePivot' => ['{@}.approved' => true] |
||
| 202 | * ]); |
||
| 203 | * |
||
| 204 | * Attention, you will not be able to correctly paginate in this case and only ORM loaders |
||
| 205 | * support different loading types. |
||
| 206 | * |
||
| 207 | * You can specify multiple loaders using array as first argument. |
||
| 208 | * |
||
| 209 | * Example: |
||
| 210 | * User::find()->load(['posts', 'comments', 'profile']); |
||
| 211 | * |
||
| 212 | * @see with() |
||
| 213 | * @param string $relation |
||
| 214 | * @param array $options |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | View Code Duplication | public function load($relation, array $options = []) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * With method is very similar to load() one, except it will always include related data to |
||
| 241 | * parent query using INNER JOIN, this method can be applied only to ORM loaders and relations |
||
| 242 | * using same database as parent record. |
||
| 243 | * |
||
| 244 | * Method generally used to filter data based on some relation condition. |
||
| 245 | * Attention, with() method WILL NOT load relation data, it will only make it accessible in |
||
| 246 | * query. |
||
| 247 | * |
||
| 248 | * By default joined tables will be available in query based on realtion name, you can change |
||
| 249 | * joined table alias using relation option "alias". |
||
| 250 | * |
||
| 251 | * Do not forget to set DISTINCT flag while including HAS_MANY and MANY_TO_MANY relations. In |
||
| 252 | * other scenario you will not able to paginate data well. |
||
| 253 | * |
||
| 254 | * Examples: |
||
| 255 | * |
||
| 256 | * //Find all users who have comments comments |
||
| 257 | * User::find()->with('comments'); |
||
| 258 | * |
||
| 259 | * //Find all users who have approved comments (we can use comments table alias in where |
||
| 260 | * statement). |
||
| 261 | * User::find()->with('comments')->where('comments.approved', true); |
||
| 262 | * |
||
| 263 | * //Find all users who have posts which have approved comments |
||
| 264 | * User::find()->with('posts.comments')->where('posts_comments.approved', true); |
||
| 265 | * |
||
| 266 | * //Custom join alias for post comments relation |
||
| 267 | * $user->with('posts.comments', [ |
||
| 268 | * 'alias' => 'comments' |
||
| 269 | * ])->where('comments.approved', true); |
||
| 270 | * |
||
| 271 | * //If you joining MANY_TO_MANY relation you will be able to use pivot table used as relation |
||
| 272 | * name |
||
| 273 | * //plus "_pivot" postfix. Let's load all users with approved tags. |
||
| 274 | * $user->with('tags')->where('tags_pivot.approved', true); |
||
| 275 | * |
||
| 276 | * //You can also use custom alias for pivot table as well |
||
| 277 | * User::find()->with('tags', [ |
||
| 278 | * 'pivotAlias' => 'tags_connection' |
||
| 279 | * ]) |
||
| 280 | * ->where('tags_connection.approved', false); |
||
| 281 | * |
||
| 282 | * You can safely combine with() and load() methods. |
||
| 283 | * |
||
| 284 | * //Load all users with approved comments and pre-load all their comments |
||
| 285 | * User::find()->with('comments')->where('comments.approved', true) |
||
| 286 | * ->load('comments'); |
||
| 287 | * |
||
| 288 | * //You can also use custom conditions in this case, let's find all users with approved |
||
| 289 | * comments |
||
| 290 | * //and pre-load such approved comments |
||
| 291 | * User::find()->with('comments')->where('comments.approved', true) |
||
| 292 | * ->load('comments', [ |
||
| 293 | * 'where' => ['{@}.approved' => true] |
||
| 294 | * ]); |
||
| 295 | * |
||
| 296 | * //As you might notice previous construction will create 2 queries, however we can simplify |
||
| 297 | * //this construction to use already joined table as source of data for relation via "using" |
||
| 298 | * //keyword |
||
| 299 | * User::find()->with('comments')->where('comments.approved', true) |
||
| 300 | * ->load('comments', ['using' => 'comments']); |
||
| 301 | * |
||
| 302 | * //You will get only one query with INNER JOIN, to better understand this example let's use |
||
| 303 | * //custom alias for comments in with() method. |
||
| 304 | * User::find()->with('comments', ['alias' => 'commentsR'])->where('commentsR.approved', true) |
||
| 305 | * ->load('comments', ['using' => 'commentsR']); |
||
| 306 | * |
||
| 307 | * @see load() |
||
| 308 | * @param string $relation |
||
| 309 | * @param array $options |
||
| 310 | * @return $this |
||
| 311 | */ |
||
| 312 | View Code Duplication | public function with($relation, array $options = []) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Fetch one record from database using it's primary key. You can use INLOAD and JOIN_ONLY |
||
| 336 | * loaders with HAS_MANY or MANY_TO_MANY relations with this method as no limit were used. |
||
| 337 | * |
||
| 338 | * @see findOne() |
||
| 339 | * @param mixed $id Primary key value. |
||
| 340 | * @return RecordEntity|null |
||
| 341 | * @throws SelectorException |
||
| 342 | */ |
||
| 343 | public function findByPK($id) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Fetch one record from database. Attention, LIMIT statement will be used, meaning you can not |
||
| 359 | * use loaders for HAS_MANY or MANY_TO_MANY relations with data inload (joins), use default |
||
| 360 | * loading method. |
||
| 361 | * |
||
| 362 | * @see findByPK() |
||
| 363 | * @param array $where Selection WHERE statement. |
||
| 364 | * @param bool $withLimit Use limit 1. |
||
| 365 | * @return RecordEntity|null |
||
| 366 | */ |
||
| 367 | public function findOne(array $where = [], $withLimit = true) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * {@inheritdoc} |
||
| 384 | */ |
||
| 385 | public function sqlStatement(QueryCompiler $compiler = null) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * {@inheritdoc} |
||
| 416 | * |
||
| 417 | * Return type will depend if custom columns set were used. |
||
| 418 | * |
||
| 419 | * @param array $callbacks Callbacks to be used in record iterator as magic methods. |
||
| 420 | * @return QueryResult|RecordIterator |
||
| 421 | */ |
||
| 422 | public function getIterator(array $callbacks = []) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * All records. |
||
| 434 | * |
||
| 435 | * @return RecordInterface[] |
||
| 436 | */ |
||
| 437 | public function all() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Execute query and every related query to compile records data in tree form - every relation |
||
| 444 | * data will be included as sub key. |
||
| 445 | * |
||
| 446 | * Attention, Selector will cache compiled data tree and not query itself to keep data integrity |
||
| 447 | * and to skip data compilation on second query. |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | public function fetchData() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Update all matched records with provided columns set. You are no allowed to use join |
||
| 511 | * conditions or with() method, you can update your records manually in cases like that. |
||
| 512 | * |
||
| 513 | * @param array $update Array of columns to be updated, compatible with UpdateQuery. |
||
| 514 | * @return int |
||
| 515 | * @throws SelectorException |
||
| 516 | */ |
||
| 517 | public function update(array $update) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Delete all matched records and return count of affected rows. You are no allowed to use join |
||
| 564 | * conditions or with() method, you can delete your records manually in cases like that. |
||
| 565 | * |
||
| 566 | * @return int |
||
| 567 | * @throws SelectorException |
||
| 568 | */ |
||
| 569 | public function delete() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Create update statement based on WHERE statement and columns set provided by Selector. |
||
| 595 | * |
||
| 596 | * @param array $columns |
||
| 597 | * @param QueryCompiler $compiler |
||
| 598 | * @return string |
||
| 599 | */ |
||
| 600 | View Code Duplication | protected function updateStatement(array $columns, QueryCompiler $compiler = null) |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Create delete statement based on WHERE statement provided by Selector. |
||
| 615 | * |
||
| 616 | * @param QueryCompiler $compiler |
||
| 617 | * @return string |
||
| 618 | */ |
||
| 619 | View Code Duplication | protected function deleteStatement(QueryCompiler $compiler = null) |
|
| 631 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.