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 |
||
17 | class ErrorResponseBuilder extends ResponseBuilder |
||
18 | { |
||
19 | /** |
||
20 | * Optional error data appended with the response. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $data = []; |
||
25 | |||
26 | /** |
||
27 | * The error code used to identify the error. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $errorCode; |
||
32 | |||
33 | /** |
||
34 | * A descriptive error message explaining what went wrong. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $message; |
||
39 | |||
40 | /** |
||
41 | * Any parameters used to build the error message. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $parameters = []; |
||
46 | |||
47 | /** |
||
48 | * The HTTP status code for the response. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $statusCode = 500; |
||
53 | |||
54 | /** |
||
55 | * Translator service used for translating stuff. |
||
56 | * |
||
57 | * @var \Symfony\Component\Translation\TranslatorInterface |
||
58 | */ |
||
59 | protected $translator; |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory |
||
65 | * @param \Symfony\Component\Translation\TranslatorInterface $translator |
||
66 | */ |
||
67 | public function __construct(ResponseFactory $responseFactory, TranslatorInterface $translator) |
||
73 | |||
74 | /** |
||
75 | * Add additonal data appended to the error object. |
||
76 | * |
||
77 | * @param array $data |
||
78 | * @return self |
||
79 | */ |
||
80 | public function addData(array $data):ErrorResponseBuilder |
||
86 | |||
87 | /** |
||
88 | * Set the error code and optionally an error message. |
||
89 | * |
||
90 | * @param string $errorCode |
||
|
|||
91 | * @param string|array|null $message |
||
92 | * @return self |
||
93 | */ |
||
94 | public function setError(string $errorCode = null, $message = null):ErrorResponseBuilder |
||
106 | |||
107 | /** |
||
108 | * Set the HTTP status code for the response. |
||
109 | * |
||
110 | * @param int $statusCode |
||
111 | * @return self |
||
112 | * @throws \InvalidArgumentException |
||
113 | */ |
||
114 | View Code Duplication | public function setStatus(int $statusCode):ResponseBuilder |
|
122 | |||
123 | /** |
||
124 | * Serialize the data and return as an array. |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | public function toArray():array |
||
135 | |||
136 | /** |
||
137 | * Build the error object of the serialized response data. |
||
138 | * |
||
139 | * @return array|null |
||
140 | */ |
||
141 | protected function buildErrorData() |
||
154 | |||
155 | /** |
||
156 | * Resolve an error message from the translator. |
||
157 | * |
||
158 | * @return string|null |
||
159 | */ |
||
160 | protected function resolveMessage() |
||
168 | } |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.