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:
Complex classes like TGifting often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TGifting, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait TGifting |
||
| 21 | { |
||
| 22 | /** @var string */ |
||
| 23 | protected $giftItemId; |
||
| 24 | /** @var IPriceGroup */ |
||
| 25 | protected $giftPricing; |
||
| 26 | /** @var bool */ |
||
| 27 | protected $includeGiftWrapping; |
||
| 28 | /** @var string */ |
||
| 29 | protected $giftMessageTo; |
||
| 30 | /** @var string */ |
||
| 31 | protected $giftMessageFrom; |
||
| 32 | /** @var string */ |
||
| 33 | protected $giftMessageContent; |
||
| 34 | /** @var string */ |
||
| 35 | protected $giftCardTo; |
||
| 36 | /** @var string */ |
||
| 37 | protected $giftCardFrom; |
||
| 38 | /** @var string */ |
||
| 39 | protected $giftCardMessage; |
||
| 40 | /** @var string */ |
||
| 41 | protected $packslipTo; |
||
| 42 | /** @var string */ |
||
| 43 | protected $packslipFrom; |
||
| 44 | /** @var string */ |
||
| 45 | protected $packslipMessage; |
||
| 46 | /** @var string */ |
||
| 47 | protected $localizedFromLabel; |
||
| 48 | /** @var string */ |
||
| 49 | protected $localizedToLabel; |
||
| 50 | |||
| 51 | public function getEmptyGiftingPriceGroup() |
||
| 55 | |||
| 56 | public function getGiftItemId() |
||
| 60 | |||
| 61 | public function setGiftItemId($giftItemId) |
||
| 62 | { |
||
| 63 | $this->giftItemId = $this->cleanString($giftItemId, 20); |
||
| 64 | return $this; |
||
| 65 | } |
||
| 66 | |||
| 67 | public function getGiftPricing() |
||
| 71 | |||
| 72 | public function setGiftPricing(IPriceGroup $giftPricing) |
||
| 73 | { |
||
| 74 | $this->giftPricing = $giftPricing; |
||
| 75 | return $this; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function getIncludeGiftWrapping() |
||
| 82 | |||
| 83 | public function setIncludeGiftWrapping($includeGiftWrapping) |
||
| 84 | { |
||
| 85 | $this->includeGiftWrapping = (bool) $includeGiftWrapping; |
||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function getGiftMessageTo() |
||
| 93 | |||
| 94 | public function setGiftMessageTo($giftMessageTo) |
||
| 95 | { |
||
| 96 | $this->giftMessageTo = $giftMessageTo; |
||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function getGiftMessageFrom() |
||
| 104 | |||
| 105 | public function setGiftMessageFrom($giftMessageFrom) |
||
| 106 | { |
||
| 107 | $this->giftMessageFrom = $giftMessageFrom; |
||
| 108 | return $this; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function getGiftMessageContent() |
||
| 115 | |||
| 116 | public function setGiftMessageContent($giftMessageContent) |
||
| 117 | { |
||
| 118 | $this->giftMessageContent = $this->normalizeWhitespace($giftMessageContent); |
||
| 119 | return $this; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function getGiftCardTo() |
||
| 126 | |||
| 127 | public function setGiftCardTo($giftCardTo) |
||
| 128 | { |
||
| 129 | $this->giftCardTo = $giftCardTo; |
||
| 130 | return $this; |
||
| 131 | } |
||
| 132 | |||
| 133 | public function getGiftCardFrom() |
||
| 137 | |||
| 138 | public function setGiftCardFrom($giftCardFrom) |
||
| 139 | { |
||
| 140 | $this->giftCardFrom = $giftCardFrom; |
||
| 141 | return $this; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getGiftCardMessage() |
||
| 148 | |||
| 149 | public function setGiftCardMessage($giftCardMessage) |
||
| 150 | { |
||
| 151 | $this->giftCardMessage = $this->normalizeWhitespace($giftCardMessage); |
||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function getPackslipTo() |
||
| 159 | |||
| 160 | public function setPackslipTo($packslipTo) |
||
| 161 | { |
||
| 162 | $this->packslipTo = $packslipTo; |
||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getPackslipFrom() |
||
| 170 | |||
| 171 | public function setPackslipFrom($packslipFrom) |
||
| 172 | { |
||
| 173 | $this->packslipFrom = $packslipFrom; |
||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function getPackslipMessage() |
||
| 181 | |||
| 182 | public function setPackslipMessage($packslipMessage) |
||
| 183 | { |
||
| 184 | $this->packslipMessage = $this->normalizeWhitespace($packslipMessage); |
||
| 185 | return $this; |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getLocalizedFromLabel() |
||
| 192 | |||
| 193 | public function setLocalizedFromLabel($localizedFromLabel) |
||
| 194 | { |
||
| 195 | $this->localizedFromLabel = $localizedFromLabel; |
||
| 196 | return $this; |
||
| 197 | } |
||
| 198 | |||
| 199 | public function getLocalizedToLabel() |
||
| 203 | |||
| 204 | public function setLocalizedToLabel($localizedToLabel) |
||
| 205 | { |
||
| 206 | $this->localizedToLabel = $localizedToLabel; |
||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Serialize gifting data. |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | protected function serializeGifting() |
||
| 216 | { |
||
| 217 | $serializedData = $this->serializeGift() |
||
| 218 | . $this->serializeGiftingGiftCard() |
||
| 219 | . $this->serializeGiftingPackslip(); |
||
| 220 | return $serializedData ? "<Gifting>$serializedData</Gifting>" : ''; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Serialize the "Gift" gifting data. Will only be included if there is |
||
| 225 | * a gift item id set. |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | protected function serializeGift() |
||
| 230 | { |
||
| 231 | if ($this->getGiftItemId()) { |
||
| 232 | $pricing = $this->getGiftPricing(); |
||
| 233 | return '<Gift>' |
||
| 234 | . "<ItemId>{$this->xmlEncode($this->getGiftItemId())}</ItemId>" |
||
| 235 | . ($pricing ? $pricing->setRootNodeName('Pricing')->serialize() : '') |
||
| 236 | . $this->serializeGiftingGiftMessage() |
||
| 237 | . '</Gift>'; |
||
| 238 | } |
||
| 239 | return ''; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Serialize the gift message data. Will only be included if there |
||
| 244 | * are valid to and from fields. |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | protected function serializeGiftingGiftMessage() |
||
| 249 | { |
||
| 250 | $messageSerialization = $this->serializeGiftingMessage( |
||
| 251 | $this->getGiftMessageTo(), |
||
| 252 | $this->getGiftMessageFrom(), |
||
| 253 | $this->getGiftMessageContent() |
||
| 254 | ); |
||
| 255 | return $messageSerialization ? $messageSerialization : ''; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Serialize the gift card message data. Will only be included if there |
||
| 260 | * are valid to and from fields. |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | protected function serializeGiftingGiftCard() |
||
| 265 | { |
||
| 266 | $messageSerialization = $this->serializeGiftingMessage( |
||
| 267 | $this->getGiftCardTo(), |
||
| 268 | $this->getGiftCardFrom(), |
||
| 269 | $this->getGiftCardMessage() |
||
| 270 | ); |
||
| 271 | // If the gifting includes gift wrapping, the gift card node |
||
| 272 | // must always be included. |
||
| 273 | return ($messageSerialization || $this->getIncludeGiftWrapping()) |
||
| 274 | ? "<GiftCard>$messageSerialization</GiftCard>" |
||
| 275 | : ''; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Serialize the packslip message data. Will only be included if there |
||
| 280 | * are valid to and from fields. |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | protected function serializeGiftingPackslip() |
||
| 285 | { |
||
| 286 | $messageSerialization = $this->serializeGiftingMessage( |
||
| 287 | $this->getPackslipTo(), |
||
| 288 | $this->getPackslipFrom(), |
||
| 289 | $this->getPackslipMessage() |
||
| 290 | ); |
||
| 291 | return $messageSerialization ? "<Packslip>$messageSerialization</Packslip>" : ''; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Serialize a gifting message from, to and message. Will only return a |
||
| 296 | * serialization if at least a from and to name are given. |
||
| 297 | * |
||
| 298 | * @param string |
||
| 299 | * @param string |
||
| 300 | * @param string |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | protected function serializeGiftingMessage($toName, $fromName, $messageText) |
||
| 304 | { |
||
| 305 | $localizedTo = $this->serializeOptionalAttribute( |
||
| 306 | 'localizedDisplayText', |
||
| 307 | $this->xmlEncode($this->getLocalizedToLabel()) |
||
| 308 | ); |
||
| 309 | $localizedFrom = $this->serializeOptionalAttribute( |
||
| 310 | 'localizedDisplayText', |
||
| 311 | $this->xmlEncode($this->getLocalizedFromLabel()) |
||
| 312 | ); |
||
| 313 | return $toName && $fromName |
||
| 314 | ? '<Message>' |
||
| 315 | . "<To $localizedTo>{$this->xmlEncode($toName)}</To>" |
||
| 316 | . "<From $localizedFrom>{$this->xmlEncode($fromName)}</From>" |
||
| 317 | . $this->serializeOptionalXmlEncodedValue('Message', $messageText) |
||
| 318 | . '</Message>' |
||
| 319 | : ''; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Extract gifting pricing from the payload in the DOMXPath and, if it is |
||
| 324 | * included in the serialized data, deserialize it into a new price group. |
||
| 325 | * |
||
| 326 | * @return self |
||
| 327 | */ |
||
| 328 | View Code Duplication | protected function deserializeGiftPricing(DOMXPath $xpath) |
|
| 329 | { |
||
| 330 | $priceNode = $xpath->query('x:Gifting/x:Gift/x:Pricing')->item(0); |
||
| 331 | if ($priceNode) { |
||
| 332 | $this->setGiftPricing( |
||
| 333 | $this->getEmptyGiftingPriceGroup()->deserialize($priceNode->C14N()) |
||
| 334 | ); |
||
| 335 | } |
||
| 336 | return $this; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Serialize an optional element containing a string. The value will be |
||
| 341 | * xml-encoded if is not null. |
||
| 342 | * |
||
| 343 | * @param string |
||
| 344 | * @param string |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | abstract protected function serializeOptionalXmlEncodedValue($name, $value); |
||
| 348 | |||
| 349 | /** |
||
| 350 | * encode the passed in string to be safe for xml if it is not null, |
||
| 351 | * otherwise simply return the null parameter. |
||
| 352 | * |
||
| 353 | * @param string|null |
||
| 354 | * @return string|null |
||
| 355 | */ |
||
| 356 | abstract protected function xmlEncode($value = null); |
||
| 357 | } |
||
| 358 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.