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 |
||
| 14 | class HtmlCompressor |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param string $data is either a handle to an open file, or an HTML string |
||
| 19 | * @param null|array $options key => value array of execute options |
||
| 20 | * The possible keys are: |
||
| 21 | * |
||
| 22 | * - `c` or `no-comments` - removes HTML comments |
||
| 23 | * - `s` or `stats` - output filesize savings calculation |
||
| 24 | * - `x` or `extra` - perform extra (possibly unsafe) compression operations |
||
| 25 | * |
||
| 26 | * Example: HtmlCompressor::compress($HtmlCode, $options = ['no-comments' => true]) |
||
| 27 | * |
||
| 28 | * @return string |
||
| 29 | */ |
||
| 30 | public static function compress($data, $options = null) |
||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * HTML Compressor 1.0.1 |
||
| 39 | * Original Author: Tyler Hall <[email protected]> |
||
| 40 | * Edited by: Revin Roman <[email protected]> |
||
| 41 | * Latest Source and Bug Tracker: http://github.com/tylerhall/html-compressor |
||
| 42 | * |
||
| 43 | * Attemps to reduce the filesize of an HTML document by removing unnecessary |
||
| 44 | * whitespace at the beginning and end of lines, inside closing tags, and |
||
| 45 | * stripping blank lines completely. <pre> tags are respected and their contents |
||
| 46 | * are left alone. Warning, nested <pre> tags may exhibit unexpected behaviour. |
||
| 47 | * |
||
| 48 | * This code is licensed under the MIT Open Source License. |
||
| 49 | * Copyright (c) 2010 [email protected] |
||
| 50 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
||
| 51 | * of this software and associated documentation files (the "Software"), to deal |
||
| 52 | * in the Software without restriction, including without limitation the rights |
||
| 53 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
| 54 | * copies of the Software, and to permit persons to whom the Software is |
||
| 55 | * furnished to do so, subject to the following conditions: |
||
| 56 | * |
||
| 57 | * The above copyright notice and this permission notice shall be included in |
||
| 58 | * all copies or substantial portions of the Software. |
||
| 59 | * |
||
| 60 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
| 61 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
| 62 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
| 63 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
| 64 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
| 65 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||
| 66 | * THE SOFTWARE. |
||
| 67 | * |
||
| 68 | * @param $data |
||
| 69 | * @param null|array $options |
||
| 70 | * @return bool|mixed|string |
||
| 71 | */ |
||
| 72 | private function htmlCompress($data, $options = null) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param $line |
||
| 154 | * @return array |
||
| 155 | * @codeCoverageIgnore |
||
| 156 | */ |
||
| 157 | View Code Duplication | private function checkInsidePre($line) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param $line |
||
| 171 | * @return array |
||
| 172 | * @codeCoverageIgnore |
||
| 173 | */ |
||
| 174 | View Code Duplication | private function checkInsideTextarea($line) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Returns the next line from an open file handle or a string |
||
| 188 | * @param $data |
||
| 189 | * @return bool|string |
||
| 190 | * @codeCoverageIgnore |
||
| 191 | */ |
||
| 192 | private function getLine(&$data) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param $data |
||
| 215 | * @param null|array $options |
||
| 216 | * @return bool|mixed|string |
||
| 217 | * @deprecated |
||
| 218 | * @codeCoverageIgnore |
||
| 219 | */ |
||
| 220 | private function html_compress($data, $options = null) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns the next line from an open file handle or a string |
||
| 229 | * @param $data |
||
| 230 | * @return bool|string |
||
| 231 | * @deprecated |
||
| 232 | * @codeCoverageIgnore |
||
| 233 | */ |
||
| 234 | private function get_line(&$data) |
||
| 240 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.