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 |
||
19 | class Response |
||
20 | { |
||
21 | const HTTP_VERSION = 1.1; |
||
22 | const HTTP_OK = 200; |
||
23 | const HTTP_CREATED = 201; |
||
24 | const HTTP_MOVED_PERMANENTLY = 301; |
||
25 | const HTTP_FOUND = 302; |
||
26 | const HTTP_BAD_REQUEST = 400; |
||
27 | const HTTP_UNAUTHORIZED = 401; |
||
28 | const HTTP_NOT_FOUND = 404; |
||
29 | const HTTP_METHOD_NOT_ALLOWED = 405; |
||
30 | const HTTP_INTERNAL_SERVER_ERROR = 500; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | public static $statusTexts = [ |
||
36 | 200 => 'OK', |
||
37 | 201 => 'Created', |
||
38 | 301 => 'Moved Permanently', |
||
39 | 302 => 'Found', |
||
40 | 401 => 'Unauthorized', |
||
41 | 402 => 'Payment Required', |
||
42 | 403 => 'Forbidden', |
||
43 | 404 => 'Not Found', |
||
44 | 405 => 'Method Not Allowed', |
||
45 | 500 => 'Internal Server Error', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * @var array; |
||
50 | */ |
||
51 | protected $headers; |
||
52 | |||
53 | /** |
||
54 | * @var int |
||
55 | */ |
||
56 | protected $statusCode; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $content; |
||
62 | |||
63 | /** |
||
64 | * @param string $content |
||
65 | * @param int $status |
||
66 | * @param array $headers |
||
67 | */ |
||
68 | 17 | public function __construct($content = '', $status = self::HTTP_OK, array $headers = []) |
|
77 | |||
78 | /** |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getHeaders() |
||
85 | |||
86 | /** |
||
87 | * @param string $key |
||
88 | * @param string $value |
||
89 | */ |
||
90 | 17 | public function addHeader($key, $value) |
|
94 | |||
95 | /** |
||
96 | * @param string $key |
||
97 | * |
||
98 | * @return mixed |
||
99 | */ |
||
100 | 16 | View Code Duplication | public function getHeader($key) |
107 | |||
108 | /** |
||
109 | * @return int |
||
110 | */ |
||
111 | 17 | public function getStatusCode() |
|
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | 17 | public function getContent() |
|
123 | |||
124 | public function send() |
||
134 | |||
135 | /** |
||
136 | * @param string $key |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 17 | protected function normalizeHeaderName($key) |
|
144 | } |
||
145 |
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.