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 |
||
| 28 | class Mapper |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * FieldValue converter registry. |
||
| 32 | * |
||
| 33 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry |
||
| 34 | */ |
||
| 35 | protected $converterRegistry; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Caching language handler. |
||
| 39 | * |
||
| 40 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
||
| 41 | */ |
||
| 42 | protected $languageHandler; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Creates a new mapper. |
||
| 46 | * |
||
| 47 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry $converterRegistry |
||
| 48 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 49 | */ |
||
| 50 | public function __construct(Registry $converterRegistry, LanguageHandler $languageHandler) |
||
| 51 | { |
||
| 52 | $this->converterRegistry = $converterRegistry; |
||
| 53 | $this->languageHandler = $languageHandler; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Creates a Content from the given $struct and $currentVersionNo. |
||
| 58 | * |
||
| 59 | * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct |
||
| 60 | * @param mixed $currentVersionNo |
||
| 61 | * |
||
| 62 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo |
||
| 63 | */ |
||
| 64 | private function createContentInfoFromCreateStruct(CreateStruct $struct, $currentVersionNo = 1) |
||
| 65 | { |
||
| 66 | $contentInfo = new ContentInfo(); |
||
| 67 | |||
| 68 | $contentInfo->id = null; |
||
| 69 | $contentInfo->contentTypeId = $struct->typeId; |
||
| 70 | $contentInfo->sectionId = $struct->sectionId; |
||
| 71 | $contentInfo->ownerId = $struct->ownerId; |
||
| 72 | $contentInfo->alwaysAvailable = $struct->alwaysAvailable; |
||
| 73 | $contentInfo->remoteId = $struct->remoteId; |
||
| 74 | $contentInfo->mainLanguageCode = $this->languageHandler |
||
| 75 | ->load(isset($struct->mainLanguageId) ? $struct->mainLanguageId : $struct->initialLanguageId) |
||
| 76 | ->languageCode; |
||
| 77 | $contentInfo->name = isset($struct->name[$contentInfo->mainLanguageCode]) |
||
| 78 | ? $struct->name[$contentInfo->mainLanguageCode] |
||
| 79 | : ''; |
||
| 80 | // For drafts published and modified timestamps should be 0 |
||
| 81 | $contentInfo->publicationDate = 0; |
||
| 82 | $contentInfo->modificationDate = 0; |
||
| 83 | $contentInfo->currentVersionNo = $currentVersionNo; |
||
| 84 | $contentInfo->status = ContentInfo::STATUS_DRAFT; |
||
| 85 | $contentInfo->isPublished = false; |
||
| 86 | |||
| 87 | return $contentInfo; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Creates a new version for the given $struct and $versionNo. |
||
| 92 | * |
||
| 93 | * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct |
||
| 94 | * @param mixed $versionNo |
||
| 95 | * |
||
| 96 | * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo |
||
| 97 | */ |
||
| 98 | public function createVersionInfoFromCreateStruct(CreateStruct $struct, $versionNo) |
||
| 99 | { |
||
| 100 | $versionInfo = new VersionInfo(); |
||
| 101 | |||
| 102 | $versionInfo->id = null; |
||
| 103 | $versionInfo->contentInfo = $this->createContentInfoFromCreateStruct($struct, $versionNo); |
||
| 104 | $versionInfo->versionNo = $versionNo; |
||
| 105 | $versionInfo->creatorId = $struct->ownerId; |
||
| 106 | $versionInfo->status = VersionInfo::STATUS_DRAFT; |
||
| 107 | $versionInfo->initialLanguageCode = $this->languageHandler->load($struct->initialLanguageId)->languageCode; |
||
| 108 | $versionInfo->creationDate = $struct->modified; |
||
| 109 | $versionInfo->modificationDate = $struct->modified; |
||
| 110 | $versionInfo->names = $struct->name; |
||
| 111 | |||
| 112 | $languages = []; |
||
| 113 | foreach ($struct->fields as $field) { |
||
| 114 | if (!isset($languages[$field->languageCode])) { |
||
| 115 | $languages[$field->languageCode] = true; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | $versionInfo->languageCodes = array_keys($languages); |
||
|
|
|||
| 119 | |||
| 120 | return $versionInfo; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Creates a new version for the given $content. |
||
| 125 | * |
||
| 126 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
| 127 | * @param mixed $versionNo |
||
| 128 | * @param mixed $userId |
||
| 129 | * |
||
| 130 | * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo |
||
| 131 | */ |
||
| 132 | public function createVersionInfoForContent(Content $content, $versionNo, $userId) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Converts value of $field to storage value. |
||
| 151 | * |
||
| 152 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
| 153 | * |
||
| 154 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue |
||
| 155 | */ |
||
| 156 | public function convertToStorageValue(Field $field) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Extracts Content objects (and nested) from database result $rows. |
||
| 172 | * |
||
| 173 | * Expects database rows to be indexed by keys of the format |
||
| 174 | * |
||
| 175 | * "$tableName_$columnName" |
||
| 176 | * |
||
| 177 | * @param array $rows |
||
| 178 | * @param array $nameRows |
||
| 179 | * |
||
| 180 | * @return \eZ\Publish\SPI\Persistence\Content[] |
||
| 181 | */ |
||
| 182 | public function extractContentFromRows(array $rows, array $nameRows, $prefix = 'ezcontentobject_') |
||
| 183 | { |
||
| 184 | $versionedNameData = array(); |
||
| 185 | foreach ($nameRows as $row) { |
||
| 186 | $contentId = (int)$row['ezcontentobject_name_contentobject_id']; |
||
| 187 | $versionNo = (int)$row['ezcontentobject_name_content_version']; |
||
| 188 | $versionedNameData[$contentId][$versionNo][$row['ezcontentobject_name_content_translation']] = $row['ezcontentobject_name_name']; |
||
| 189 | } |
||
| 190 | |||
| 191 | $contentInfos = array(); |
||
| 192 | $versionInfos = array(); |
||
| 193 | $fields = array(); |
||
| 194 | |||
| 195 | foreach ($rows as $row) { |
||
| 196 | $contentId = (int)$row["{$prefix}id"]; |
||
| 197 | if (!isset($contentInfos[$contentId])) { |
||
| 198 | $contentInfos[$contentId] = $this->extractContentInfoFromRow($row, $prefix); |
||
| 199 | } |
||
| 200 | if (!isset($versionInfos[$contentId])) { |
||
| 201 | $versionInfos[$contentId] = array(); |
||
| 202 | } |
||
| 203 | |||
| 204 | $versionId = (int)$row['ezcontentobject_version_id']; |
||
| 205 | if (!isset($versionInfos[$contentId][$versionId])) { |
||
| 206 | $versionInfos[$contentId][$versionId] = $this->extractVersionInfoFromRow($row); |
||
| 207 | } |
||
| 208 | |||
| 209 | $fieldId = (int)$row['ezcontentobject_attribute_id']; |
||
| 210 | View Code Duplication | if (!isset($fields[$contentId][$versionId][$fieldId])) { |
|
| 211 | $fields[$contentId][$versionId][$fieldId] = $this->extractFieldFromRow($row); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | $results = array(); |
||
| 216 | foreach ($contentInfos as $contentId => $contentInfo) { |
||
| 217 | foreach ($versionInfos[$contentId] as $versionId => $versionInfo) { |
||
| 218 | // Fallback to just main language name if versioned name data is missing |
||
| 219 | View Code Duplication | if (isset($versionedNameData[$contentId][$versionInfo->versionNo])) { |
|
| 220 | $names = $versionedNameData[$contentId][$versionInfo->versionNo]; |
||
| 221 | } else { |
||
| 222 | $names = [$contentInfo->mainLanguageCode => $contentInfo->name]; |
||
| 223 | } |
||
| 224 | |||
| 225 | $content = new Content(); |
||
| 226 | $content->versionInfo = $versionInfo; |
||
| 227 | $content->versionInfo->names = $names; |
||
| 228 | $content->versionInfo->contentInfo = $contentInfo; |
||
| 229 | $content->fields = array_values($fields[$contentId][$versionId]); |
||
| 230 | $results[] = $content; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | return $results; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Extracts a ContentInfo object from $row. |
||
| 239 | * |
||
| 240 | * @param array $row |
||
| 241 | * @param string $prefix Prefix for row keys, which are initially mapped by ezcontentobject fields |
||
| 242 | * @param string $treePrefix Prefix for tree row key, which are initially mapped by ezcontentobject_tree_ fields |
||
| 243 | * |
||
| 244 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo |
||
| 245 | */ |
||
| 246 | public function extractContentInfoFromRow(array $row, $prefix = '', $treePrefix = 'ezcontentobject_tree_') |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Extracts ContentInfo objects from $rows. |
||
| 270 | * |
||
| 271 | * @param array $rows |
||
| 272 | * @param string $prefix Prefix for row keys, which are initially mapped by ezcontentobject fields |
||
| 273 | * @param string $treePrefix Prefix for tree row key, which are initially mapped by ezcontentobject_tree_ fields |
||
| 274 | * |
||
| 275 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo[] |
||
| 276 | */ |
||
| 277 | public function extractContentInfoFromRows(array $rows, $prefix = '', $treePrefix = 'ezcontentobject_tree_') |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Extracts a VersionInfo object from $row. |
||
| 289 | * |
||
| 290 | * This method will return VersionInfo with incomplete data. It is intended to be used only by |
||
| 291 | * {@link self::extractContentFromRows} where missing data will be filled in. |
||
| 292 | * |
||
| 293 | * @param array $row |
||
| 294 | * @param array $names |
||
| 295 | * |
||
| 296 | * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo |
||
| 297 | */ |
||
| 298 | private function extractVersionInfoFromRow(array $row, array $names = array()) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Extracts a VersionInfo object from $row. |
||
| 336 | * |
||
| 337 | * @param array $rows |
||
| 338 | * @param array $nameRows |
||
| 339 | * |
||
| 340 | * @return \eZ\Publish\SPI\Persistence\Content\VersionInfo[] |
||
| 341 | */ |
||
| 342 | public function extractVersionInfoListFromRows(array $rows, array $nameRows) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param int $languageMask |
||
| 391 | * @param \eZ\Publish\SPI\Persistence\Content\Language[] $allLanguages |
||
| 392 | * @param int[] &$missing |
||
| 393 | * |
||
| 394 | * @return string[] |
||
| 395 | */ |
||
| 396 | private function extractLanguageCodesFromMask(int $languageMask, array $allLanguages, &$missing = []) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return \eZ\Publish\SPI\Persistence\Content\Language[] |
||
| 419 | */ |
||
| 420 | private function loadAllLanguagesWithIdKey() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Extracts a Field from $row. |
||
| 432 | * |
||
| 433 | * @param array $row |
||
| 434 | * |
||
| 435 | * @return Field |
||
| 436 | */ |
||
| 437 | protected function extractFieldFromRow(array $row) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Extracts a FieldValue of $type from $row. |
||
| 455 | * |
||
| 456 | * @param array $row |
||
| 457 | * @param string $type |
||
| 458 | * |
||
| 459 | * @return \eZ\Publish\SPI\Persistence\Content\FieldValue |
||
| 460 | * |
||
| 461 | * @throws \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\Exception\NotFound |
||
| 462 | * if the necessary converter for $type could not be found. |
||
| 463 | */ |
||
| 464 | protected function extractFieldValueFromRow(array $row, $type) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Creates CreateStruct from $content. |
||
| 491 | * |
||
| 492 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
| 493 | * |
||
| 494 | * @return \eZ\Publish\SPI\Persistence\Content\CreateStruct |
||
| 495 | */ |
||
| 496 | public function createCreateStructFromContent(Content $content) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Extracts relation objects from $rows. |
||
| 521 | */ |
||
| 522 | public function extractRelationsFromRows(array $rows) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Extracts a Relation object from a $row. |
||
| 538 | * |
||
| 539 | * @param array $row Associative array representing a relation |
||
| 540 | * |
||
| 541 | * @return \eZ\Publish\SPI\Persistence\Content\Relation |
||
| 542 | */ |
||
| 543 | protected function extractRelationFromRow(array $row) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Creates a Content from the given $struct. |
||
| 562 | * |
||
| 563 | * @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $struct |
||
| 564 | * |
||
| 565 | * @return \eZ\Publish\SPI\Persistence\Content\Relation |
||
| 566 | */ |
||
| 567 | public function createRelationFromCreateStruct(RelationCreateStruct $struct) |
||
| 579 | } |
||
| 580 |
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..