Complex classes like AbstractRelation 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.
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 AbstractRelation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | abstract class AbstractRelation implements IteratorAggregate |
||
| 56 | { |
||
| 57 | /** |
||
| 58 | * Local column name |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $local; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Foreign column name |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $foreign; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Fetch mode |
||
| 73 | * {@see RelationInterface::FETCH_LAZY} and {@see RelationInterface::FETCH_EAGER} |
||
| 74 | * |
||
| 75 | * @var integer |
||
| 76 | */ |
||
| 77 | protected $fetchMode = RelationInterface::FETCH_LAZY; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Entity classname for this relation |
||
| 81 | * |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $entity; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Column name in parent entity for this relation |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $columnName; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Connection |
||
| 95 | * |
||
| 96 | * @var \Fwk\Db\Connection |
||
| 97 | */ |
||
| 98 | protected $connection; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Is the relation fetched ? |
||
| 102 | * |
||
| 103 | * @var boolean |
||
| 104 | */ |
||
| 105 | protected $fetched = false; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Parent references |
||
| 109 | * |
||
| 110 | * @var mixed |
||
| 111 | */ |
||
| 112 | protected $parentRefs; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Parent entity (if any) |
||
| 116 | * |
||
| 117 | * @var mixed |
||
| 118 | */ |
||
| 119 | protected $parent; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * RelationInterface's own registry |
||
| 123 | * |
||
| 124 | * @var Registry |
||
| 125 | */ |
||
| 126 | protected $registry; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Referenced table name |
||
| 130 | * |
||
| 131 | * @var string |
||
| 132 | */ |
||
| 133 | protected $tableName; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * List of entity listeners |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | protected $listeners = array(); |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Constructor |
||
| 144 | * |
||
| 145 | * @param string $local The local column's name |
||
| 146 | * @param string $foreign The foreign column's name |
||
| 147 | * @param string $table The foreign table name |
||
| 148 | * @param string $entity The entity's class name |
||
|
|
|||
| 149 | * @param array $listeners List of entity listeners |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | public function __construct($local, $foreign, $table, $entity = null, |
||
| 163 | |||
| 164 | /** |
||
| 165 | * FETCH_EAGER -or- FETCH_LAZY |
||
| 166 | * |
||
| 167 | * @param integer $mode The fetch mode (@see constants) |
||
| 168 | * |
||
| 169 | * @return RelationInterface |
||
| 170 | */ |
||
| 171 | public function setFetchMode($mode) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Changes the fetched state of this relation |
||
| 180 | * |
||
| 181 | * @param boolean $bool Is the data fetched yet? |
||
| 182 | * |
||
| 183 | * @return RelationInterface |
||
| 184 | */ |
||
| 185 | public function setFetched($bool) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Returns the connection defined for this relation |
||
| 205 | * |
||
| 206 | * @return Connection |
||
| 207 | */ |
||
| 208 | public function getConnection() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Sets a connection for this relation (used for lazy loading) |
||
| 226 | * |
||
| 227 | * @param Connection $connection The database connection instance |
||
| 228 | * |
||
| 229 | * @return RelationInterface |
||
| 230 | */ |
||
| 231 | public function setConnection(Connection $connection) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Tells if an entity managed by this relation has changed |
||
| 240 | * |
||
| 241 | * @return boolean |
||
| 242 | */ |
||
| 243 | public function hasChanged() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Tells if this relation is active (parents references have been defined) |
||
| 259 | * |
||
| 260 | * @return boolean |
||
| 261 | */ |
||
| 262 | public function isActive() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Tells if the specified object is in the relation |
||
| 269 | * |
||
| 270 | * @param object $object The object to test |
||
| 271 | * |
||
| 272 | * @return boolean |
||
| 273 | */ |
||
| 274 | public function has($object) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Defines parent references |
||
| 281 | * |
||
| 282 | * @param array $refs Defines parent's references (eg. Primary Keys) |
||
| 283 | * |
||
| 284 | * @return RelationInterface |
||
| 285 | */ |
||
| 286 | public function setParentRefs($refs) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Sets the parent entity of this relation |
||
| 295 | * |
||
| 296 | * @param object $object The parent entity |
||
| 297 | * @param Dispatcher $evd The Event Dispatcher for the parent entity. |
||
| 298 | * |
||
| 299 | * @return boolean true if parent has been changed/defined |
||
| 300 | */ |
||
| 301 | public function setParent($object, Dispatcher $evd) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Tells if this relation has been fetched |
||
| 314 | * |
||
| 315 | * @return boolean |
||
| 316 | */ |
||
| 317 | public function isFetched() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Tells if this relation is in LAZY fetch mode |
||
| 324 | * |
||
| 325 | * @return boolean |
||
| 326 | */ |
||
| 327 | public function isLazy() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Tells if this relation is in EAGER fetch mode |
||
| 334 | * |
||
| 335 | * @return boolean |
||
| 336 | */ |
||
| 337 | public function isEager() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Return the defined entity for this relation |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function getEntity() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Return the foreign column for this relation |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function getForeign() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Return the local column for this relation |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function getLocal() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Removes all objects |
||
| 374 | * |
||
| 375 | * @return RelationInterface |
||
| 376 | */ |
||
| 377 | public function clear() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns this relation's registry |
||
| 387 | * |
||
| 388 | * @return Registry |
||
| 389 | */ |
||
| 390 | public function getRegistry() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Defines a Registry for this relation |
||
| 397 | * |
||
| 398 | * @param Registry $registry The registry |
||
| 399 | * |
||
| 400 | * @return RelationInterface |
||
| 401 | */ |
||
| 402 | public function setRegistry(Registry $registry) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Return this relation's table name |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function getTableName() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Add an entity to this relation |
||
| 421 | * |
||
| 422 | * @param object $object The entity to add |
||
| 423 | * @param array $identifiers Identifiers (PK) of this entity if any |
||
| 424 | * |
||
| 425 | * @return RelationInterface |
||
| 426 | */ |
||
| 427 | public function add($object, array $identifiers = array()) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Removes an entity from this relation |
||
| 440 | * |
||
| 441 | * @param object $object The entity to be removed |
||
| 442 | * |
||
| 443 | * @return RelationInterface |
||
| 444 | */ |
||
| 445 | public function remove($object) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Fetches data from database |
||
| 456 | * |
||
| 457 | * @return RelationInterface |
||
| 458 | */ |
||
| 459 | abstract public function fetch(); |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Returns a list of all entities in this relations. |
||
| 463 | * Triggers a fetch() when fetchMode = FETCH_LAZY |
||
| 464 | * |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | abstract public function toArray(); |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Return this relation data within an Iterator (foreach ...) |
||
| 471 | * {@see \Traversable} |
||
| 472 | * |
||
| 473 | * @return \ArrayIterator |
||
| 474 | */ |
||
| 475 | public function getIterator() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Returns all entity-listeners for this relation |
||
| 484 | * |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | public function getListeners() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Returns to-be-executed workers queue |
||
| 494 | * |
||
| 495 | * @return \SplPriorityQueue |
||
| 496 | */ |
||
| 497 | protected function getWorkersQueue() |
||
| 521 | } |
||
| 522 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.