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 |
||
7 | class ApiResponse |
||
8 | { |
||
9 | /** |
||
10 | * @var FractalAdapter |
||
11 | */ |
||
12 | private $fractalAdapter; |
||
13 | |||
14 | /** |
||
15 | * Response class. |
||
16 | * |
||
17 | * @param FractalAdapter $fractal |
||
18 | */ |
||
19 | 12 | public function __construct(FractalAdapter $fractal) |
|
23 | |||
24 | /** |
||
25 | * Returns one item. |
||
26 | * |
||
27 | * @param mixed $data |
||
28 | * @param null $transformer |
||
29 | * @param null $resourceKey |
||
30 | * @param bool $build |
||
31 | * |
||
32 | * @return array|\Illuminate\Http\Response|ResponseBuilder* |
||
|
|||
33 | */ |
||
34 | 2 | View Code Duplication | public function item($data, $transformer = null, $resourceKey = null, $build = true) |
1 ignored issue
–
show
|
|||
35 | { |
||
36 | 2 | return $this->toResponseBuilder( |
|
37 | 2 | $this->fractalAdapter->item($data, $transformer, $resourceKey), |
|
38 | $build |
||
39 | 2 | ); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Returns collection. |
||
44 | * |
||
45 | * @param $data |
||
46 | * @param null $transformer |
||
47 | * @param null $resourceKey |
||
48 | * @param bool $build |
||
49 | * |
||
50 | * @return array|\Illuminate\Http\Response|ResponseBuilder |
||
51 | */ |
||
52 | 2 | View Code Duplication | public function collection($data, $transformer = null, $resourceKey = null, $build = true) |
1 ignored issue
–
show
|
|||
53 | { |
||
54 | 2 | return $this->toResponseBuilder( |
|
55 | 2 | $this->fractalAdapter->collection($data, $transformer, $resourceKey), |
|
56 | $build |
||
57 | 2 | ); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Returns paginated collection. |
||
62 | * |
||
63 | * @param $paginator |
||
64 | * @param null $transformer |
||
65 | * @param null $resourceKey |
||
66 | * @param bool $build |
||
67 | * |
||
68 | * @return array|\Illuminate\Http\Response|ResponseBuilder |
||
69 | */ |
||
70 | 2 | View Code Duplication | public function paginatedCollection($paginator, $transformer = null, $resourceKey = null, $build = true) |
1 ignored issue
–
show
|
|||
71 | { |
||
72 | 2 | return $this->toResponseBuilder( |
|
73 | 2 | $this->fractalAdapter->paginatedCollection($paginator, $transformer, $resourceKey), |
|
74 | $build |
||
75 | 2 | ); |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Respond with a no content response. |
||
80 | * |
||
81 | * @param bool $build |
||
82 | * |
||
83 | * @return ResponseBuilder|\Illuminate\Http\Response |
||
84 | */ |
||
85 | 1 | public function noContent($build = true) |
|
92 | |||
93 | /** |
||
94 | * Return a 404 not found error. |
||
95 | * |
||
96 | * @param string|array $message |
||
97 | * @param bool $build |
||
98 | * |
||
99 | * @return \Illuminate\Http\Response |
||
100 | */ |
||
101 | 1 | public function errorNotFound($message = 'Not Found', $build = true) |
|
105 | |||
106 | /** |
||
107 | * Return a 400 bad request error. |
||
108 | * |
||
109 | * @param string|array $message |
||
110 | * @param bool $build |
||
111 | * |
||
112 | * @return \Illuminate\Http\Response |
||
113 | */ |
||
114 | 1 | public function errorBadRequest($message = 'Bad Request', $build = true) |
|
118 | |||
119 | /** |
||
120 | * Return a 401 unauthorized error. |
||
121 | * |
||
122 | * @param string|array $message |
||
123 | * @param bool $build |
||
124 | * |
||
125 | * @return \Illuminate\Http\Response |
||
126 | */ |
||
127 | public function errorUnauthorized($message = 'Unauthorized', $build = true) |
||
131 | |||
132 | /** |
||
133 | * Return a 401 unauthorized error. |
||
134 | * |
||
135 | * @param string|array $message |
||
136 | * @param bool $build |
||
137 | * |
||
138 | * @return \Illuminate\Http\Response |
||
139 | */ |
||
140 | 1 | public function errorForbidden($message = 'Forbidden', $build = true) |
|
144 | |||
145 | /** |
||
146 | * Return a 500 internal server error. |
||
147 | * |
||
148 | * @param string|array $message |
||
149 | * @param bool $build |
||
150 | * |
||
151 | * @return \Illuminate\Http\Response |
||
152 | */ |
||
153 | 1 | public function errorInternal($message = 'Internal Error', $build = true) |
|
157 | |||
158 | /** |
||
159 | * Return an error response. |
||
160 | * |
||
161 | * @param $messages |
||
162 | * @param int $statusCode |
||
163 | * @param mixed|MessageBag $errors |
||
164 | * @param bool $build |
||
165 | * |
||
166 | * @return \Illuminate\Http\Response |
||
167 | */ |
||
168 | 5 | public function error($messages, $statusCode = 500, $errors = null, $build = true) |
|
184 | |||
185 | /** |
||
186 | * Wrap into ResponseBuilder, and build if requested. |
||
187 | * If not will return ResponseBuilder object, to be able to add headers for instance |
||
188 | * before sending back response using `build()` method. |
||
189 | * |
||
190 | * @param $data |
||
191 | * @param $build |
||
192 | * |
||
193 | * @return \Illuminate\Http\Response|ResponseBuilder |
||
194 | */ |
||
195 | 6 | protected function toResponseBuilder($data, $build) |
|
201 | } |
||
202 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.