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 |
||
| 13 | trait Relations |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @param string $entity |
||
| 17 | * @param string|null $field |
||
| 18 | * @param callable|null $callback |
||
| 19 | * |
||
| 20 | * @return OneToOne |
||
| 21 | */ |
||
| 22 | 5 | public function hasOne($entity, $field = null, callable $callback = null) |
|
| 23 | { |
||
| 24 | 5 | return $this->oneToOne($entity, $field, $callback)->ownedBy( |
|
| 25 | 5 | $this->guessSingularField($entity) |
|
| 26 | 5 | ); |
|
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param string $entity |
||
| 31 | * @param string|null $field |
||
| 32 | * @param callable|null $callback |
||
| 33 | * |
||
| 34 | * @return OneToOne |
||
| 35 | */ |
||
| 36 | 7 | View Code Duplication | public function oneToOne($entity, $field = null, callable $callback = null) |
| 37 | { |
||
| 38 | 7 | return $this->addRelation( |
|
| 39 | 7 | new OneToOne( |
|
| 40 | 7 | $this->getBuilder(), |
|
| 41 | 7 | $this->getNamingStrategy(), |
|
| 42 | 7 | $this->guessSingularField($entity, $field), |
|
| 43 | $entity |
||
| 44 | 7 | ), |
|
| 45 | $callback |
||
| 46 | 7 | ); |
|
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $entity |
||
| 51 | * @param string|null $field |
||
| 52 | * @param callable|null $callback |
||
| 53 | * |
||
| 54 | * @return ManyToOne |
||
| 55 | */ |
||
| 56 | 4 | public function belongsTo($entity, $field = null, callable $callback = null) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $entity |
||
| 63 | * @param string|null $field |
||
| 64 | * @param callable|null $callback |
||
| 65 | * |
||
| 66 | * @return ManyToOne |
||
| 67 | */ |
||
| 68 | 6 | View Code Duplication | public function manyToOne($entity, $field = null, callable $callback = null) |
| 69 | { |
||
| 70 | 6 | return $this->addRelation( |
|
| 71 | 6 | new ManyToOne( |
|
| 72 | 6 | $this->getBuilder(), |
|
| 73 | 6 | $this->getNamingStrategy(), |
|
| 74 | 6 | $this->guessSingularField($entity, $field), |
|
| 75 | $entity |
||
| 76 | 6 | ), |
|
| 77 | $callback |
||
| 78 | 6 | ); |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $entity |
||
| 83 | * @param string|null $field |
||
| 84 | * @param callable|null $callback |
||
| 85 | * |
||
| 86 | * @return OneToMany |
||
| 87 | */ |
||
| 88 | 4 | public function hasMany($entity, $field = null, callable $callback = null) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $entity |
||
| 95 | * @param string|null $field |
||
| 96 | * @param callable|null $callback |
||
| 97 | * |
||
| 98 | * @return OneToMany |
||
| 99 | */ |
||
| 100 | 6 | View Code Duplication | public function oneToMany($entity, $field = null, callable $callback = null) |
| 101 | { |
||
| 102 | 6 | return $this->addRelation( |
|
| 103 | 6 | new OneToMany( |
|
| 104 | 6 | $this->getBuilder(), |
|
| 105 | 6 | $this->getNamingStrategy(), |
|
| 106 | 6 | $this->guessPluralField($entity, $field), |
|
| 107 | $entity |
||
| 108 | 6 | ), |
|
| 109 | $callback |
||
| 110 | 6 | ); |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $entity |
||
| 115 | * @param string|null $field |
||
| 116 | * @param callable|null $callback |
||
| 117 | * |
||
| 118 | * @return ManyToMany |
||
| 119 | */ |
||
| 120 | 4 | public function belongsToMany($entity, $field = null, callable $callback = null) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $entity |
||
| 127 | * @param string|null $field |
||
| 128 | * @param callable|null $callback |
||
| 129 | * |
||
| 130 | * @return ManyToMany |
||
| 131 | */ |
||
| 132 | 7 | View Code Duplication | public function manyToMany($entity, $field = null, callable $callback = null) |
| 133 | { |
||
| 134 | 7 | return $this->addRelation( |
|
| 135 | 7 | new ManyToMany( |
|
| 136 | 7 | $this->getBuilder(), |
|
| 137 | 7 | $this->getNamingStrategy(), |
|
| 138 | 7 | $this->guessPluralField($entity, $field), |
|
| 139 | $entity |
||
| 140 | 7 | ), |
|
| 141 | $callback |
||
| 142 | 7 | ); |
|
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Adds a custom relation to the entity. |
||
| 147 | * |
||
| 148 | * @param Relation $relation |
||
| 149 | * @param callable|null $callback |
||
| 150 | * |
||
| 151 | * @return Relation |
||
| 152 | */ |
||
| 153 | 21 | public function addRelation(Relation $relation, callable $callback = null) |
|
| 154 | { |
||
| 155 | 21 | $this->callbackAndQueue($relation, $callback); |
|
| 156 | |||
| 157 | 21 | return $relation; |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $entity |
||
| 162 | * @param string|null $field |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | 17 | protected function guessSingularField($entity, $field = null) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $entity |
||
| 173 | * @param string|null $field |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | 11 | protected function guessPluralField($entity, $field = null) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @return \Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder |
||
| 184 | */ |
||
| 185 | abstract public function getBuilder(); |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param Buildable $buildable |
||
| 189 | * @param callable|null $callback |
||
| 190 | */ |
||
| 191 | abstract protected function callbackAndQueue(Buildable $buildable, callable $callback = null); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return \Doctrine\ORM\Mapping\NamingStrategy |
||
| 195 | */ |
||
| 196 | abstract public function getNamingStrategy(); |
||
| 197 | } |
||
| 198 |