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 |
||
31 | abstract class JsonApiController extends Controller |
||
32 | { |
||
33 | /** |
||
34 | * @var JsonApiSerializer |
||
35 | */ |
||
36 | protected $serializer; |
||
37 | |||
38 | /** |
||
39 | * @param JsonApiSerializer $serializer |
||
40 | */ |
||
41 | public function __construct(JsonApiSerializer $serializer) |
||
45 | |||
46 | /** |
||
47 | * @return \Symfony\Component\HttpFoundation\Response |
||
48 | */ |
||
49 | public function listAction() |
||
61 | |||
62 | /** |
||
63 | * Returns the total number of results available for the current resource. |
||
64 | * |
||
65 | * @return callable |
||
66 | */ |
||
67 | View Code Duplication | protected function totalAmountResourceCallable() |
|
75 | |||
76 | /** |
||
77 | * Returns an Eloquent Model. |
||
78 | * |
||
79 | * @return Model |
||
80 | */ |
||
81 | abstract public function getDataModel(); |
||
82 | |||
83 | /** |
||
84 | * Returns a list of resources based on pagination criteria. |
||
85 | * |
||
86 | * @return callable |
||
87 | */ |
||
88 | protected function listResourceCallable() |
||
94 | |||
95 | /** |
||
96 | * @param Response $response |
||
97 | * |
||
98 | * @return \Symfony\Component\HttpFoundation\Response |
||
99 | */ |
||
100 | protected function addHeaders(Response $response) |
||
104 | |||
105 | /** |
||
106 | * @param Request $request |
||
107 | * |
||
108 | * @return \Symfony\Component\HttpFoundation\Response |
||
109 | */ |
||
110 | View Code Duplication | public function getAction(Request $request) |
|
117 | |||
118 | /** |
||
119 | * @param Request $request |
||
120 | * |
||
121 | * @return callable |
||
122 | */ |
||
123 | protected function findResourceCallable(Request $request) |
||
131 | |||
132 | /** |
||
133 | * @param Request $request |
||
134 | * |
||
135 | * @return \Symfony\Component\HttpFoundation\Response |
||
136 | */ |
||
137 | View Code Duplication | public function postAction(Request $request) |
|
147 | |||
148 | /** |
||
149 | * Reads the input and creates and saves a new Eloquent Model. |
||
150 | * |
||
151 | * @return callable |
||
152 | */ |
||
153 | protected function createResourceCallable() |
||
176 | |||
177 | /** |
||
178 | * @param Request $request |
||
179 | * |
||
180 | * @return \Symfony\Component\HttpFoundation\Response |
||
181 | */ |
||
182 | View Code Duplication | public function patchAction(Request $request) |
|
199 | |||
200 | /** |
||
201 | * @return callable |
||
202 | */ |
||
203 | protected function updateResourceCallable() |
||
217 | |||
218 | /** |
||
219 | * @param Request $request |
||
220 | * |
||
221 | * @return \Symfony\Component\HttpFoundation\Response |
||
222 | */ |
||
223 | View Code Duplication | public function putAction(Request $request) |
|
240 | |||
241 | /** |
||
242 | * @param Request $request |
||
243 | * |
||
244 | * @return \Symfony\Component\HttpFoundation\Response |
||
245 | */ |
||
246 | View Code Duplication | public function deleteAction(Request $request) |
|
253 | } |
||
254 |
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.