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 BaseApiController extends Controller |
||
14 | { |
||
15 | private $statusCode = 200; |
||
16 | |||
17 | const CODE_WRONG_ARGS = 'GEN-ARGUMENTS'; |
||
18 | const CODE_NOT_FOUND = 'GEN-NOTFOUND'; |
||
19 | const CODE_INTERNAL_ERROR = 'GEN-SERVERERROR'; |
||
20 | const CODE_UNAUTHORIZED = 'GEN-UNAUTHORIZED'; |
||
21 | const CODE_FORBIDDEN = 'GEN-FORBIDDEN'; |
||
22 | |||
23 | /** |
||
24 | * Getter for statusCode. |
||
25 | * |
||
26 | * @return int |
||
27 | */ |
||
28 | public function getStatusCode() |
||
32 | |||
33 | /** |
||
34 | * Setter for statusCode. |
||
35 | * |
||
36 | * @param int $statusCode Value to set |
||
37 | * |
||
38 | * @return self |
||
39 | */ |
||
40 | public function setStatusCode($statusCode) |
||
46 | |||
47 | /** |
||
48 | * @param mixed $item |
||
49 | * @param $callback |
||
50 | * |
||
51 | * @return JsonResponse |
||
52 | */ |
||
53 | View Code Duplication | protected function respondWithItem($item, $callback) |
|
60 | |||
61 | /** |
||
62 | * @param mixed $collection |
||
63 | * @param $callback |
||
64 | * |
||
65 | * @return JsonResponse |
||
66 | */ |
||
67 | View Code Duplication | protected function respondWithCollection($collection, $callback) |
|
74 | |||
75 | /** |
||
76 | * @param string $message |
||
77 | * @param int $errorCode |
||
78 | * |
||
79 | * @return JsonResponse |
||
80 | */ |
||
81 | protected function respondWithArray(array $array, array $headers = []) |
||
85 | |||
86 | /** |
||
87 | * @param string $message |
||
88 | * @param int $errorCode |
||
89 | * |
||
90 | * @return JsonResponse |
||
91 | */ |
||
92 | protected function respondWithError($message, $errorCode) |
||
109 | |||
110 | /** |
||
111 | * Generates a Response with a 403 HTTP header and a given message. |
||
112 | * |
||
113 | * @return JsonResponse |
||
114 | */ |
||
115 | public function errorForbidden($message = 'Forbidden') |
||
120 | |||
121 | /** |
||
122 | * Generates a Response with a 500 HTTP header and a given message. |
||
123 | * |
||
124 | * @return JsonResponse |
||
125 | */ |
||
126 | public function errorInternalError($message = 'Internal Error') |
||
131 | |||
132 | /** |
||
133 | * Generates a Response with a 404 HTTP header and a given message. |
||
134 | * |
||
135 | * @return JsonResponse |
||
136 | */ |
||
137 | public function errorNotFound($message = 'Resource Not Found') |
||
142 | |||
143 | /** |
||
144 | * Generates a Response with a 401 HTTP header and a given message. |
||
145 | * |
||
146 | * @return JsonResponse |
||
147 | */ |
||
148 | public function errorUnauthorized($message = 'Unauthorized') |
||
153 | |||
154 | /** |
||
155 | * Generates a Response with a 400 HTTP header and a given message. |
||
156 | * |
||
157 | * @return JsonResponse |
||
158 | */ |
||
159 | public function errorWrongArgs($message = 'Wrong Arguments') |
||
164 | } |
||
165 |
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.