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 |
||
21 | class RelationListConverter implements Converter |
||
22 | { |
||
23 | /** |
||
24 | * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler |
||
25 | */ |
||
26 | private $db; |
||
27 | |||
28 | /** |
||
29 | * Create instance of RelationList converter. |
||
30 | * |
||
31 | * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $db |
||
32 | */ |
||
33 | public function __construct(DatabaseHandler $db) |
||
34 | { |
||
35 | $this->db = $db; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Converts data from $value to $storageFieldValue. |
||
40 | * |
||
41 | * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $value |
||
42 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $storageFieldValue |
||
43 | */ |
||
44 | public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue) |
||
45 | { |
||
46 | $doc = new DOMDocument('1.0', 'utf-8'); |
||
47 | $root = $doc->createElement('related-objects'); |
||
48 | $doc->appendChild($root); |
||
49 | |||
50 | $relationList = $doc->createElement('relation-list'); |
||
51 | $data = $this->getRelationXmlHashFromDB($value->data['destinationContentIds']); |
||
52 | $priority = 0; |
||
53 | |||
54 | foreach ($value->data['destinationContentIds'] as $id) { |
||
55 | if (!isset($data[$id][0])) { |
||
56 | // Ignore deleted content items (we can't throw as it would block ContentService->createContentDraft()) |
||
57 | continue; |
||
58 | } |
||
59 | $row = $data[$id][0]; |
||
60 | $row['ezcontentobject_id'] = $id; |
||
61 | $row['priority'] = ($priority += 1); |
||
62 | |||
63 | $relationItem = $doc->createElement('relation-item'); |
||
64 | foreach (self::dbAttributeMap() as $domAttrKey => $propertyKey) { |
||
65 | if (!isset($row[$propertyKey])) { |
||
66 | // left join data missing, ignore the given attribute (content in trash missing location) |
||
67 | continue; |
||
68 | } |
||
69 | |||
70 | $relationItem->setAttribute($domAttrKey, $row[$propertyKey]); |
||
71 | } |
||
72 | $relationList->appendChild($relationItem); |
||
73 | unset($relationItem); |
||
74 | } |
||
75 | |||
76 | $root->appendChild($relationList); |
||
77 | $doc->appendChild($root); |
||
78 | |||
79 | $storageFieldValue->dataText = $doc->saveXML(); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Converts data from $value to $fieldValue. |
||
84 | * |
||
85 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
||
86 | * @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue |
||
87 | */ |
||
88 | public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue) |
||
89 | { |
||
90 | $fieldValue->data = array('destinationContentIds' => array()); |
||
91 | if ($value->dataText === null) { |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | $priorityByContentId = array(); |
||
96 | |||
97 | $dom = new DOMDocument('1.0', 'utf-8'); |
||
98 | if ($dom->loadXML($value->dataText) === true) { |
||
99 | foreach ($dom->getElementsByTagName('relation-item') as $relationItem) { |
||
100 | /* @var \DOMElement $relationItem */ |
||
101 | $priorityByContentId[$relationItem->getAttribute('contentobject-id')] = |
||
102 | $relationItem->getAttribute('priority'); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | asort($priorityByContentId, SORT_NUMERIC); |
||
107 | |||
108 | $fieldValue->data['destinationContentIds'] = array_keys($priorityByContentId); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Converts field definition data in $fieldDef into $storageFieldDef. |
||
113 | * |
||
114 | * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef |
||
115 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef |
||
116 | */ |
||
117 | public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef) |
||
118 | { |
||
119 | $fieldSettings = $fieldDef->fieldTypeConstraints->fieldSettings; |
||
120 | $doc = new DOMDocument('1.0', 'utf-8'); |
||
121 | $root = $doc->createElement('related-objects'); |
||
122 | $doc->appendChild($root); |
||
123 | |||
124 | $constraints = $doc->createElement('constraints'); |
||
125 | View Code Duplication | if (!empty($fieldSettings['selectionContentTypes'])) { |
|
|
|||
126 | foreach ($fieldSettings['selectionContentTypes'] as $typeIdentifier) { |
||
127 | $allowedClass = $doc->createElement('allowed-class'); |
||
128 | $allowedClass->setAttribute('contentclass-identifier', $typeIdentifier); |
||
129 | $constraints->appendChild($allowedClass); |
||
130 | unset($allowedClass); |
||
131 | } |
||
132 | } |
||
133 | $root->appendChild($constraints); |
||
134 | |||
135 | $type = $doc->createElement('type'); |
||
136 | $type->setAttribute('value', 2); //Deprecated advance object relation list type, set since 4.x does |
||
137 | $root->appendChild($type); |
||
138 | |||
139 | $objectClass = $doc->createElement('object_class'); |
||
140 | $objectClass->setAttribute('value', ''); //Deprecated advance object relation class type, set since 4.x does |
||
141 | $root->appendChild($objectClass); |
||
142 | |||
143 | $selectionType = $doc->createElement('selection_type'); |
||
144 | View Code Duplication | if (isset($fieldSettings['selectionMethod'])) { |
|
145 | $selectionType->setAttribute('value', (int)$fieldSettings['selectionMethod']); |
||
146 | } else { |
||
147 | $selectionType->setAttribute('value', 0); |
||
148 | } |
||
149 | $root->appendChild($selectionType); |
||
150 | |||
151 | $defaultLocation = $doc->createElement('contentobject-placement'); |
||
152 | if (!empty($fieldSettings['selectionDefaultLocation'])) { |
||
153 | $defaultLocation->setAttribute('node-id', (int)$fieldSettings['selectionDefaultLocation']); |
||
154 | } |
||
155 | $root->appendChild($defaultLocation); |
||
156 | |||
157 | $doc->appendChild($root); |
||
158 | $storageDef->dataText5 = $doc->saveXML(); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Converts field definition data in $storageDef into $fieldDef. |
||
163 | * |
||
164 | * <code> |
||
165 | * <?xml version="1.0" encoding="utf-8"?> |
||
166 | * <related-objects> |
||
167 | * <constraints> |
||
168 | * <allowed-class contentclass-identifier="blog_post"/> |
||
169 | * </constraints> |
||
170 | * <type value="2"/> |
||
171 | * <selection_type value="1"/> |
||
172 | * <object_class value=""/> |
||
173 | * <contentobject-placement node-id="67"/> |
||
174 | * </related-objects> |
||
175 | * |
||
176 | * <?xml version="1.0" encoding="utf-8"?> |
||
177 | * <related-objects> |
||
178 | * <constraints/> |
||
179 | * <type value="2"/> |
||
180 | * <selection_type value="0"/> |
||
181 | * <object_class value=""/> |
||
182 | * <contentobject-placement/> |
||
183 | * </related-objects> |
||
184 | * </code> |
||
185 | * |
||
186 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition $storageDef |
||
187 | * @param \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition $fieldDef |
||
188 | */ |
||
189 | public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef) |
||
190 | { |
||
191 | // default settings |
||
192 | $fieldDef->fieldTypeConstraints->fieldSettings = [ |
||
193 | 'selectionMethod' => 0, |
||
194 | 'selectionDefaultLocation' => null, |
||
195 | 'selectionContentTypes' => [], |
||
196 | ]; |
||
197 | |||
198 | // default value |
||
199 | $fieldDef->defaultValue = new FieldValue(); |
||
200 | $fieldDef->defaultValue->data = array('destinationContentIds' => array()); |
||
201 | |||
202 | if ($storageDef->dataText5 === null) { |
||
203 | return; |
||
204 | } |
||
205 | |||
206 | // read settings from storage |
||
207 | $fieldSettings = &$fieldDef->fieldTypeConstraints->fieldSettings; |
||
208 | $dom = new DOMDocument('1.0', 'utf-8'); |
||
209 | if (empty($storageDef->dataText5) || $dom->loadXML($storageDef->dataText5) !== true) { |
||
210 | return; |
||
211 | } |
||
212 | |||
213 | View Code Duplication | if ($selectionType = $dom->getElementsByTagName('selection_type')) { |
|
214 | $fieldSettings['selectionMethod'] = (int)$selectionType->item(0)->getAttribute('value'); |
||
215 | } |
||
216 | |||
217 | View Code Duplication | if ( |
|
218 | ($defaultLocation = $dom->getElementsByTagName('contentobject-placement')) && |
||
219 | $defaultLocation->item(0)->hasAttribute('node-id') |
||
220 | ) { |
||
221 | $fieldSettings['selectionDefaultLocation'] = (int)$defaultLocation->item(0)->getAttribute('node-id'); |
||
222 | } |
||
223 | |||
224 | if (!($constraints = $dom->getElementsByTagName('constraints'))) { |
||
225 | return; |
||
226 | } |
||
227 | |||
228 | foreach ($constraints->item(0)->getElementsByTagName('allowed-class') as $allowedClass) { |
||
229 | $fieldSettings['selectionContentTypes'][] = $allowedClass->getAttribute('contentclass-identifier'); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Returns the name of the index column in the attribute table. |
||
235 | * |
||
236 | * Returns the name of the index column the datatype uses, which is either |
||
237 | * "sort_key_int" or "sort_key_string". This column is then used for |
||
238 | * filtering and sorting for this type. |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | public function getIndexColumn() |
||
246 | |||
247 | /** |
||
248 | * @param mixed[] $destinationContentIds |
||
249 | * |
||
250 | * @throws \Exception |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | protected function getRelationXmlHashFromDB(array $destinationContentIds) |
||
309 | |||
310 | /** |
||
311 | * @return array |
||
312 | */ |
||
313 | private static function dbAttributeMap() |
||
329 | } |
||
330 |
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.