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 |
||
10 | abstract class Gateway |
||
11 | { |
||
12 | use QueryHelperTrait; |
||
13 | |||
14 | protected $statusCode = 200; |
||
15 | protected $fractal; |
||
16 | |||
17 | public function __construct() |
||
21 | |||
22 | /** |
||
23 | * Returns the current status code. |
||
24 | * |
||
25 | * @return int |
||
26 | */ |
||
27 | protected function getStatusCode() |
||
31 | |||
32 | /** |
||
33 | * Sets the current status code. |
||
34 | * |
||
35 | * @param $statusCode |
||
36 | * @return $this |
||
37 | */ |
||
38 | protected function setStatusCode($statusCode) |
||
44 | |||
45 | /** |
||
46 | * Returns a json response that contains the specified resource |
||
47 | * passed through fractal and optionally a transformer. |
||
48 | * |
||
49 | * @param $item |
||
50 | * @param null $callback |
||
51 | * @param null $resourceKey |
||
52 | * @return \Illuminate\Http\JsonResponse |
||
53 | */ |
||
54 | View Code Duplication | protected function respondWithItem($item, $callback = null, $resourceKey = null) |
|
61 | |||
62 | /** |
||
63 | * Returns a json response that contains the specified collection |
||
64 | * passed through fractal and optionally a transformer. |
||
65 | * |
||
66 | * @param $collection |
||
67 | * @param $callback |
||
68 | * @param null $resourceKey |
||
69 | * @return \Illuminate\Http\JsonResponse |
||
70 | */ |
||
71 | protected function respondWithCollection($collection, $callback, $resourceKey = null) |
||
77 | |||
78 | /** |
||
79 | * Parses the incoming json parameters into a ParameterBag object. |
||
80 | * |
||
81 | * @param $parameters |
||
82 | * @return ParameterBag |
||
83 | */ |
||
84 | protected function parseParameters($parameters) |
||
96 | |||
97 | /** |
||
98 | * Returns a json response that contains the specified array, |
||
99 | * and the current status code. |
||
100 | * |
||
101 | * @param array $array |
||
102 | * @return \Illuminate\Http\JsonResponse |
||
103 | */ |
||
104 | protected function respondWithArray(array $array) |
||
108 | |||
109 | /** |
||
110 | * Returns a response that indicates a 404 Not Found. |
||
111 | * |
||
112 | * @param string $message |
||
113 | * @return \Illuminate\Http\JsonResponse |
||
114 | */ |
||
115 | protected function errorNotFound($message = 'Resource Not Found') |
||
119 | |||
120 | /** |
||
121 | * Returns a response that indicates an an error occurred. |
||
122 | * |
||
123 | * @param $message |
||
124 | * @return \Illuminate\Http\JsonResponse |
||
125 | */ |
||
126 | protected function respondWithError($message) |
||
135 | } |
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.