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 |
||
24 | class Headers implements \ArrayAccess, \IteratorAggregate |
||
25 | { |
||
26 | static private $mapping = [ |
||
27 | |||
28 | 'Cache-Control' => Headers\CacheControl::class, |
||
29 | 'Content-Disposition' => Headers\ContentDisposition::class, |
||
30 | 'Content-Type' => Headers\ContentType::class, |
||
31 | 'Date' => Headers\Date::class, |
||
32 | 'Expires' => Headers\Date::class, |
||
33 | 'If-Modified-Since' => Headers\Date::class, |
||
34 | 'If-Unmodified-Since' => Headers\Date::class, |
||
35 | 'Last-Modified' => Headers\Date::class |
||
36 | |||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * Normalizes field name. |
||
41 | * |
||
42 | * @param string $name |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | static private function normalize_field_name($name) |
||
50 | |||
51 | /** |
||
52 | * Header fields. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $fields = []; |
||
57 | |||
58 | /** |
||
59 | * If the `REQUEST_URI` key is found in the header fields they are considered coming from the |
||
60 | * super global `$_SERVER` array in which case they are filtered to keep only keys |
||
61 | * starting with the `HTTP_` prefix. Also, header field names are normalized. For instance, |
||
62 | * `HTTP_CONTENT_TYPE` becomes `Content-Type`. |
||
63 | * |
||
64 | * @param array $fields The initial headers. |
||
65 | */ |
||
66 | public function __construct(array $fields = []) |
||
95 | |||
96 | /** |
||
97 | * Clone instantiated fields. |
||
98 | */ |
||
99 | public function __clone() |
||
111 | |||
112 | /** |
||
113 | * Returns the header as a string. |
||
114 | * |
||
115 | * Header fields with empty string values are discarded. |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function __toString() |
||
137 | |||
138 | /** |
||
139 | * Sends header fields using the {@link header()} function. |
||
140 | * |
||
141 | * Header fields with empty string values are discarded. |
||
142 | */ |
||
143 | public function __invoke() |
||
157 | |||
158 | /** |
||
159 | * Send header field. |
||
160 | * |
||
161 | * Note: The only reason for this method is testing. |
||
162 | * |
||
163 | * @param string $field |
||
164 | * @param string $value |
||
165 | */ |
||
166 | // @codeCoverageIgnoreStart |
||
167 | protected function send_header($field, $value) |
||
171 | // @codeCoverageIgnoreEnd |
||
172 | |||
173 | /** |
||
174 | * Checks if a header field exists. |
||
175 | * |
||
176 | * @param mixed $field |
||
177 | * |
||
178 | * @return boolean |
||
179 | */ |
||
180 | public function offsetExists($field) |
||
184 | |||
185 | /** |
||
186 | * Returns a header. |
||
187 | * |
||
188 | * @param mixed $field |
||
189 | * |
||
190 | * @return string|null The header field value or null if it is not defined. |
||
191 | */ |
||
192 | public function offsetGet($field) |
||
208 | |||
209 | /** |
||
210 | * Sets a header field. |
||
211 | * |
||
212 | * Note: Setting a header field to `null` removes it, just like unset() would. |
||
213 | * |
||
214 | * ## Date, Expires, Last-Modified |
||
215 | * |
||
216 | * The `Date`, `Expires` and `Last-Modified` header fields can be provided as a Unix |
||
217 | * timestamp, a string or a {@link \DateTime} object. |
||
218 | * |
||
219 | * ## Cache-Control, Content-Disposition and Content-Type |
||
220 | * |
||
221 | * Instances of the {@link Headers\CacheControl}, {@link Headers\ContentDisposition} and |
||
222 | * {@link Headers\ContentType} are used to handle the values of the `Cache-Control`, |
||
223 | * `Content-Disposition` and `Content-Type` header fields. |
||
224 | * |
||
225 | * @param string $field The header field to set. |
||
226 | * @param mixed $value The value of the header field. |
||
227 | */ |
||
228 | public function offsetSet($field, $value) |
||
276 | |||
277 | /** |
||
278 | * Removes a header field. |
||
279 | * |
||
280 | * @param mixed $field |
||
281 | */ |
||
282 | public function offsetUnset($field) |
||
286 | |||
287 | /** |
||
288 | * Returns an iterator for the header fields. |
||
289 | */ |
||
290 | public function getIterator() |
||
294 | } |
||
295 |