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 |
||
15 | class ErrorResponseBuilder extends ResponseBuilder |
||
16 | { |
||
17 | /** |
||
18 | * Optional error data appended with the response. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $data = []; |
||
23 | |||
24 | /** |
||
25 | * The error code used to identify the error. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $errorCode; |
||
30 | |||
31 | /** |
||
32 | * A descriptive error message explaining what went wrong. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $message; |
||
37 | |||
38 | /** |
||
39 | * Any parameters used to build the error message. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $parameters = []; |
||
44 | |||
45 | /** |
||
46 | * The HTTP status code for the response. |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $statusCode = 500; |
||
51 | |||
52 | /** |
||
53 | * Translator service used for translating stuff. |
||
54 | * |
||
55 | * @var \Symfony\Component\Translation\TranslatorInterface|Illuminate\Contracts\Translation\Translator |
||
56 | */ |
||
57 | protected $translator; |
||
58 | |||
59 | /** |
||
60 | * Constructor. |
||
61 | * |
||
62 | * @param \Illuminate\Contracts\Routing\ResponseFactory|\Laravel\Lumen\Http\ResponseFactory $responseFactory |
||
63 | * @param \Symfony\Component\Translation\TranslatorInterface|Illuminate\Contracts\Translation\Translator $translator |
||
64 | */ |
||
65 | public function __construct($responseFactory, $translator) |
||
71 | |||
72 | /** |
||
73 | * Add additonal data appended to the error object. |
||
74 | * |
||
75 | * @param array $data |
||
76 | * @return self |
||
77 | */ |
||
78 | public function addData(array $data):ErrorResponseBuilder |
||
84 | |||
85 | /** |
||
86 | * Set the error code and optionally an error message. |
||
87 | * |
||
88 | * @param mixed|null $errorCode |
||
89 | * @param string|array|null $message |
||
90 | * @return self |
||
91 | */ |
||
92 | public function setError($errorCode = null, $message = null):ErrorResponseBuilder |
||
104 | |||
105 | /** |
||
106 | * Set the HTTP status code for the response. |
||
107 | * |
||
108 | * @param int $statusCode |
||
109 | * @return self |
||
110 | * @throws \InvalidArgumentException |
||
111 | */ |
||
112 | View Code Duplication | public function setStatus(int $statusCode):ResponseBuilder |
|
119 | |||
120 | /** |
||
121 | * Return response success flag as true |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | public function isSuccessResponse():bool |
||
129 | |||
130 | /** |
||
131 | * Serialize the data and return as an array. |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | public function toArray():array |
||
141 | |||
142 | /** |
||
143 | * Build the error object of the serialized response data. |
||
144 | * |
||
145 | * @return array|null |
||
146 | */ |
||
147 | protected function buildErrorData() |
||
160 | |||
161 | /** |
||
162 | * Resolve an error message from the translator. |
||
163 | * |
||
164 | * @return string|null |
||
165 | */ |
||
166 | protected function resolveMessage() |
||
174 | } |
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.