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 | final class ResponseFactory |
||
14 | { |
||
15 | const CODE_WRONG_ARGS = 'GEN-ARGUMENTS'; |
||
16 | const CODE_NOT_FOUND = 'GEN-NOTFOUND'; |
||
17 | const CODE_INTERNAL_ERROR = 'GEN-SERVERERROR'; |
||
18 | const CODE_UNAUTHORIZED = 'GEN-UNAUTHORIZED'; |
||
19 | const CODE_FORBIDDEN = 'GEN-FORBIDDEN'; |
||
20 | |||
21 | /** |
||
22 | * @var Manager |
||
23 | */ |
||
24 | private $fractal; |
||
25 | |||
26 | /** |
||
27 | * @param Manager $fractal |
||
28 | */ |
||
29 | public function __construct(Manager $fractal) |
||
33 | |||
34 | /** |
||
35 | * @param mixed $item |
||
36 | * @param $callback |
||
37 | * |
||
38 | * @return JsonResponse |
||
39 | */ |
||
40 | View Code Duplication | public function createWithItem($item, $callback) |
|
47 | |||
48 | /** |
||
49 | * @param mixed $collection |
||
50 | * @param $callback |
||
51 | * |
||
52 | * @return JsonResponse |
||
53 | */ |
||
54 | View Code Duplication | public function createWithCollection($collection, $callback) |
|
61 | |||
62 | /** |
||
63 | * @param array $array |
||
64 | * @param int $statusCode |
||
65 | * @param array $headers |
||
66 | * |
||
67 | * @return JsonResponse |
||
68 | */ |
||
69 | public function createWithArray(array $array, $statusCode = 200, array $headers = []) |
||
73 | |||
74 | /** |
||
75 | * @param string $message |
||
76 | * @param int $statusCode |
||
77 | * @param int $errorCode |
||
78 | * |
||
79 | * @return JsonResponse |
||
80 | */ |
||
81 | public function createWithError($message, $statusCode, $errorCode) |
||
98 | |||
99 | /** |
||
100 | * Generates a Response with a 403 HTTP header and a given message. |
||
101 | * |
||
102 | * @return JsonResponse |
||
103 | */ |
||
104 | public function createForbidden($message = 'Forbidden') |
||
108 | |||
109 | /** |
||
110 | * Generates a Response with a 500 HTTP header and a given message. |
||
111 | * |
||
112 | * @return JsonResponse |
||
113 | */ |
||
114 | public function createInternalError($message = 'Internal Error') |
||
118 | |||
119 | /** |
||
120 | * Generates a Response with a 404 HTTP header and a given message. |
||
121 | * |
||
122 | * @return JsonResponse |
||
123 | */ |
||
124 | public function createNotFound($message = 'Resource Not Found') |
||
128 | |||
129 | /** |
||
130 | * Generates a Response with a 401 HTTP header and a given message. |
||
131 | * |
||
132 | * @return JsonResponse |
||
133 | */ |
||
134 | public function createUnauthorized($message = 'Unauthorized') |
||
138 | |||
139 | /** |
||
140 | * Generates a Response with a 400 HTTP header and a given message. |
||
141 | * |
||
142 | * @return JsonResponse |
||
143 | */ |
||
144 | public function createWrongArgs($message = 'Wrong Arguments') |
||
148 | } |
||
149 |
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.