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 |
||
| 27 | class ManyToMorphed implements RelationInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Nested ManyToMany relations. |
||
| 31 | * |
||
| 32 | * @var ManyToMany[] |
||
| 33 | */ |
||
| 34 | private $relations = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Parent Record caused relation to be created. |
||
| 38 | * |
||
| 39 | * @var RecordInterface |
||
| 40 | */ |
||
| 41 | protected $parent = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Relation definition fetched from ORM schema. Must already be normalized by RelationSchema. |
||
| 45 | * |
||
| 46 | * @invisible |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $definition = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @invisible |
||
| 54 | * |
||
| 55 | * @var ORM |
||
| 56 | */ |
||
| 57 | protected $orm = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | public function __construct( |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | public function isLoaded() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | * |
||
| 86 | * We can return self: |
||
| 87 | * $tag->tagged->users->count(); |
||
| 88 | */ |
||
| 89 | public function getRelated() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | public function associate(EntityInterface $related = null) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function saveRelated($validate = true) |
||
| 106 | { |
||
| 107 | foreach ($this->relations as $relation) { |
||
| 108 | if (!$relation->saveRelated($validate)) { |
||
| 109 | return false; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return true; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | public function reset(array $data = [], $loaded = false) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | public function isValid() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritdoc} |
||
| 146 | */ |
||
| 147 | public function hasErrors() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | public function getErrors($reset = false) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Count method will work with pivot table directly. |
||
| 175 | * |
||
| 176 | * @return int |
||
| 177 | */ |
||
| 178 | public function count() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get access to data instance stored in nested relation. |
||
| 189 | * |
||
| 190 | * Example: |
||
| 191 | * $tag->tagged->users; |
||
| 192 | * $tag->tagged->posts; |
||
| 193 | * |
||
| 194 | * @param string $alias |
||
| 195 | * |
||
| 196 | * @return RecordEntity|RecordIterator |
||
| 197 | */ |
||
| 198 | public function __get($alias) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get access to sub relation. |
||
| 205 | * |
||
| 206 | * Example: |
||
| 207 | * $tag->tagged->users()->count(); |
||
| 208 | * foreach($tag->tagged->users()->find(["status" => "active"]) as $user) |
||
| 209 | * { |
||
| 210 | * } |
||
| 211 | * |
||
| 212 | * @param string $alias |
||
| 213 | * @param array $arguments |
||
| 214 | * |
||
| 215 | * @return ManyToMany |
||
| 216 | */ |
||
| 217 | public function __call($alias, array $arguments) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Link morphed record to relation. Method will bypass request to appropriate nested relation. |
||
| 224 | * |
||
| 225 | * @param RecordInterface $record |
||
| 226 | * @param array $pivotData Custom pivot data. |
||
| 227 | */ |
||
| 228 | public function link(RecordInterface $record, array $pivotData = []) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Unlink morphed record from relation. |
||
| 235 | * |
||
| 236 | * @param RecordInterface $record |
||
| 237 | * |
||
| 238 | * @return int |
||
| 239 | */ |
||
| 240 | public function unlink(RecordInterface $record) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Unlink every associated record, method will return amount of affected rows. Method will |
||
| 247 | * unlink only records matched WHERE_PIVOT by default. Set wherePivot to false to unlink every |
||
| 248 | * record. |
||
| 249 | * |
||
| 250 | * @param bool $wherePivot Use conditions specified by WHERE_PIVOT, enabled by default. |
||
| 251 | * |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | public function unlinkAll($wherePivot = true) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get nested-relation associated with one of record morph aliases. |
||
| 271 | * |
||
| 272 | * @param string $alias |
||
| 273 | * |
||
| 274 | * @return ManyToMany |
||
| 275 | */ |
||
| 276 | protected function morphed($alias) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Instance of DBAL\Table associated with relation pivot table. |
||
| 316 | * |
||
| 317 | * @return Table |
||
| 318 | */ |
||
| 319 | protected function pivotTable() |
||
| 325 | } |
||
| 326 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.