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 |
||
29 | class OrderDestinationIterable extends SPLObjectStorage implements IOrderDestinationIterable |
||
30 | { |
||
31 | use TIterablePayload; |
||
32 | |||
33 | const SUBPAYLOAD_XPATH = 'x:MailingAddress|x:StoreLocation|x:Email'; |
||
34 | const ROOT_NODE = 'Destinations'; |
||
35 | |||
36 | /** |
||
37 | * @param IValidatorIterator |
||
38 | * @param ISchemaValidator |
||
39 | * @param IPayloadMap |
||
40 | * @param LoggerInterface |
||
41 | * @param IPayload |
||
42 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
43 | */ |
||
44 | public function __construct( |
||
45 | IValidatorIterator $validators, |
||
46 | ISchemaValidator $schemaValidator, |
||
|
|||
47 | IPayloadMap $payloadMap, |
||
48 | LoggerInterface $logger, |
||
49 | IPayload $parentPayload = null |
||
50 | ) { |
||
51 | $this->logger = $logger; |
||
52 | $this->validators = $validators; |
||
53 | $this->payloadMap = $payloadMap; |
||
54 | $this->parentPayload = $parentPayload; |
||
55 | $this->payloadFactory = new PayloadFactory; |
||
56 | } |
||
57 | |||
58 | View Code Duplication | public function deserialize($serializedData) |
|
59 | { |
||
60 | $xpath = $this->getPayloadAsXPath($serializedData); |
||
61 | foreach ($xpath->query($this->getSubpayloadXPath()) as $subpayloadNode) { |
||
62 | switch ($subpayloadNode->nodeName) { |
||
63 | case 'MailingAddress': |
||
64 | $pl = $this->getEmptyMailingAddress(); |
||
65 | break; |
||
66 | case 'StoreLocation': |
||
67 | $pl = $this->getEmptyStoreLocation(); |
||
68 | break; |
||
69 | case 'Email': |
||
70 | $pl = $this->getEmptyEmailAddress(); |
||
71 | break; |
||
72 | } |
||
73 | if (isset($pl)) { |
||
74 | $pl->deserialize($subpayloadNode->C14N()); |
||
75 | $this->offsetSet($pl); |
||
76 | } |
||
77 | } |
||
78 | $this->validate(); |
||
79 | return $this; |
||
80 | } |
||
81 | |||
82 | protected function serializeContents() |
||
102 | |||
103 | public function getNewSubpayload() |
||
107 | |||
108 | /** |
||
109 | * Get a new, empty mailing address destination object. |
||
110 | * |
||
111 | * @return IMailingAddress |
||
112 | */ |
||
113 | public function getEmptyMailingAddress() |
||
117 | |||
118 | /** |
||
119 | * Get a new, empty store location destination object. |
||
120 | * |
||
121 | * @return IStoreLocation |
||
122 | */ |
||
123 | public function getEmptyStoreLocation() |
||
127 | |||
128 | /** |
||
129 | * Get a new, empty email address destination object. |
||
130 | * |
||
131 | * @return IEmailAddressDestination |
||
132 | */ |
||
133 | public function getEmptyEmailAddress() |
||
137 | |||
138 | public function getSubpayloadXPath() |
||
142 | |||
143 | public function getRootNodeName() |
||
147 | |||
148 | public function getXmlNamespace() |
||
152 | } |
||
153 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.