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 | ||
| 7 | class SingleTableInheritanceScope implements ScopeInterface | ||
| 8 | { | ||
| 9 | /** | ||
| 10 | * Discriminator column name. | ||
| 11 | * | ||
| 12 | * @var string | ||
| 13 | */ | ||
| 14 | protected $column; | ||
| 15 | |||
| 16 | /** | ||
| 17 | * Discriminator column allowed types. | ||
| 18 | * | ||
| 19 | * @var array | ||
| 20 | */ | ||
| 21 | protected $types = []; | ||
| 22 | |||
| 23 | public function __construct(EntityMap $entityMap) | ||
| 24 |     { | ||
| 25 | // Putting the heavy logic in here, so we won't have | ||
| 26 | // to go through it each time we reach for a query | ||
| 27 | // builder. | ||
| 28 | |||
| 29 | $this->column = $entityMap->getDiscriminatorColumn(); | ||
| 30 | |||
| 31 | // First we need to retrieve the base class & it's normalized | ||
| 32 | // type string | ||
| 33 | $class = $entityMap->getClass(); | ||
| 34 | $this->types[] = $this->getTypeStringForEntity($class, $entityMap); | ||
| 35 | |||
| 36 | // Then, we parse all registered entities for any potential | ||
| 37 | // child class. | ||
| 38 | $classes = Manager::getInstance()->getRegisteredEntities(); | ||
| 39 | |||
| 40 |         foreach ($classes as $otherClass => $entityMap) { | ||
| 41 |             if (is_subclass_of($otherClass, $class)) { | ||
|  | |||
| 42 | $this->types[] = $this->getTypeStringForEntity($otherClass, $entityMap); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | /** | ||
| 48 | * Get the normalized value to use for query on discriminator column. | ||
| 49 | * | ||
| 50 | * @param string $class | ||
| 51 | * @param EntityMap $entityMap | ||
| 52 | * | ||
| 53 | * @return string | ||
| 54 | */ | ||
| 55 | protected function getTypeStringForEntity($class, EntityMap $entityMap) | ||
| 56 |     { | ||
| 57 | $class = $entityMap->getClass(); | ||
| 58 | |||
| 59 | $type = array_keys( | ||
| 60 | $entityMap->getDiscriminatorColumnMap(), | ||
| 61 | $class | ||
| 62 | ); | ||
| 63 | |||
| 64 |         if (count($type) == 0) { | ||
| 65 | return $class; | ||
| 66 | } | ||
| 67 | |||
| 68 | return $type[0]; | ||
| 69 | } | ||
| 70 | |||
| 71 | /** | ||
| 72 | * Apply the scope to a given Analogue query builder. | ||
| 73 | * | ||
| 74 | * @param \Analogue\ORM\System\Query $query | ||
| 75 | * | ||
| 76 | * @return void | ||
| 77 | */ | ||
| 78 | public function apply(Query $query) | ||
| 82 | |||
| 83 | /** | ||
| 84 | * Remove the scope from the given Analogue query builder. | ||
| 85 | * | ||
| 86 | * @param mixed $query | ||
| 87 | * | ||
| 88 | * @return void | ||
| 89 | */ | ||
| 90 | public function remove(Query $query) | ||
| 102 | |||
| 103 | /** | ||
| 104 | * Determine if the given where clause is a single table inheritance constraint. | ||
| 105 | * | ||
| 106 | * @param array $where | ||
| 107 | * @param string $column | ||
| 108 | * | ||
| 109 | * @return bool | ||
| 110 | */ | ||
| 111 | protected function isSingleTableConstraint(array $where, $column) | ||
| 115 | } | ||
| 116 |