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 |
||
| 43 | class RecordSelector extends AbstractSelect implements LoggerAwareInterface |
||
| 44 | { |
||
| 45 | use LoggerTrait, BenchmarkTrait, SaturateTrait; |
||
| 46 | |||
| 47 | const DEFAULT_COUNTING_FIELD = '*'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Class name of record to be loaded. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $class = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Data columns are set of columns automatically created by inner loaders using |
||
| 58 | * generateColumns() method, this is not the same column set as one provided by user using |
||
| 59 | * columns() method. Do not define columns using generateColumns() method outside of loaders. |
||
| 60 | * |
||
| 61 | * @see addColumns() |
||
| 62 | * |
||
| 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 | * |
||
| 84 | * @var ORM |
||
| 85 | */ |
||
| 86 | protected $orm = null; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $class |
||
| 90 | * @param ORM $orm |
||
| 91 | * @param Loader $loader |
||
| 92 | */ |
||
| 93 | public function __construct($class, ORM $orm = null, Loader $loader = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Primary selection table. |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function primaryTable() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Primary alias points to table related to parent record. |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function primaryAlias() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function columns($columns = ['*']) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Automatically generate set of columns for specified table or alias, method used by loaders |
||
| 151 | * in cases where data is joined. |
||
| 152 | * |
||
| 153 | * @param string $table Source table name or alias. |
||
| 154 | * @param array $columns Original set of record columns. |
||
| 155 | * |
||
| 156 | * @return int |
||
| 157 | */ |
||
| 158 | public function registerColumns($table, array $columns) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Request primary selector loader to pre-load relation name. Any type of loader can be used |
||
| 172 | * for |
||
| 173 | * data preloading. ORM loaders by default will select the most efficient way to load related |
||
| 174 | * data which might include additional select query or left join. Loaded data will |
||
| 175 | * automatically pre-populate record relations. You can specify nested relations using "." |
||
| 176 | * separator. |
||
| 177 | * |
||
| 178 | * Examples: |
||
| 179 | * |
||
| 180 | * //Select users and load their comments (will cast 2 queries, HAS_MANY comments) |
||
| 181 | * User::find()->with('comments'); |
||
| 182 | * |
||
| 183 | * //You can load chain of relations - select user and load their comments and post related to |
||
| 184 | * //comment |
||
| 185 | * User::find()->with('comments.post'); |
||
| 186 | * |
||
| 187 | * //We can also specify custom where conditions on data loading, let's load only public |
||
| 188 | * comments. User::find()->load('comments', [ |
||
| 189 | * 'where' => ['{@}.status' => 'public'] |
||
| 190 | * ]); |
||
| 191 | * |
||
| 192 | * Please note using "{@}" column name, this placeholder is required to prevent collisions and |
||
| 193 | * it will be automatically replaced with valid table alias of pre-loaded comments table. |
||
| 194 | * |
||
| 195 | * //In case where your loaded relation is MANY_TO_MANY you can also specify pivot table |
||
| 196 | * conditions, |
||
| 197 | * //let's pre-load all approved user tags, we can use same placeholder for pivot table alias |
||
| 198 | * User::find()->load('tags', [ |
||
| 199 | * 'wherePivot' => ['{@}.approved' => true] |
||
| 200 | * ]); |
||
| 201 | * |
||
| 202 | * //In most of cases you don't need to worry about how data was loaded, using external query |
||
| 203 | * or |
||
| 204 | * //left join, however if you want to change such behaviour you can force load method to |
||
| 205 | * INLOAD |
||
| 206 | * User::find()->load('tags', [ |
||
| 207 | * 'method' => Loader::INLOAD, |
||
| 208 | * 'wherePivot' => ['{@}.approved' => true] |
||
| 209 | * ]); |
||
| 210 | * |
||
| 211 | * Attention, you will not be able to correctly paginate in this case and only ORM loaders |
||
| 212 | * support different loading types. |
||
| 213 | * |
||
| 214 | * You can specify multiple loaders using array as first argument. |
||
| 215 | * |
||
| 216 | * Example: |
||
| 217 | * User::find()->load(['posts', 'comments', 'profile']); |
||
| 218 | * |
||
| 219 | * @see with() |
||
| 220 | * |
||
| 221 | * @param string $relation |
||
| 222 | * @param array $options |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function load($relation, array $options = []) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * With method is very similar to load() one, except it will always include related data to |
||
| 250 | * parent query using INNER JOIN, this method can be applied only to ORM loaders and relations |
||
| 251 | * using same database as parent record. |
||
| 252 | * |
||
| 253 | * Method generally used to filter data based on some relation condition. |
||
| 254 | * Attention, with() method WILL NOT load relation data, it will only make it accessible in |
||
| 255 | * query. |
||
| 256 | * |
||
| 257 | * By default joined tables will be available in query based on realtion name, you can change |
||
| 258 | * joined table alias using relation option "alias". |
||
| 259 | * |
||
| 260 | * Do not forget to set DISTINCT flag while including HAS_MANY and MANY_TO_MANY relations. In |
||
| 261 | * other scenario you will not able to paginate data well. |
||
| 262 | * |
||
| 263 | * Examples: |
||
| 264 | * |
||
| 265 | * //Find all users who have comments comments |
||
| 266 | * User::find()->with('comments'); |
||
| 267 | * |
||
| 268 | * //Find all users who have approved comments (we can use comments table alias in where |
||
| 269 | * statement). |
||
| 270 | * User::find()->with('comments')->where('comments.approved', true); |
||
| 271 | * |
||
| 272 | * //Find all users who have posts which have approved comments |
||
| 273 | * User::find()->with('posts.comments')->where('posts_comments.approved', true); |
||
| 274 | * |
||
| 275 | * //Custom join alias for post comments relation |
||
| 276 | * $user->with('posts.comments', [ |
||
| 277 | * 'alias' => 'comments' |
||
| 278 | * ])->where('comments.approved', true); |
||
| 279 | * |
||
| 280 | * //If you joining MANY_TO_MANY relation you will be able to use pivot table used as relation |
||
| 281 | * name |
||
| 282 | * //plus "_pivot" postfix. Let's load all users with approved tags. |
||
| 283 | * $user->with('tags')->where('tags_pivot.approved', true); |
||
| 284 | * |
||
| 285 | * //You can also use custom alias for pivot table as well |
||
| 286 | * User::find()->with('tags', [ |
||
| 287 | * 'pivotAlias' => 'tags_connection' |
||
| 288 | * ]) |
||
| 289 | * ->where('tags_connection.approved', false); |
||
| 290 | * |
||
| 291 | * You can safely combine with() and load() methods. |
||
| 292 | * |
||
| 293 | * //Load all users with approved comments and pre-load all their comments |
||
| 294 | * User::find()->with('comments')->where('comments.approved', true) |
||
| 295 | * ->load('comments'); |
||
| 296 | * |
||
| 297 | * //You can also use custom conditions in this case, let's find all users with approved |
||
| 298 | * comments |
||
| 299 | * //and pre-load such approved comments |
||
| 300 | * User::find()->with('comments')->where('comments.approved', true) |
||
| 301 | * ->load('comments', [ |
||
| 302 | * 'where' => ['{@}.approved' => true] |
||
| 303 | * ]); |
||
| 304 | * |
||
| 305 | * //As you might notice previous construction will create 2 queries, however we can simplify |
||
| 306 | * //this construction to use already joined table as source of data for relation via "using" |
||
| 307 | * //keyword |
||
| 308 | * User::find()->with('comments')->where('comments.approved', true) |
||
| 309 | * ->load('comments', ['using' => 'comments']); |
||
| 310 | * |
||
| 311 | * //You will get only one query with INNER JOIN, to better understand this example let's use |
||
| 312 | * //custom alias for comments in with() method. |
||
| 313 | * User::find()->with('comments', ['alias' => 'commentsR'])->where('commentsR.approved', true) |
||
| 314 | * ->load('comments', ['using' => 'commentsR']); |
||
| 315 | * |
||
| 316 | * @see load() |
||
| 317 | * |
||
| 318 | * @param string $relation |
||
| 319 | * @param array $options |
||
| 320 | * |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | View Code Duplication | public function with($relation, array $options = []) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Fetch one record from database using it's primary key. You can use INLOAD and JOIN_ONLY |
||
| 347 | * loaders with HAS_MANY or MANY_TO_MANY relations with this method as no limit were used. |
||
| 348 | * |
||
| 349 | * @see findOne() |
||
| 350 | * |
||
| 351 | * @param mixed $id Primary key value. |
||
| 352 | * |
||
| 353 | * @return RecordEntity|null |
||
| 354 | * |
||
| 355 | * @throws SelectorException |
||
| 356 | */ |
||
| 357 | public function findByPK($id) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Fetch one record from database. Attention, LIMIT statement will be used, meaning you can not |
||
| 373 | * use loaders for HAS_MANY or MANY_TO_MANY relations with data inload (joins), use default |
||
| 374 | * loading method. |
||
| 375 | * |
||
| 376 | * @see findByPK() |
||
| 377 | * |
||
| 378 | * @param array $where Selection WHERE statement. |
||
| 379 | * @param bool $withLimit Use limit 1. |
||
| 380 | * |
||
| 381 | * @return RecordEntity|null |
||
| 382 | */ |
||
| 383 | public function findOne(array $where = [], $withLimit = true) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * {@inheritdoc} |
||
| 400 | */ |
||
| 401 | public function sqlStatement(QueryCompiler $compiler = null) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * {@inheritdoc} |
||
| 434 | * |
||
| 435 | * Return type will depend if custom columns set were used. |
||
| 436 | * |
||
| 437 | * @return PDOQuery|RecordIterator |
||
| 438 | */ |
||
| 439 | public function getIterator() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * All records. |
||
| 455 | * |
||
| 456 | * @return RecordInterface[] |
||
| 457 | */ |
||
| 458 | public function all() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Execute query and every related query to compile records data in tree form - every relation |
||
| 465 | * data will be included as sub key. |
||
| 466 | * |
||
| 467 | * Attention, Selector will cache compiled data tree and not query itself to keep data integrity |
||
| 468 | * and to skip data compilation on second query. |
||
| 469 | * |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | public function fetchData() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * {@inheritdoc} |
||
| 532 | */ |
||
| 533 | public function count($column = self::DEFAULT_COUNTING_FIELD) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Cloning selector presets. |
||
| 544 | */ |
||
| 545 | public function __clone() |
||
| 549 | } |
||
| 550 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: