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 |
||
15 | class BelongsToMany extends Relation |
||
16 | { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $tablename; |
||
21 | |||
22 | /** |
||
23 | * @param Model $localModel |
||
24 | * @param string $localKey identifying key on local model |
||
25 | * @param string $tablename pivot table name |
||
26 | * @param string $foreignModel foreign model class |
||
27 | * @param string $foreignKey identifying key on foreign model |
||
28 | */ |
||
29 | public function __construct(Model $localModel, $localKey, $tablename, $foreignModel, $foreignKey) |
||
35 | |||
36 | protected function initQuery() |
||
51 | |||
52 | public function getResults() |
||
60 | |||
61 | /** |
||
62 | * Gets the pivot tablename. |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | public function getTablename() |
||
70 | |||
71 | View Code Duplication | public function create(array $values = []) |
|
81 | |||
82 | /** |
||
83 | * Attaches a model to the relationship by creating |
||
84 | * a pivot model. |
||
85 | * |
||
86 | * @param Model $model |
||
87 | * |
||
88 | * @return self |
||
89 | */ |
||
90 | public function attach(Model $model) |
||
113 | |||
114 | /** |
||
115 | * Detaches a model from the relationship by deleting |
||
116 | * the pivot model. |
||
117 | * |
||
118 | * @param Model $model |
||
119 | * |
||
120 | * @return self |
||
121 | */ |
||
122 | public function detach(Model $model) |
||
129 | } |
||
130 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.