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 | abstract class Model extends BaseModel |
||
14 | { |
||
15 | /** |
||
16 | * {@inheritdoc} |
||
17 | */ |
||
18 | protected $primaryKey = '_key'; |
||
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | protected $keyType = 'string'; |
||
24 | |||
25 | /** |
||
26 | * Get collection name |
||
27 | * |
||
28 | * @return string |
||
29 | */ |
||
30 | public function getCollection() |
||
31 | { |
||
32 | return $this->getTable(); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Set collection name |
||
37 | * |
||
38 | * @param string $collection |
||
39 | */ |
||
40 | public function setCollection($collection) |
||
41 | { |
||
42 | $this->table = $collection; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | public function qualifyColumn($column) |
||
49 | { |
||
50 | if (Str::contains($column, '.')) { |
||
51 | return $column; |
||
52 | } |
||
53 | |||
54 | return $this->getEntityName().'.'.$column; |
||
55 | } |
||
56 | |||
57 | public function getEntityName() |
||
58 | { |
||
59 | return getEntityName($this->getCollection()); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function newFromBuilder($attributes = [], $connection = null) |
||
66 | { |
||
67 | $model = $this->newInstance([], true); |
||
68 | |||
69 | $attributesResult = []; |
||
70 | foreach ($attributes as $key => $value) { |
||
71 | if (is_array($value) && $this->getEntityName() === $key) { |
||
72 | $attributesResult = array_merge($attributesResult, $value); |
||
73 | continue; |
||
74 | } |
||
75 | $attributesResult[$key] = $value; |
||
76 | } |
||
77 | |||
78 | $model->setRawAttributes((array) $attributesResult, true); |
||
79 | |||
80 | $model->setConnection($connection ?: $this->getConnectionName()); |
||
81 | |||
82 | $model->fireModelEvent('retrieved', false); |
||
83 | |||
84 | return $model; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | View Code Duplication | public function hasOne($related, $foreignKey = null, $localKey = null) |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | View Code Duplication | public function hasMany($related, $foreignKey = null, $localKey = null) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function newBelongsTo(BaseBuilder $query, BaseModel $child, $foreignKey, $ownerKey, $relation) |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function newBelongsToMany(BaseBuilder $query, BaseModel $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null) |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | protected function newBaseQueryBuilder() |
||
142 | } |
||
143 |
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.