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 |
||
| 17 | class ExpansionGenerator extends Generator |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var Generator |
||
| 21 | */ |
||
| 22 | private $innerGenerator; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * ExpansionGenerator constructor. |
||
| 26 | * @param Generator $generator |
||
| 27 | */ |
||
| 28 | public function __construct(Generator $generator) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Not supported in this generator, as the context is always within a generated document. |
||
| 35 | * |
||
| 36 | * @param mixed $data |
||
| 37 | * |
||
| 38 | * @throws BadStateException |
||
| 39 | */ |
||
| 40 | public function startDocument($data) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Returns if the document is empty or already contains data. |
||
| 50 | * |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | public function isEmpty() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Not supported in this generator, as the context is always within a generated document. |
||
| 60 | * |
||
| 61 | * @param mixed $data |
||
| 62 | * |
||
| 63 | * @throws BadStateException |
||
| 64 | */ |
||
| 65 | public function endDocument($data) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Start object element. |
||
| 72 | * If the element is the first added to this generator, it is silently skipped. |
||
| 73 | * Subsequent elements are started as expected. |
||
| 74 | * |
||
| 75 | * @param string $name |
||
| 76 | * @param string $mediaTypeName |
||
| 77 | */ |
||
| 78 | public function startObjectElement($name, $mediaTypeName = null) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * End object element. |
||
| 89 | * |
||
| 90 | * @param string $name |
||
| 91 | */ |
||
| 92 | public function endObjectElement($name) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Start hash element. |
||
| 106 | * |
||
| 107 | * @param string $name |
||
| 108 | */ |
||
| 109 | public function startHashElement($name) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * End hash element. |
||
| 116 | * |
||
| 117 | * @param string $name |
||
| 118 | */ |
||
| 119 | public function endHashElement($name) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Start value element. |
||
| 126 | * |
||
| 127 | * @param string $name |
||
| 128 | * @param string $value |
||
| 129 | */ |
||
| 130 | public function startValueElement($name, $value) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * End value element. |
||
| 137 | * |
||
| 138 | * @param string $name |
||
| 139 | */ |
||
| 140 | public function endValueElement($name) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Start list. |
||
| 147 | * |
||
| 148 | * @param string $name |
||
| 149 | */ |
||
| 150 | public function startList($name) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * End list. |
||
| 157 | * |
||
| 158 | * @param string $name |
||
| 159 | */ |
||
| 160 | public function endList($name) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Start attribute. |
||
| 167 | * Skips the href and media-type attribute at stack depth 0. |
||
| 168 | * |
||
| 169 | * @param string $name |
||
| 170 | * @param string $value |
||
| 171 | */ |
||
| 172 | public function startAttribute($name, $value) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * End attribute. |
||
| 181 | * |
||
| 182 | * @param string $name |
||
| 183 | */ |
||
| 184 | public function endAttribute($name) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get media type. |
||
| 193 | * |
||
| 194 | * @param string $name |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getMediaType($name) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Generates a generic representation of the scalar, hash or list given in |
||
| 205 | * $hashValue into the document, using an element of $hashElementName as |
||
| 206 | * its parent. |
||
| 207 | * |
||
| 208 | * @param string $hashElementName |
||
| 209 | * @param mixed $hashValue |
||
| 210 | */ |
||
| 211 | public function generateFieldTypeHash($hashElementName, $hashValue) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Serializes a boolean value. |
||
| 218 | * |
||
| 219 | * @param bool $boolValue |
||
| 220 | * |
||
| 221 | * @return mixed |
||
| 222 | */ |
||
| 223 | public function serializeBool($boolValue) |
||
| 227 | |||
| 228 | public function getStackPath() |
||
| 232 | } |
||
| 233 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.