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 |
||
| 15 | class JsonContent extends TextContent { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @since 1.25 |
||
| 19 | * @var Status |
||
| 20 | */ |
||
| 21 | protected $jsonParse; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param string $text JSON |
||
| 25 | */ |
||
| 26 | public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Decodes the JSON into a PHP associative array. |
||
| 32 | * |
||
| 33 | * @deprecated since 1.25 Use getData instead. |
||
| 34 | * @return array|null |
||
| 35 | */ |
||
| 36 | public function getJsonData() { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Decodes the JSON string. |
||
| 43 | * |
||
| 44 | * Note that this parses it without casting objects to associative arrays. |
||
| 45 | * Objects and arrays are kept as distinguishable types in the PHP values. |
||
| 46 | * |
||
| 47 | * @return Status |
||
| 48 | */ |
||
| 49 | public function getData() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return bool Whether content is valid. |
||
| 58 | */ |
||
| 59 | public function isValid() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Pretty-print JSON. |
||
| 65 | * |
||
| 66 | * If called before validation, it may return JSON "null". |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function beautifyJSON() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Beautifies JSON prior to save. |
||
| 76 | * |
||
| 77 | * @param Title $title Title |
||
| 78 | * @param User $user User |
||
| 79 | * @param ParserOptions $popts |
||
| 80 | * @return JsonContent |
||
| 81 | */ |
||
| 82 | public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Set the HTML and add the appropriate styles. |
||
| 94 | * |
||
| 95 | * @param Title $title |
||
| 96 | * @param int $revId |
||
| 97 | * @param ParserOptions $options |
||
| 98 | * @param bool $generateHtml |
||
| 99 | * @param ParserOutput $output |
||
| 100 | */ |
||
| 101 | protected function fillParserOutput( Title $title, $revId, |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Construct HTML table representation of any JSON value. |
||
| 116 | * |
||
| 117 | * See also valueCell, which is similar. |
||
| 118 | * |
||
| 119 | * @param mixed $val |
||
| 120 | * @return string HTML. |
||
| 121 | */ |
||
| 122 | View Code Duplication | protected function rootValueTable( $val ) { |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Create HTML table representing a JSON object. |
||
| 144 | * |
||
| 145 | * @param stdClass $mapping |
||
| 146 | * @return string HTML |
||
| 147 | */ |
||
| 148 | View Code Duplication | protected function objectTable( $mapping ) { |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Create HTML table row representing one object property. |
||
| 170 | * |
||
| 171 | * @param string $key |
||
| 172 | * @param mixed $val |
||
| 173 | * @return string HTML. |
||
| 174 | */ |
||
| 175 | protected function objectRow( $key, $val ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Create HTML table representing a JSON array. |
||
| 183 | * |
||
| 184 | * @param array $mapping |
||
| 185 | * @return string HTML |
||
| 186 | */ |
||
| 187 | View Code Duplication | protected function arrayTable( $mapping ) { |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Create HTML table row representing the value in an array. |
||
| 209 | * |
||
| 210 | * @param mixed $val |
||
| 211 | * @return string HTML. |
||
| 212 | */ |
||
| 213 | protected function arrayRow( $val ) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Construct HTML table cell representing any JSON value. |
||
| 220 | * |
||
| 221 | * @param mixed $val |
||
| 222 | * @return string HTML. |
||
| 223 | */ |
||
| 224 | View Code Duplication | protected function valueCell( $val ) { |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Construct text representing a JSON primitive value. |
||
| 238 | * |
||
| 239 | * @param mixed $val |
||
| 240 | * @return string Text. |
||
| 241 | */ |
||
| 242 | protected function primitiveValue( $val ) { |
||
| 250 | } |
||
| 251 |