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 Mapper 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 Mapper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Mapper |
||
28 | { |
||
29 | /** |
||
30 | * FieldValue converter registry. |
||
31 | * |
||
32 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry |
||
33 | */ |
||
34 | protected $converterRegistry; |
||
35 | |||
36 | /** |
||
37 | * Caching language handler. |
||
38 | * |
||
39 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
||
40 | */ |
||
41 | protected $languageHandler; |
||
42 | |||
43 | /** |
||
44 | * Creates a new mapper. |
||
45 | * |
||
46 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry $converterRegistry |
||
47 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
48 | */ |
||
49 | public function __construct(Registry $converterRegistry, LanguageHandler $languageHandler) |
||
50 | { |
||
51 | $this->converterRegistry = $converterRegistry; |
||
52 | $this->languageHandler = $languageHandler; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Creates a Content from the given $struct and $currentVersionNo. |
||
57 | * |
||
58 | * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct |
||
59 | * @param mixed $currentVersionNo |
||
60 | * |
||
61 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo |
||
62 | */ |
||
63 | private function createContentInfoFromCreateStruct(CreateStruct $struct, $currentVersionNo = 1) |
||
64 | { |
||
65 | $contentInfo = new ContentInfo(); |
||
66 | |||
67 | $contentInfo->id = null; |
||
68 | $contentInfo->contentTypeId = $struct->typeId; |
||
69 | $contentInfo->sectionId = $struct->sectionId; |
||
70 | $contentInfo->ownerId = $struct->ownerId; |
||
71 | $contentInfo->alwaysAvailable = $struct->alwaysAvailable; |
||
72 | $contentInfo->remoteId = $struct->remoteId; |
||
73 | $contentInfo->mainLanguageCode = $this->languageHandler |
||
74 | ->load(isset($struct->mainLanguageId) ? $struct->mainLanguageId : $struct->initialLanguageId) |
||
75 | ->languageCode; |
||
76 | $contentInfo->name = isset($struct->name[$contentInfo->mainLanguageCode]) |
||
77 | ? $struct->name[$contentInfo->mainLanguageCode] |
||
78 | : ''; |
||
79 | // For drafts published and modified timestamps should be 0 |
||
80 | $contentInfo->publicationDate = 0; |
||
81 | $contentInfo->modificationDate = 0; |
||
82 | $contentInfo->currentVersionNo = $currentVersionNo; |
||
83 | $contentInfo->isPublished = false; |
||
84 | |||
85 | return $contentInfo; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Creates a new version for the given $struct and $versionNo. |
||
90 | * |
||
91 | * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct |
||
92 | * @param mixed $versionNo |
||
93 | * |
||
94 | * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo |
||
95 | */ |
||
96 | public function createVersionInfoFromCreateStruct(CreateStruct $struct, $versionNo) |
||
121 | |||
122 | /** |
||
123 | * Creates a new version for the given $content. |
||
124 | * |
||
125 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
126 | * @param mixed $versionNo |
||
127 | * @param mixed $userId |
||
128 | * |
||
129 | * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo |
||
130 | */ |
||
131 | public function createVersionInfoForContent(Content $content, $versionNo, $userId) |
||
147 | |||
148 | /** |
||
149 | * Converts value of $field to storage value. |
||
150 | * |
||
151 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
152 | * |
||
153 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue |
||
154 | */ |
||
155 | public function convertToStorageValue(Field $field) |
||
168 | |||
169 | /** |
||
170 | * Extracts Content objects (and nested) from database result $rows. |
||
171 | * |
||
172 | * Expects database rows to be indexed by keys of the format |
||
173 | * |
||
174 | * "$tableName_$columnName" |
||
175 | * |
||
176 | * @param array $rows |
||
177 | * @param array $nameRows |
||
178 | * |
||
179 | * @return \eZ\Publish\SPI\Persistence\Content[] |
||
180 | */ |
||
181 | public function extractContentFromRows(array $rows, array $nameRows) |
||
182 | { |
||
183 | $versionedNameData = array(); |
||
184 | foreach ($nameRows as $row) { |
||
185 | $contentId = (int)$row['ezcontentobject_name_contentobject_id']; |
||
186 | $versionNo = (int)$row['ezcontentobject_name_content_version']; |
||
187 | $versionedNameData[$contentId][$versionNo][$row['ezcontentobject_name_content_translation']] = $row['ezcontentobject_name_name']; |
||
188 | } |
||
189 | |||
190 | $contentInfos = array(); |
||
191 | $versionInfos = array(); |
||
192 | $fields = array(); |
||
193 | |||
194 | foreach ($rows as $row) { |
||
195 | $contentId = (int)$row['ezcontentobject_id']; |
||
196 | if (!isset($contentInfos[$contentId])) { |
||
197 | $contentInfos[$contentId] = $this->extractContentInfoFromRow($row, 'ezcontentobject_'); |
||
198 | } |
||
199 | if (!isset($versionInfos[$contentId])) { |
||
200 | $versionInfos[$contentId] = array(); |
||
201 | } |
||
202 | |||
203 | $versionId = (int)$row['ezcontentobject_version_id']; |
||
204 | if (!isset($versionInfos[$contentId][$versionId])) { |
||
205 | $versionInfos[$contentId][$versionId] = $this->extractVersionInfoFromRow($row); |
||
206 | } |
||
207 | |||
208 | $fieldId = (int)$row['ezcontentobject_attribute_id']; |
||
209 | View Code Duplication | if (!isset($fields[$contentId][$versionId][$fieldId])) { |
|
210 | $fields[$contentId][$versionId][$fieldId] = $this->extractFieldFromRow($row); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | $results = array(); |
||
215 | foreach ($contentInfos as $contentId => $contentInfo) { |
||
216 | foreach ($versionInfos[$contentId] as $versionId => $versionInfo) { |
||
217 | // Fallback to just main language name if versioned name data is missing |
||
218 | View Code Duplication | if (isset($versionedNameData[$contentId][$versionInfo->versionNo])) { |
|
219 | $names = $versionedNameData[$contentId][$versionInfo->versionNo]; |
||
220 | } else { |
||
221 | $names = [$contentInfo->mainLanguageCode => $contentInfo->name]; |
||
222 | } |
||
223 | |||
224 | $content = new Content(); |
||
225 | $content->versionInfo = $versionInfo; |
||
226 | $content->versionInfo->names = $names; |
||
227 | $content->versionInfo->contentInfo = $contentInfo; |
||
228 | $content->fields = array_values($fields[$contentId][$versionId]); |
||
229 | $results[] = $content; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | return $results; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Extracts a ContentInfo object from $row. |
||
238 | * |
||
239 | * @param array $row |
||
240 | * @param string $prefix Prefix for row keys, which are initially mapped by ezcontentobject fields |
||
241 | * @param string $treePrefix Prefix for tree row key, which are initially mapped by ezcontentobject_tree_ fields |
||
242 | * |
||
243 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo |
||
244 | */ |
||
245 | public function extractContentInfoFromRow(array $row, $prefix = '', $treePrefix = 'ezcontentobject_tree_') |
||
264 | |||
265 | /** |
||
266 | * Extracts ContentInfo objects from $rows. |
||
267 | * |
||
268 | * @param array $rows |
||
269 | * @param string $prefix Prefix for row keys, which are initially mapped by ezcontentobject fields |
||
270 | * @param string $treePrefix Prefix for tree row key, which are initially mapped by ezcontentobject_tree_ fields |
||
271 | * |
||
272 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo[] |
||
273 | */ |
||
274 | public function extractContentInfoFromRows(array $rows, $prefix = '', $treePrefix = 'ezcontentobject_tree_') |
||
283 | |||
284 | /** |
||
285 | * Extracts a VersionInfo object from $row. |
||
286 | * |
||
287 | * This method will return VersionInfo with incomplete data. It is intended to be used only by |
||
288 | * {@link self::extractContentFromRows} where missing data will be filled in. |
||
289 | * |
||
290 | * @param array $row |
||
291 | * @param array $names |
||
292 | * |
||
293 | * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo |
||
294 | */ |
||
295 | private function extractVersionInfoFromRow(array $row, array $names = array()) |
||
311 | |||
312 | /** |
||
313 | * Extracts a VersionInfo object from $row. |
||
314 | * |
||
315 | * @param array $rows |
||
316 | * @param array $nameRows |
||
317 | * |
||
318 | * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo[] |
||
319 | */ |
||
320 | public function extractVersionInfoListFromRows(array $rows, array $nameRows) |
||
349 | |||
350 | /** |
||
351 | * @todo use langmask handler for this |
||
352 | * |
||
353 | * @param int $languageMask |
||
354 | * |
||
355 | * @return array |
||
356 | */ |
||
357 | View Code Duplication | public function extractLanguageIdsFromMask($languageMask) |
|
373 | |||
374 | /** |
||
375 | * Extracts a Field from $row. |
||
376 | * |
||
377 | * @param array $row |
||
378 | * |
||
379 | * @return Field |
||
380 | */ |
||
381 | protected function extractFieldFromRow(array $row) |
||
394 | |||
395 | /** |
||
396 | * Extracts a FieldValue of $type from $row. |
||
397 | * |
||
398 | * @param array $row |
||
399 | * @param string $type |
||
400 | * |
||
401 | * @return \eZ\Publish\SPI\Persistence\Content\FieldValue |
||
402 | * |
||
403 | * @throws \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\Exception\NotFound |
||
404 | * if the necessary converter for $type could not be found. |
||
405 | */ |
||
406 | protected function extractFieldValueFromRow(array $row, $type) |
||
430 | |||
431 | /** |
||
432 | * Creates CreateStruct from $content. |
||
433 | * |
||
434 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
435 | * |
||
436 | * @return \eZ\Publish\SPI\Persistence\Content\CreateStruct |
||
437 | */ |
||
438 | public function createCreateStructFromContent(Content $content) |
||
460 | |||
461 | /** |
||
462 | * Extracts relation objects from $rows. |
||
463 | */ |
||
464 | public function extractRelationsFromRows(array $rows) |
||
477 | |||
478 | /** |
||
479 | * Extracts a Relation object from a $row. |
||
480 | * |
||
481 | * @param array $row Associative array representing a relation |
||
482 | * |
||
483 | * @return \eZ\Publish\SPI\Persistence\Content\Relation |
||
484 | */ |
||
485 | protected function extractRelationFromRow(array $row) |
||
501 | |||
502 | /** |
||
503 | * Creates a Content from the given $struct. |
||
504 | * |
||
505 | * @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $struct |
||
506 | * |
||
507 | * @return \eZ\Publish\SPI\Persistence\Content\Relation |
||
508 | */ |
||
509 | public function createRelationFromCreateStruct(RelationCreateStruct $struct) |
||
521 | } |
||
522 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.