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 |
||
| 16 | abstract class Generator |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Generator creation stack. |
||
| 20 | * |
||
| 21 | * Use to check if it is OK to start / close the requested element in the |
||
| 22 | * current state. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $stack = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * If set to true, output will be formatted and indented. |
||
| 30 | * |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | protected $formatOutput = false; |
||
| 34 | |||
| 35 | public function setFormatOutput($formatOutput) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Reset output visitor to a virgin state. |
||
| 42 | */ |
||
| 43 | public function reset() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Start document. |
||
| 51 | * |
||
| 52 | * @param mixed $data |
||
| 53 | */ |
||
| 54 | abstract public function startDocument($data); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns if the document is empty or already contains data. |
||
| 58 | * |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | abstract public function isEmpty(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Check start document. |
||
| 65 | * |
||
| 66 | * @param mixed $data |
||
| 67 | */ |
||
| 68 | protected function checkStartDocument($data) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * End document. |
||
| 81 | * |
||
| 82 | * Returns the generated document as a string. |
||
| 83 | * |
||
| 84 | * @param mixed $data |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | abstract public function endDocument($data); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Check end document. |
||
| 92 | * |
||
| 93 | * @param mixed $data |
||
| 94 | */ |
||
| 95 | protected function checkEndDocument($data) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Start object element. |
||
| 102 | * |
||
| 103 | * @param string $name |
||
| 104 | * @param string $mediaTypeName |
||
| 105 | */ |
||
| 106 | abstract public function startObjectElement($name, $mediaTypeName = null); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Check start object element. |
||
| 110 | * |
||
| 111 | * @param mixed $data |
||
| 112 | */ |
||
| 113 | View Code Duplication | protected function checkStartObjectElement($data) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * End object element. |
||
| 131 | * |
||
| 132 | * @param string $name |
||
| 133 | */ |
||
| 134 | abstract public function endObjectElement($name); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check end object element. |
||
| 138 | * |
||
| 139 | * @param mixed $data |
||
| 140 | */ |
||
| 141 | protected function checkEndObjectElement($data) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Start hash element. |
||
| 148 | * |
||
| 149 | * @param string $name |
||
| 150 | */ |
||
| 151 | abstract public function startHashElement($name); |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Check start hash element. |
||
| 155 | * |
||
| 156 | * @param mixed $data |
||
| 157 | */ |
||
| 158 | View Code Duplication | protected function checkStartHashElement($data) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * End hash element. |
||
| 176 | * |
||
| 177 | * @param string $name |
||
| 178 | */ |
||
| 179 | abstract public function endHashElement($name); |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Check end hash element. |
||
| 183 | * |
||
| 184 | * @param mixed $data |
||
| 185 | */ |
||
| 186 | protected function checkEndHashElement($data) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Start value element. |
||
| 193 | * |
||
| 194 | * @param string $name |
||
| 195 | * @param string $value |
||
| 196 | */ |
||
| 197 | abstract public function startValueElement($name, $value); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Check start value element. |
||
| 201 | * |
||
| 202 | * @param mixed $data |
||
| 203 | */ |
||
| 204 | protected function checkStartValueElement($data) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * End value element. |
||
| 211 | * |
||
| 212 | * @param string $name |
||
| 213 | */ |
||
| 214 | abstract public function endValueElement($name); |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Check end value element. |
||
| 218 | * |
||
| 219 | * @param mixed $data |
||
| 220 | */ |
||
| 221 | protected function checkEndValueElement($data) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Start list. |
||
| 228 | * |
||
| 229 | * @param string $name |
||
| 230 | */ |
||
| 231 | abstract public function startList($name); |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Check start list. |
||
| 235 | * |
||
| 236 | * @param mixed $data |
||
| 237 | */ |
||
| 238 | protected function checkStartList($data) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * End list. |
||
| 245 | * |
||
| 246 | * @param string $name |
||
| 247 | */ |
||
| 248 | abstract public function endList($name); |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Check end list. |
||
| 252 | * |
||
| 253 | * @param mixed $data |
||
| 254 | */ |
||
| 255 | protected function checkEndList($data) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Start attribute. |
||
| 262 | * |
||
| 263 | * @param string $name |
||
| 264 | * @param string $value |
||
| 265 | */ |
||
| 266 | abstract public function startAttribute($name, $value); |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Check start attribute. |
||
| 270 | * |
||
| 271 | * @param mixed $data |
||
| 272 | */ |
||
| 273 | protected function checkStartAttribute($data) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * End attribute. |
||
| 280 | * |
||
| 281 | * @param string $name |
||
| 282 | */ |
||
| 283 | abstract public function endAttribute($name); |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Check end attribute. |
||
| 287 | * |
||
| 288 | * @param mixed $data |
||
| 289 | */ |
||
| 290 | protected function checkEndAttribute($data) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get media type. |
||
| 297 | * |
||
| 298 | * @param string $name |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | abstract public function getMediaType($name); |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Generates a media type from $name and $type. |
||
| 306 | * |
||
| 307 | * @param string $name |
||
| 308 | * @param string $type |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | protected function generateMediaType($name, $type) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Generates a generic representation of the scalar, hash or list given in |
||
| 319 | * $hashValue into the document, using an element of $hashElementName as |
||
| 320 | * its parent. |
||
| 321 | * |
||
| 322 | * @param string $hashElementName |
||
| 323 | * @param mixed $hashValue |
||
| 324 | */ |
||
| 325 | abstract public function generateFieldTypeHash($hashElementName, $hashValue); |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Check close / end operation. |
||
| 329 | * |
||
| 330 | * @param string $type |
||
| 331 | * @param mixed $data |
||
| 332 | * @param array $validParents |
||
| 333 | */ |
||
| 334 | protected function checkStart($type, $data, array $validParents) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Check close / end operation. |
||
| 362 | * |
||
| 363 | * @param string $type |
||
| 364 | * @param mixed $data |
||
| 365 | */ |
||
| 366 | protected function checkEnd($type, $data) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Serializes a boolean value. |
||
| 393 | * |
||
| 394 | * @param bool $boolValue |
||
| 395 | * |
||
| 396 | * @return mixed |
||
| 397 | */ |
||
| 398 | abstract public function serializeBool($boolValue); |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns a string representation of the current stack of the document. |
||
| 402 | * |
||
| 403 | * Example: "Location.ContentInfo'. |
||
| 404 | * Elements of type 'attribute', 'document' and 'list' are ignored. |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function getStackPath() |
||
| 426 | } |
||
| 427 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: