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 OrderSummary 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 OrderSummary, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class OrderSummary implements IOrderSummary |
||
| 28 | { |
||
| 29 | use TPayload; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | protected $id; |
||
| 33 | /** @var string */ |
||
| 34 | protected $orderType; |
||
| 35 | /** @var string */ |
||
| 36 | protected $orderPurpose; |
||
| 37 | /** @var string */ |
||
| 38 | protected $testType; |
||
| 39 | /** @var DateTime */ |
||
| 40 | protected $modifiedTime; |
||
| 41 | /** @var string */ |
||
| 42 | protected $cancellable; |
||
| 43 | /** @var string */ |
||
| 44 | protected $customerOrderId; |
||
| 45 | /** @var string */ |
||
| 46 | protected $customerId; |
||
| 47 | /** @var DateTime */ |
||
| 48 | protected $orderDate; |
||
| 49 | /** @var string */ |
||
| 50 | protected $dashboardRepId; |
||
| 51 | /** @var string */ |
||
| 52 | protected $status; |
||
| 53 | /** @var string */ |
||
| 54 | protected $orderTotal; |
||
| 55 | /** @var string */ |
||
| 56 | protected $source; |
||
| 57 | /** @var string */ |
||
| 58 | protected $chainedOrder; |
||
| 59 | /** @var string */ |
||
| 60 | protected $type; |
||
| 61 | /** @var string */ |
||
| 62 | protected $parentRef; |
||
| 63 | /** @var string */ |
||
| 64 | protected $derivedOrder; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param IValidatorIterator |
||
| 68 | * @param ISchemaValidator |
||
| 69 | * @param IPayloadMap |
||
| 70 | * @param LoggerInterface |
||
| 71 | * @param IPayload |
||
| 72 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 73 | */ |
||
| 74 | public function __construct( |
||
| 75 | IValidatorIterator $validators, |
||
| 76 | ISchemaValidator $schemaValidator, |
||
| 77 | IPayloadMap $payloadMap, |
||
|
|
|||
| 78 | LoggerInterface $logger, |
||
| 79 | IPayload $parentPayload = null |
||
| 80 | ) { |
||
| 81 | $this->logger = $logger; |
||
| 82 | $this->validators = $validators; |
||
| 83 | $this->schemaValidator = $schemaValidator; |
||
| 84 | $this->parentPayload = $parentPayload; |
||
| 85 | |||
| 86 | $this->extractionPaths = [ |
||
| 87 | 'id' => 'string(@id)', |
||
| 88 | 'orderTotal' => 'string(x:OrderTotal)', |
||
| 89 | ]; |
||
| 90 | $this->optionalExtractionPaths = [ |
||
| 91 | 'orderType' => '@orderType', |
||
| 92 | 'orderPurpose' => '@orderPurpose', |
||
| 93 | 'testType' => '@testType', |
||
| 94 | 'cancellable' => '@cancellable', |
||
| 95 | 'customerOrderId' => 'x:CustomerOrderId', |
||
| 96 | 'customerId' => 'x:CustomerId', |
||
| 97 | 'dashboardRepId' => 'x:DashboardRepId', |
||
| 98 | 'status' => 'x:Status', |
||
| 99 | 'source' => 'x:Source', |
||
| 100 | 'chainedOrder' => 'x:ChainedOrder', |
||
| 101 | 'type' => 'x:ChainedOrder/@type', |
||
| 102 | 'parentRef' => 'x:ChainedOrder/@parentRef|x:DerivedOrder/@parentRef', |
||
| 103 | 'derivedOrder' => 'x:DerivedOrder', |
||
| 104 | ]; |
||
| 105 | $this->datetimeExtractionPaths = [ |
||
| 106 | 'modifiedTime' => 'string(@modifiedTime)', |
||
| 107 | 'orderDate' => 'string(x:OrderDate)', |
||
| 108 | ]; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @see IOrderSummary::getId() |
||
| 113 | */ |
||
| 114 | public function getId() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @see IOrderSummary::setId() |
||
| 121 | * @codeCoverageIgnore |
||
| 122 | */ |
||
| 123 | public function setId($id) |
||
| 124 | { |
||
| 125 | $this->id = $id; |
||
| 126 | return $this; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @see IOrderSummary::getOrderType() |
||
| 131 | */ |
||
| 132 | public function getOrderType() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @see IOrderSummary::setOrderType() |
||
| 139 | * @codeCoverageIgnore |
||
| 140 | */ |
||
| 141 | public function setOrderType($orderType) |
||
| 142 | { |
||
| 143 | $this->orderType = $orderType; |
||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @see IOrderSummary::getOrderPurpose() |
||
| 149 | */ |
||
| 150 | public function getOrderPurpose() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @see IOrderSummary::setOrderPurpose() |
||
| 157 | * @codeCoverageIgnore |
||
| 158 | */ |
||
| 159 | public function setOrderPurpose($orderPurpose) |
||
| 160 | { |
||
| 161 | $this->orderPurpose = $orderPurpose; |
||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @see IOrderSummary::getTestType() |
||
| 167 | */ |
||
| 168 | public function getTestType() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @see IOrderSummary::setTestType() |
||
| 175 | * @codeCoverageIgnore |
||
| 176 | */ |
||
| 177 | public function setTestType($testType) |
||
| 178 | { |
||
| 179 | $this->testType = $testType; |
||
| 180 | return $this; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @see IOrderSummary::getModifiedTime() |
||
| 185 | */ |
||
| 186 | public function getModifiedTime() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @see IOrderSummary::setModifiedTime() |
||
| 193 | * @codeCoverageIgnore |
||
| 194 | */ |
||
| 195 | public function setModifiedTime(DateTime $modifiedTime) |
||
| 196 | { |
||
| 197 | $this->modifiedTime = $modifiedTime; |
||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @see IOrderSummary::getCancellable() |
||
| 203 | */ |
||
| 204 | public function getCancellable() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @see IOrderSummary::setCancellable() |
||
| 211 | * @codeCoverageIgnore |
||
| 212 | */ |
||
| 213 | public function setCancellable($cancellable) |
||
| 214 | { |
||
| 215 | $this->cancellable = $cancellable; |
||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @see IOrderSummary::getCustomerId() |
||
| 221 | */ |
||
| 222 | public function getCustomerId() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @see IOrderSummary::setCustomerId() |
||
| 229 | * @codeCoverageIgnore |
||
| 230 | */ |
||
| 231 | public function setCustomerId($customerId) |
||
| 232 | { |
||
| 233 | $this->customerId = $customerId; |
||
| 234 | return $this; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @see IOrderSummary::getCustomerOrderId() |
||
| 239 | */ |
||
| 240 | public function getCustomerOrderId() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @see IOrderSummary::setCustomerOrderId() |
||
| 247 | * @codeCoverageIgnore |
||
| 248 | */ |
||
| 249 | public function setCustomerOrderId($customerOrderId) |
||
| 250 | { |
||
| 251 | $this->customerOrderId = $customerOrderId; |
||
| 252 | return $this; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @see IOrderSummary::getOrderDate() |
||
| 257 | */ |
||
| 258 | public function getOrderDate() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @see IOrderSummary::setOrderDate() |
||
| 265 | * @codeCoverageIgnore |
||
| 266 | */ |
||
| 267 | public function setOrderDate(DateTime $orderDate) |
||
| 268 | { |
||
| 269 | $this->orderDate = $orderDate; |
||
| 270 | return $this; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @see IOrderSummary::getDashboardRepId() |
||
| 275 | */ |
||
| 276 | public function getDashboardRepId() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @see IOrderSummary::setDashboardRepId() |
||
| 283 | * @codeCoverageIgnore |
||
| 284 | */ |
||
| 285 | public function setDashboardRepId($dashboardRepId) |
||
| 286 | { |
||
| 287 | $this->dashboardRepId = $dashboardRepId; |
||
| 288 | return $this; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @see IOrderSummary::getStatus() |
||
| 293 | */ |
||
| 294 | public function getStatus() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @see IOrderSummary::setStatus() |
||
| 301 | * @codeCoverageIgnore |
||
| 302 | */ |
||
| 303 | public function setStatus($status) |
||
| 304 | { |
||
| 305 | $this->status = $status; |
||
| 306 | return $this; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @see IOrderSummary::getOrderTotal() |
||
| 311 | */ |
||
| 312 | public function getOrderTotal() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @see IOrderSummary::setOrderTotal() |
||
| 319 | * @codeCoverageIgnore |
||
| 320 | */ |
||
| 321 | public function setOrderTotal($orderTotal) |
||
| 322 | { |
||
| 323 | $this->orderTotal = $orderTotal; |
||
| 324 | return $this; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @see IOrderSummary::getSource() |
||
| 329 | */ |
||
| 330 | public function getSource() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @see IOrderSummary::setSource() |
||
| 337 | * @codeCoverageIgnore |
||
| 338 | */ |
||
| 339 | public function setSource($source) |
||
| 340 | { |
||
| 341 | $this->source = $source; |
||
| 342 | return $this; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @see IOrderSummary::getChainedOrder() |
||
| 347 | */ |
||
| 348 | public function getChainedOrder() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @see IOrderSummary::setChainedOrder() |
||
| 355 | * @codeCoverageIgnore |
||
| 356 | */ |
||
| 357 | public function setChainedOrder($chainedOrder) |
||
| 358 | { |
||
| 359 | $this->chainedOrder = $chainedOrder; |
||
| 360 | return $this; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @see IOrderSummary::getType() |
||
| 365 | */ |
||
| 366 | public function getType() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @see IOrderSummary::setType() |
||
| 373 | * @codeCoverageIgnore |
||
| 374 | */ |
||
| 375 | public function setType($type) |
||
| 376 | { |
||
| 377 | $this->type = $type; |
||
| 378 | return $this; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @see IOrderSummary::getParentRef() |
||
| 383 | */ |
||
| 384 | public function getParentRef() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @see IOrderSummary::setParentRef() |
||
| 391 | * @codeCoverageIgnore |
||
| 392 | */ |
||
| 393 | public function setParentRef($parentRef) |
||
| 394 | { |
||
| 395 | $this->parentRef = $parentRef; |
||
| 396 | return $this; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @see IOrderSummary::getDerivedOrder() |
||
| 401 | */ |
||
| 402 | public function getDerivedOrder() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @see IOrderSummary::setDerivedOrder() |
||
| 409 | * @codeCoverageIgnore |
||
| 410 | */ |
||
| 411 | public function setDerivedOrder($derivedOrder) |
||
| 412 | { |
||
| 413 | $this->derivedOrder = $derivedOrder; |
||
| 414 | return $this; |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @see TPayload::serializeContents() |
||
| 419 | */ |
||
| 420 | protected function serializeContents() |
||
| 421 | { |
||
| 422 | return $this->serializeOptionalXmlEncodedValue('CustomerOrderId', $this->getCustomerOrderId()) |
||
| 423 | . $this->serializeOptionalXmlEncodedValue('CustomerId', $this->getCustomerId()) |
||
| 424 | . $this->serializeOptionalDateValue('OrderDate', 'c', $this->getOrderDate()) |
||
| 425 | . $this->serializeOptionalXmlEncodedValue('DashboardRepId', $this->getDashboardRepId()) |
||
| 426 | . $this->serializeOptionalXmlEncodedValue('Status', $this->getStatus()) |
||
| 427 | . $this->serializeRequiredValue('OrderTotal', $this->xmlEncode($this->getOrderTotal())) |
||
| 428 | . $this->serializeOptionalXmlEncodedValue('Source', $this->getSource()) |
||
| 429 | . $this->serializeChainedOrder('ChainedOrder', $this->xmlEncode($this->getChainedOrder())) |
||
| 430 | . $this->serializeDerivedOrder('DerivedOrder', $this->xmlEncode($this->getDerivedOrder())); |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Serialize the chain order node. |
||
| 435 | * @return string | null |
||
| 436 | */ |
||
| 437 | View Code Duplication | protected function serializeChainedOrder($node, $value) |
|
| 438 | { |
||
| 439 | $parentRef = $this->getParentRef(); |
||
| 440 | $typeAttribute = $this->serializeOptionalAttribute('type', $this->xmlEncode($this->getType())); |
||
| 441 | $parentRefAttribute = $this->serializeOptionalAttribute('parentRef', $this->xmlEncode($parentRef)); |
||
| 442 | return $parentRef |
||
| 443 | ? sprintf('<%s %s %s>%s</%1$s>', $node, $typeAttribute, $parentRefAttribute, $value) : null; |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Serialize the derived order node. |
||
| 448 | * @return string | null |
||
| 449 | */ |
||
| 450 | protected function serializeDerivedOrder($node, $value) |
||
| 451 | { |
||
| 452 | $parentRef = $this->getParentRef(); |
||
| 453 | $parentRefAttribute = $this->serializeOptionalAttribute('parentRef', $this->xmlEncode($parentRef)); |
||
| 454 | return $parentRef |
||
| 455 | ? sprintf('<%s %s>%s</%1$s>', $node, $parentRefAttribute, $value) : null; |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @see TPayload::getRootNodeName() |
||
| 460 | */ |
||
| 461 | protected function getRootNodeName() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @see TPayload::getXmlNamespace() |
||
| 468 | */ |
||
| 469 | protected function getXmlNamespace() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @see TPayload::getRootAttributes() |
||
| 476 | */ |
||
| 477 | protected function getRootAttributes() |
||
| 478 | { |
||
| 479 | $orderType = $this->getOrderType(); |
||
| 480 | $orderPurpose = $this->getOrderPurpose(); |
||
| 481 | $testType = $this->getTestType(); |
||
| 482 | $cancellable = $this->getCancellable(); |
||
| 483 | return array_merge( |
||
| 484 | ['id' => $this->getId()], |
||
| 485 | $orderType ? ['orderType' => $orderType] : [], |
||
| 486 | $orderPurpose ? ['orderPurpose' => $orderPurpose] : [], |
||
| 487 | $testType ? ['testType' => $testType] : [], |
||
| 488 | ['modifiedTime' => $this->getModifiedTime()->format('c')], |
||
| 489 | $cancellable ? ['cancellable' => $cancellable] : [] |
||
| 490 | ); |
||
| 491 | } |
||
| 492 | } |
||
| 493 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.