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 |
||
27 | class ItemRelationship implements IItemRelationship |
||
28 | { |
||
29 | use TPayload, TOrderItemReferenceContainer; |
||
30 | |||
31 | const ROOT_NODE = 'Relationship'; |
||
32 | |||
33 | /** @var string */ |
||
34 | protected $parentItemId; |
||
35 | /** @var IOrderItem */ |
||
36 | protected $parentItem; |
||
37 | /** @var string */ |
||
38 | protected $type; |
||
39 | /** @var string */ |
||
40 | protected $name; |
||
41 | /** @var array */ |
||
42 | protected $allowedRelationshipTypes = [ |
||
43 | self::TYPE_BUNDLE, |
||
44 | self::TYPE_WARRANTY, |
||
45 | self::TYPE_KIT, |
||
46 | self::TYPE_DYNAMIC, |
||
47 | self::TYPE_CUSTOMIZATION, |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * @param IValidatorIterator |
||
52 | * @param ISchemaValidator |
||
53 | * @param IPayloadMap |
||
54 | * @param LoggerInterface |
||
55 | * @param IPayload |
||
56 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
57 | */ |
||
58 | public function __construct( |
||
59 | IValidatorIterator $validators, |
||
60 | ISchemaValidator $schemaValidator, |
||
61 | IPayloadMap $payloadMap, |
||
62 | LoggerInterface $logger, |
||
63 | IPayload $parentPayload = null |
||
64 | ) { |
||
65 | $this->logger = $logger; |
||
|
|||
66 | $this->validators = $validators; |
||
67 | $this->schemaValidator = $schemaValidator; |
||
68 | $this->payloadMap = $payloadMap; |
||
69 | $this->parentPayload = $parentPayload; |
||
70 | $this->payloadFactory = new PayloadFactory; |
||
71 | |||
72 | $this->extractionPaths = [ |
||
73 | 'parentItemId' => 'string(@parent)', |
||
74 | 'type' => 'string(x:Type)', |
||
75 | ]; |
||
76 | $this->optionalExtractionPaths = [ |
||
77 | 'name' => 'x:Name', |
||
78 | ]; |
||
79 | $this->subpayloadExtractionPaths = [ |
||
80 | 'itemReferences' => 'x:Members', |
||
81 | ]; |
||
82 | |||
83 | $this->itemReferences = $this->buildPayloadForInterface( |
||
84 | self::ITEM_REFERENCE_ITERABLE_INTERFACE |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | public function getParentItem() |
|
89 | { |
||
90 | if (!$this->parentItem && $this->parentItemId) { |
||
91 | $itemContainer = $this->getItemContainer(); |
||
92 | if ($itemContainer) { |
||
93 | foreach ($itemContainer->getOrderItems() as $item) { |
||
94 | if ($item->getId() === $this->parentItemId) { |
||
95 | $this->parentItem = $item; |
||
96 | break; |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | return $this->parentItem; |
||
102 | } |
||
103 | |||
104 | View Code Duplication | public function setParentItem(IOrderItem $parentItem) |
|
105 | { |
||
106 | $this->parentItem = $parentItem; |
||
107 | $itemContainer = $this->getItemContainer(); |
||
108 | if ($itemContainer) { |
||
109 | $itemContainer->getOrderItems()->offsetSet($parentItem); |
||
110 | } |
||
111 | return $this; |
||
112 | } |
||
113 | |||
114 | public function getParentItemId() |
||
115 | { |
||
116 | $item = $this->getParentItem(); |
||
117 | return $item ? $item->getId() : $this->parentItemId; |
||
118 | } |
||
119 | |||
120 | public function getType() |
||
124 | |||
125 | public function setType($type) |
||
126 | { |
||
127 | $this->type = in_array($type, $this->allowedRelationshipTypes) ? $type : null; |
||
128 | return $this; |
||
129 | } |
||
130 | |||
131 | public function getName() |
||
135 | |||
136 | public function setName($name) |
||
137 | { |
||
138 | $this->name = $name; |
||
139 | return $this; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Get the order item container associated with this payload. Will return |
||
144 | * null if one cannot be found. |
||
145 | * |
||
146 | * @return IOrderItemContainer|null |
||
147 | */ |
||
148 | protected function getItemContainer() |
||
149 | { |
||
150 | $this->debug = true; |
||
151 | return $this->getAncestorPayloadOfType('\eBayEnterprise\RetailOrderManagement\Payload\Order\IOrderItemContainer'); |
||
152 | } |
||
153 | |||
154 | protected function serializeContents() |
||
155 | { |
||
156 | return $this->getItemReferences()->setRootNodeName('Members')->serialize() |
||
157 | . "<Type>{$this->xmlEncode($this->getType())}</Type>" |
||
158 | . $this->serializeOptionalXmlEncodedValue('Name', $this->getName()); |
||
159 | } |
||
160 | |||
161 | protected function getRootAttributes() |
||
165 | |||
166 | protected function getRootNodeName() |
||
170 | |||
171 | protected function getXmlNamespace() |
||
175 | } |
||
176 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..