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:
| 1 | <?php |
||
| 20 | abstract class Relationship |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * The mapper instance for the related entity |
||
| 24 | * |
||
| 25 | * @var Mapper |
||
| 26 | */ |
||
| 27 | protected $relatedMapper; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The Analogue Query Builder instance. |
||
| 31 | * |
||
| 32 | * @var Query |
||
| 33 | */ |
||
| 34 | protected $query; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The parent entity proxy instance. |
||
| 38 | * |
||
| 39 | * @var InternallyMappable |
||
| 40 | */ |
||
| 41 | protected $parent; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The parent entity map |
||
| 45 | * @var \Analogue\ORM\EntityMap |
||
| 46 | */ |
||
| 47 | protected $parentMap; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The Parent Mapper instance |
||
| 51 | * |
||
| 52 | * @var Mapper |
||
| 53 | */ |
||
| 54 | protected $parentMapper; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The related entity instance. |
||
| 58 | * |
||
| 59 | * @var object |
||
| 60 | */ |
||
| 61 | protected $related; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The related entity Map |
||
| 65 | * @var \Analogue\ORM\EntityMap |
||
| 66 | */ |
||
| 67 | protected $relatedMap; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Indicate if the parent entity hold the key for the relation. |
||
| 71 | * |
||
| 72 | * @var boolean |
||
| 73 | */ |
||
| 74 | protected static $ownForeignKey = false; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Indicate if the relationships use a pivot table.* |
||
| 78 | * |
||
| 79 | * @var boolean |
||
| 80 | */ |
||
| 81 | protected static $hasPivot = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Indicates if the relation is adding constraints. |
||
| 85 | * |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | protected static $constraints = true; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Wrapper factory |
||
| 92 | * |
||
| 93 | * @var \Analogue\ORM\System\Wrappers\Factory |
||
| 94 | */ |
||
| 95 | protected $factory; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Create a new relation instance. |
||
| 99 | * |
||
| 100 | * @param Mapper $mapper |
||
| 101 | * @param mixed $parent |
||
| 102 | * @throws \Analogue\ORM\Exceptions\MappingException |
||
| 103 | */ |
||
| 104 | public function __construct(Mapper $mapper, $parent) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Indicate if the parent entity hold the foreign key for relation. |
||
| 127 | * |
||
| 128 | * @return boolean |
||
| 129 | */ |
||
| 130 | public function ownForeignKey() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Indicate if the relationship uses a pivot table |
||
| 137 | * |
||
| 138 | * @return boolean |
||
| 139 | */ |
||
| 140 | public function hasPivot() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set the base constraints on the relation query. |
||
| 147 | * |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | abstract public function addConstraints(); |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Set the constraints for an eager load of the relation. |
||
| 154 | * |
||
| 155 | * @param array $results |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | abstract public function addEagerConstraints(array $results); |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Match the eagerly loaded results to their parents, then return |
||
| 162 | * updated results |
||
| 163 | * |
||
| 164 | * @param array $results |
||
| 165 | * @param string $relation |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | abstract public function match(array $results, $relation); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get the results of the relationship. |
||
| 172 | * |
||
| 173 | * @param string $relation relation name in parent's entity map |
||
| 174 | * @return mixed |
||
| 175 | */ |
||
| 176 | abstract public function getResults($relation); |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the relationship for eager loading. |
||
| 180 | * |
||
| 181 | * @return EntityCollection |
||
| 182 | */ |
||
| 183 | public function getEager() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Add the constraints for a relationship count query. |
||
| 190 | * |
||
| 191 | * @param Query $query |
||
| 192 | * @param Query $parent |
||
| 193 | * @return Query |
||
| 194 | */ |
||
| 195 | public function getRelationCountQuery(Query $query, Query $parent) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Run a callback with constraints disabled on the relation. |
||
| 206 | * |
||
| 207 | * @param Closure $callback |
||
| 208 | * @return mixed |
||
| 209 | */ |
||
| 210 | public static function noConstraints(Closure $callback) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get all of the primary keys for an array of entities. |
||
| 226 | * |
||
| 227 | * @param array $entities |
||
| 228 | * @param string $key |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | protected function getKeys(array $entities, $key = null) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get all the keys from a result set |
||
| 251 | * |
||
| 252 | * @param array $results |
||
| 253 | * @param string $key |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | protected function getKeysFromResults(array $results, $key = null) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get the underlying query for the relation. |
||
| 269 | * |
||
| 270 | * @return Query |
||
| 271 | */ |
||
| 272 | public function getQuery() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the base query builder |
||
| 279 | * |
||
| 280 | * @return \Analogue\ORM\Drivers\QueryAdapter |
||
| 281 | */ |
||
| 282 | public function getBaseQuery() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get the parent model of the relation. |
||
| 289 | * |
||
| 290 | * @return InternallyMappable |
||
| 291 | */ |
||
| 292 | public function getParent() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Set the parent model of the relation |
||
| 299 | * |
||
| 300 | * @param InternallyMappable $parent |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | public function setParent(InternallyMappable $parent) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Get the fully qualified parent key name. |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | protected function getQualifiedParentKeyName() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get the related entity of the relation. |
||
| 320 | * |
||
| 321 | * @return \Analogue\ORM\Entity |
||
| 322 | */ |
||
| 323 | public function getRelated() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get the related mapper for the relation |
||
| 330 | * |
||
| 331 | * @return Mapper |
||
| 332 | */ |
||
| 333 | public function getRelatedMapper() |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * Get the name of the "created at" column. |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | public function createdAt() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get the name of the "updated at" column. |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function updatedAt() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get the name of the related model's "updated at" column. |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | public function relatedUpdatedAt() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Wrap the given value with the parent query's grammar. |
||
| 371 | * |
||
| 372 | * @param string $value |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | public function wrap($value) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get a fresh timestamp |
||
| 382 | * |
||
| 383 | * @return Carbon |
||
| 384 | */ |
||
| 385 | protected function freshTimestamp() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Cache the link between parent and related |
||
| 392 | * into the mapper's Entity Cache. |
||
| 393 | * |
||
| 394 | * @param EntityCollection|Mappable $results result of the relation query |
||
| 395 | * @param string $relation name of the relation method on the parent entity |
||
| 396 | * @return void |
||
| 397 | */ |
||
| 398 | protected function cacheRelation($results, $relation) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Return Pivot attributes when available on a relationship |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | public function getPivotAttributes() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Get a combo type.primaryKey |
||
| 417 | * |
||
| 418 | * @param Mappable $entity |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | View Code Duplication | protected function getEntityHash(Mappable $entity) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Run synchronization content if needed by the |
||
| 432 | * relation type. |
||
| 433 | * |
||
| 434 | * @param array $actualContent |
||
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | abstract public function sync(array $actualContent); |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Handle dynamic method calls to the relationship. |
||
| 441 | * |
||
| 442 | * @param string $method |
||
| 443 | * @param array $parameters |
||
| 444 | * @return mixed |
||
| 445 | */ |
||
| 446 | public function __call($method, $parameters) |
||
| 456 | } |
||
| 457 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: