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 |
||
12 | abstract class Model extends BaseModel |
||
13 | { |
||
14 | /** |
||
15 | * @inheritdoc |
||
16 | */ |
||
17 | protected $primaryKey = '_key'; |
||
18 | |||
19 | /** |
||
20 | * @inheritdoc |
||
21 | */ |
||
22 | protected $keyType = 'string'; |
||
23 | |||
24 | /** |
||
25 | * Get collection name |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | function getCollection(){ |
||
32 | |||
33 | /** |
||
34 | * Set collection name |
||
35 | * |
||
36 | * @param string $collection |
||
37 | */ |
||
38 | function setCollection($collection){ |
||
41 | |||
42 | /** |
||
43 | * @inheritdoc |
||
44 | */ |
||
45 | protected function newBaseQueryBuilder() |
||
53 | |||
54 | /** |
||
55 | * @inheritdoc |
||
56 | */ |
||
57 | public function qualifyColumn($column) |
||
65 | |||
66 | public function getEntityName(){ |
||
69 | |||
70 | /** |
||
71 | * @inheritdoc |
||
72 | */ |
||
73 | public function newFromBuilder($attributes = [], $connection = null) |
||
95 | |||
96 | /** |
||
97 | * @inheritdoc |
||
98 | */ |
||
99 | View Code Duplication | public function hasOne($related, $foreignKey = null, $localKey = null) |
|
109 | |||
110 | /** |
||
111 | * @inheritdoc |
||
112 | */ |
||
113 | View Code Duplication | public function hasMany($related, $foreignKey = null, $localKey = null) |
|
123 | |||
124 | /** |
||
125 | * @inheritdoc |
||
126 | */ |
||
127 | function newBelongsTo(BaseBuilder $query, BaseModel $child, $foreignKey, $ownerKey, $relation) |
||
131 | |||
132 | /** |
||
133 | * @inheritdoc |
||
134 | */ |
||
135 | function newBelongsToMany(BaseBuilder $query, BaseModel $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null) |
||
139 | } |
||
140 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.