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 | 1 | public function __call($name, $arguments) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $name |
||
| 92 | * @param $arguments |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | 1 | protected function isCallUrl($name, $arguments) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $action |
||
| 110 | * @param array $params |
||
| 111 | * @param null $module |
||
| 112 | * @return string|null |
||
| 113 | */ |
||
| 114 | public function compileURL($action, $params = [], $module = null) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | 13 | public function getController() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param null|string $controller |
||
| 161 | */ |
||
| 162 | 13 | public function setController($controller) |
|
| 166 | |||
| 167 | 13 | protected function initController() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | 8 | protected function generateControllerNamespaced() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | 8 | public function getModelNamespacePath() |
|
| 199 | |||
| 200 | 8 | public function initModelNamespacePath() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 8 | protected function generateModelNamespacePathFromClassName() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | 8 | public function getRootNamespace() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 5 | protected function generateControllerGeneric() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $name |
||
| 249 | * @return \Nip\Helpers\AbstractHelper |
||
| 250 | */ |
||
| 251 | 1 | public function getHelper($name) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public function getModelNamespace() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * When searching by primary key, look for items in current registry before |
||
| 266 | * fetching them from the database |
||
| 267 | * |
||
| 268 | * @param array $pk_list |
||
| 269 | * @return RecordCollection |
||
| 270 | */ |
||
| 271 | public function findByPrimary($pk_list = []) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return RecordCollection |
||
| 304 | */ |
||
| 305 | 1 | public function newCollection() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | 2 | public function getCollectionClass() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $collectionClass |
||
| 329 | */ |
||
| 330 | 2 | public function setCollectionClass($collectionClass) |
|
| 334 | |||
| 335 | 2 | protected function initCollectionClass() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | 2 | protected function generateCollectionClass() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @return \Nip\Collections\Registry |
||
| 350 | */ |
||
| 351 | public function getRegistry() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Factory |
||
| 362 | * |
||
| 363 | * @return Record |
||
| 364 | * @param array $data [optional] |
||
| 365 | */ |
||
| 366 | public function getNew($data = []) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param array $data |
||
| 384 | * @return Record |
||
| 385 | */ |
||
| 386 | public function getNewRecordFromDB($data = []) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param array $data |
||
| 396 | * @return Record |
||
| 397 | */ |
||
| 398 | public function getNewRecord($data = []) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | 1 | public function getModel() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @param null $model |
||
| 423 | */ |
||
| 424 | 1 | public function setModel($model) |
|
| 428 | |||
| 429 | protected function inflectModel() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param string $class |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | 1 | public function generateModelClass($class = null) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * @return \Nip\Request |
||
| 463 | */ |
||
| 464 | public function getRequest() |
||
| 468 | |||
| 469 | public function __wakeup() |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * Finds one Record using params array |
||
| 477 | * |
||
| 478 | * @param array $params |
||
| 479 | * @return Record|null |
||
| 480 | */ |
||
| 481 | public function findOneByParams(array $params = []) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Finds Records using params array |
||
| 494 | * |
||
| 495 | * @param array $params |
||
| 496 | * @return RecordCollection |
||
| 497 | */ |
||
| 498 | public function findByParams($params = []) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return RecordCollection |
||
| 507 | */ |
||
| 508 | public function getAll() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @return RecordCollection |
||
| 519 | */ |
||
| 520 | public function findAll() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param int $count |
||
| 527 | * @return RecordCollection |
||
| 528 | */ |
||
| 529 | public function findLast($count = 9) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Inserts a Record into the database |
||
| 538 | * @param Record $model |
||
| 539 | * @param array|bool $onDuplicate |
||
| 540 | * @return integer |
||
| 541 | */ |
||
| 542 | public function insert($model, $onDuplicate = false) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param Record $model |
||
| 552 | * @param $onDuplicate |
||
| 553 | * @return InsertQuery |
||
| 554 | */ |
||
| 555 | public function insertQuery($model, $onDuplicate) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @param Record $model |
||
| 571 | * @return array |
||
| 572 | */ |
||
| 573 | public function getQueryModelData($model) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * The name of the field used as a foreign key in other tables |
||
| 589 | * @return string |
||
| 590 | */ |
||
| 591 | public function getUrlPK() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @param $name |
||
| 602 | * @return bool |
||
| 603 | */ |
||
| 604 | public function hasField($name) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @return array |
||
| 616 | */ |
||
| 617 | public function getFullTextFields() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Sets model and database table from the class name |
||
| 632 | */ |
||
| 633 | protected function inflect() |
||
| 637 | } |
||
| 638 |
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: