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 App\Repositories; |
||
10 | class SnapshotDetailsRepository extends Repository { |
||
11 | |||
12 | public function getModelName() |
||
16 | |||
17 | View Code Duplication | public function get($id) |
|
|
|||
18 | { |
||
19 | $this->validateID($id); |
||
20 | |||
21 | try { |
||
22 | return $this->model->findOrFail($id); |
||
23 | } |
||
24 | catch(\Exception $e) { |
||
25 | throw new RepositoryException('Could not retrieve snapshot detail', RepositoryException::DATABASE_ERROR); |
||
26 | } |
||
27 | } |
||
28 | |||
29 | public function fromSnapshot($id) |
||
30 | { |
||
31 | $this->validateID($id); |
||
32 | |||
33 | try { |
||
34 | return $this->model->group( Session::get('groupID') ) |
||
35 | ->leftJoin('users', 'snapshot_details.user_id', '=', 'users.user_id') |
||
36 | ->where('snapshot_details.cs_id', '=', $id) |
||
37 | ->get(); |
||
38 | } |
||
39 | catch(Exception $e) { |
||
40 | throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | public function APIFormat($object) |
||
60 | |||
61 | public function store(array $data) |
||
62 | { |
||
87 | |||
88 | View Code Duplication | public function delete($id) |
|
99 | |||
100 | public function validate(array $data) |
||
120 | |||
121 | private function formatRecord($object) { |
||
136 | |||
137 | } |
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.