Complex classes like RecordManager 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 RecordManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | abstract class RecordManager |
||
| 19 | {
|
||
| 20 | use NameWorksTrait; |
||
| 21 | use ActiveRecordsTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Collection class for current record manager |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $collectionClass = null; |
||
| 29 | |||
| 30 | protected $helpers = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var null|string |
||
| 34 | */ |
||
| 35 | protected $urlPK = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Model class name |
||
| 39 | * @var null|string |
||
| 40 | */ |
||
| 41 | protected $model = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var null|string |
||
| 45 | */ |
||
| 46 | protected $controller = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var null|string |
||
| 50 | */ |
||
| 51 | protected $modelNamespacePath = null; |
||
| 52 | |||
| 53 | protected $registry = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Overloads findByRecord, findByField, deleteByRecord, deleteByField, countByRecord, countByField |
||
| 57 | * |
||
| 58 | * @example findByCategory(Category $item) |
||
| 59 | * @example deleteByProduct(Product $item) |
||
| 60 | * @example findByIdUser(2) |
||
| 61 | * @example deleteByTitle(array('Lorem ipsum', 'like'))
|
||
| 62 | * @example countByIdCategory(1) |
||
| 63 | * |
||
| 64 | * @param string $name |
||
| 65 | * @param array $arguments |
||
| 66 | * |
||
| 67 | * @return mixed |
||
| 68 | */ |
||
| 69 | public function __call($name, $arguments) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param $name |
||
| 92 | * @param $arguments |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | protected function isCallUrl($name, $arguments) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param $action |
||
| 110 | * @param array $params |
||
| 111 | * @param null $module |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | public function compileURL($action, $params = [], $module = null) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getController() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param null|string $controller |
||
| 161 | */ |
||
| 162 | public function setController($controller) |
||
| 166 | |||
| 167 | protected function initController() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | protected function generateControllerNamespaced() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function getModelNamespacePath() |
||
| 199 | |||
| 200 | public function initModelNamespacePath() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | protected function generateModelNamespacePathFromClassName() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getRootNamespace() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | protected function generateControllerGeneric() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param $name |
||
| 246 | * @return mixed |
||
| 247 | */ |
||
| 248 | public function getHelper($name) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function getModelNamespace() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * When searching by primary key, look for items in current registry before |
||
| 263 | * fetching them from the database |
||
| 264 | * |
||
| 265 | * @param array $pk_list |
||
| 266 | * @return RecordCollection |
||
| 267 | */ |
||
| 268 | public function findByPrimary($pk_list = []) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return RecordCollection |
||
| 301 | */ |
||
| 302 | public function newCollection() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getCollectionClass() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $collectionClass |
||
| 326 | */ |
||
| 327 | public function setCollectionClass($collectionClass) |
||
| 331 | |||
| 332 | protected function initCollectionClass() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | protected function generateCollectionClass() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return \Nip\Collections\Registry |
||
| 347 | */ |
||
| 348 | public function getRegistry() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Factory |
||
| 359 | * |
||
| 360 | * @return Record |
||
| 361 | * @param array $data [optional] |
||
| 362 | */ |
||
| 363 | public function getNew($data = []) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param array $data |
||
| 381 | * @return Record |
||
| 382 | */ |
||
| 383 | public function getNewRecordFromDB($data = []) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param array $data |
||
| 393 | * @return Record |
||
| 394 | */ |
||
| 395 | public function getNewRecord($data = []) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function getModel() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param null $model |
||
| 420 | */ |
||
| 421 | public function setModel($model) |
||
| 425 | |||
| 426 | protected function inflectModel() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param null $class |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | public function generateModelClass($class = null) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @return \Nip\Request |
||
| 460 | */ |
||
| 461 | public function getRequest() |
||
| 465 | |||
| 466 | public function __wakeup() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param Record $item |
||
| 473 | * @return bool|false|Record |
||
| 474 | */ |
||
| 475 | public function exists(Record $item) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @return null |
||
| 500 | */ |
||
| 501 | public function getUniqueFields() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @return array|null |
||
| 512 | */ |
||
| 513 | public function initUniqueFields() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Finds one Record using params array |
||
| 532 | * |
||
| 533 | * @param array $params |
||
| 534 | * @return Record|false |
||
| 535 | */ |
||
| 536 | public function findOneByParams(array $params = []) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Finds Records using params array |
||
| 549 | * |
||
| 550 | * @param array $params |
||
| 551 | * @return mixed |
||
| 552 | */ |
||
| 553 | public function findByParams($params = []) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @return RecordCollection |
||
| 562 | */ |
||
| 563 | public function getAll() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @return RecordCollection |
||
| 574 | */ |
||
| 575 | public function findAll() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @param int $count |
||
| 582 | * @return mixed |
||
| 583 | */ |
||
| 584 | public function findLast($count = 9) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Inserts a Record into the database |
||
| 593 | * @param Record $model |
||
| 594 | * @param array|bool $onDuplicate |
||
| 595 | * @return mixed |
||
| 596 | */ |
||
| 597 | public function insert($model, $onDuplicate = false) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @param $model |
||
| 607 | * @param $onDuplicate |
||
| 608 | * @return InsertQuery |
||
| 609 | */ |
||
| 610 | public function insertQuery($model, $onDuplicate) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @param Record $model |
||
| 626 | * @return array |
||
| 627 | */ |
||
| 628 | public function getQueryModelData($model) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * The name of the field used as a foreign key in other tables |
||
| 644 | * @return string |
||
| 645 | */ |
||
| 646 | public function getPrimaryFK() |
||
| 654 | |||
| 655 | public function initPrimaryFK() |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @param null $foreignKey |
||
| 662 | */ |
||
| 663 | public function setForeignKey($foreignKey) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * @return string |
||
| 670 | */ |
||
| 671 | public function generatePrimaryFK() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @param $fk |
||
| 680 | */ |
||
| 681 | public function setPrimaryFK($fk) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * The name of the field used as a foreign key in other tables |
||
| 688 | * @return string |
||
| 689 | */ |
||
| 690 | public function getUrlPK() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @param $name |
||
| 701 | * @return bool |
||
| 702 | */ |
||
| 703 | public function hasField($name) |
||
| 712 | |||
| 713 | /** |
||
| 714 | * @return array |
||
| 715 | */ |
||
| 716 | public function getFullTextFields() |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Sets model and database table from the class name |
||
| 731 | */ |
||
| 732 | protected function inflect() |
||
| 736 | } |
||
| 737 |
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: