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 |
||
| 26 | class StreamFile { |
||
| 27 | // Do not send any HTTP headers unless requested by caller (e.g. body only) |
||
| 28 | const STREAM_HEADLESS = HTTPFileStreamer::STREAM_HEADLESS; |
||
| 29 | // Do not try to tear down any PHP output buffers |
||
| 30 | const STREAM_ALLOW_OB = HTTPFileStreamer::STREAM_ALLOW_OB; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Stream a file to the browser, adding all the headings and fun stuff. |
||
| 34 | * Headers sent include: Content-type, Content-Length, Last-Modified, |
||
| 35 | * and Content-Disposition. |
||
| 36 | * |
||
| 37 | * @param string $fname Full name and path of the file to stream |
||
| 38 | * @param array $headers Any additional headers to send if the file exists |
||
| 39 | * @param bool $sendErrors Send error messages if errors occur (like 404) |
||
| 40 | * @param array $optHeaders HTTP request header map (e.g. "range") (use lowercase keys) |
||
| 41 | * @param integer $flags Bitfield of STREAM_* constants |
||
| 42 | * @throws MWException |
||
| 43 | * @return bool Success |
||
| 44 | */ |
||
| 45 | public static function stream( |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Send out a standard 404 message for a file |
||
| 65 | * |
||
| 66 | * @param string $fname Full name and path of the file to stream |
||
| 67 | * @param integer $flags Bitfield of STREAM_* constants |
||
| 68 | * @since 1.24 |
||
| 69 | */ |
||
| 70 | public static function send404Message( $fname, $flags = 0 ) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Convert a Range header value to an absolute (start, end) range tuple |
||
| 76 | * |
||
| 77 | * @param string $range Range header value |
||
| 78 | * @param integer $size File size |
||
| 79 | * @return array|string Returns error string on failure (start, end, length) |
||
| 80 | * @since 1.24 |
||
| 81 | */ |
||
| 82 | public static function parseRange( $range, $size ) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Determine the file type of a file based on the path |
||
| 88 | * |
||
| 89 | * @param string $filename Storage path or file system path |
||
| 90 | * @param bool $safe Whether to do retroactive upload blacklist checks |
||
| 91 | * @return null|string |
||
| 92 | */ |
||
| 93 | public static function contentTypeFromPath( $filename, $safe = true ) { |
||
| 144 | } |
||
| 145 |