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 Anomaly\Streams\Platform\Http\Controller; |
||
18 | class AssignmentsController extends AdminController |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * The stream namespace. |
||
23 | * |
||
24 | * @var null|string |
||
25 | */ |
||
26 | protected $namespace = null; |
||
27 | |||
28 | /** |
||
29 | * Return an index of existing assignments. |
||
30 | * |
||
31 | * @param AssignmentTableBuilder $table |
||
32 | * @param StreamRepositoryInterface $streams |
||
33 | * @return \Symfony\Component\HttpFoundation\Response |
||
34 | */ |
||
35 | View Code Duplication | public function index(AssignmentTableBuilder $table, StreamRepositoryInterface $streams) |
|
49 | |||
50 | /** |
||
51 | * Choose a field to assign. |
||
52 | * |
||
53 | * @param FieldRepositoryInterface $fields |
||
54 | * @param StreamRepositoryInterface $streams |
||
55 | * @param ModuleCollection $modules |
||
56 | * @return \Illuminate\Contracts\View\View|mixed |
||
57 | */ |
||
58 | public function choose( |
||
77 | |||
78 | /** |
||
79 | * Create a new assignment. |
||
80 | * |
||
81 | * @param AssignmentFormBuilder $builder |
||
82 | * @param StreamRepositoryInterface $streams |
||
83 | * @param FieldRepositoryInterface $fields |
||
84 | * @return \Symfony\Component\HttpFoundation\Response |
||
85 | */ |
||
86 | public function create( |
||
104 | |||
105 | /** |
||
106 | * Edit an existing assignment. |
||
107 | * |
||
108 | * @param AssignmentFormBuilder $builder |
||
109 | * @param StreamRepositoryInterface $streams |
||
110 | * @return \Symfony\Component\HttpFoundation\Response |
||
111 | */ |
||
112 | View Code Duplication | public function edit(AssignmentFormBuilder $builder, StreamRepositoryInterface $streams) |
|
123 | |||
124 | /** |
||
125 | * Get the namespace. |
||
126 | * |
||
127 | * @return null|string |
||
128 | */ |
||
129 | public function getNamespace() |
||
133 | } |
||
134 |
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.