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 namespace Modules\Core\Repositories\Cache; |
||
5 | abstract class BaseCacheDecorator implements BaseRepository |
||
6 | { |
||
7 | /** |
||
8 | * @var \Modules\Core\Repositories\BaseRepository |
||
9 | */ |
||
10 | protected $repository; |
||
11 | /** |
||
12 | * @var \Illuminate\Cache\Repository |
||
13 | */ |
||
14 | protected $cache; |
||
15 | /** |
||
16 | * @var string The entity name |
||
17 | */ |
||
18 | protected $entityName; |
||
19 | /** |
||
20 | * @var string The application locale |
||
21 | */ |
||
22 | protected $locale; |
||
23 | |||
24 | /** |
||
25 | * @var int caching time |
||
26 | */ |
||
27 | protected $cacheTime; |
||
28 | |||
29 | public function __construct() |
||
35 | |||
36 | /** |
||
37 | * @param int $id |
||
38 | * @return mixed |
||
39 | */ |
||
40 | View Code Duplication | public function find($id) |
|
50 | |||
51 | /** |
||
52 | * @return \Illuminate\Database\Eloquent\Collection |
||
53 | */ |
||
54 | View Code Duplication | public function all() |
|
64 | |||
65 | /** |
||
66 | * Return all categories in the given language |
||
67 | * |
||
68 | * @param string $lang |
||
69 | * @return object |
||
70 | */ |
||
71 | View Code Duplication | public function allTranslatedIn($lang) |
|
81 | |||
82 | /** |
||
83 | * Find a resource by the given slug |
||
84 | * @param string $slug |
||
85 | * @return object |
||
86 | */ |
||
87 | View Code Duplication | public function findBySlug($slug) |
|
97 | |||
98 | /** |
||
99 | * Create a resource |
||
100 | * |
||
101 | * @param $data |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function create($data) |
||
110 | |||
111 | /** |
||
112 | * Update a resource |
||
113 | * |
||
114 | * @param $model |
||
115 | * @param array $data |
||
116 | * @return mixed |
||
117 | */ |
||
118 | public function update($model, $data) |
||
124 | |||
125 | /** |
||
126 | * Destroy a resource |
||
127 | * |
||
128 | * @param $model |
||
129 | * @return mixed |
||
130 | */ |
||
131 | public function destroy($model) |
||
137 | |||
138 | /** |
||
139 | * Find a resource by an array of attributes |
||
140 | * @param array $attributes |
||
141 | * @return object |
||
142 | */ |
||
143 | View Code Duplication | public function findByAttributes(array $attributes) |
|
155 | |||
156 | /** |
||
157 | * Get resources by an array of attributes |
||
158 | * @param array $attributes |
||
159 | * @param null|string $orderBy |
||
160 | * @param string $sortOrder |
||
161 | * @return \Illuminate\Database\Eloquent\Collection |
||
162 | */ |
||
163 | public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc') |
||
175 | |||
176 | /** |
||
177 | * Return a collection of elements who's ids match |
||
178 | * @param array $ids |
||
179 | * @return mixed |
||
180 | */ |
||
181 | View Code Duplication | public function findByMany(array $ids) |
|
193 | |||
194 | /** |
||
195 | * Clear the cache for this Repositories' Entity |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function clearCache() |
||
202 | } |
||
203 |
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.