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 string $model foreign model class |
||
24 | * @param string $tablename pivot table name |
||
25 | * @param string $foreignKey identifying key on foreign model |
||
26 | * @param string $localKey identifying key on local model |
||
27 | * @param Model $relation |
||
28 | */ |
||
29 | public function __construct($model, $tablename, $foreignKey, $localKey, Model $relation) |
||
35 | |||
36 | View Code Duplication | protected function initQuery() |
|
50 | |||
51 | public function getResults() |
||
59 | |||
60 | /** |
||
61 | * Gets the pivot tablename. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public function getTablename() |
||
69 | |||
70 | public function create(array $values = []) |
||
95 | } |
||
96 |
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.