Complex classes like DomainMapper 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 DomainMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 45 | class DomainMapper  | 
            ||
| 46 | { | 
            ||
| 47 | const ROOT_LOCATION_ID = 1;  | 
            ||
| 48 | |||
| 49 | const MAX_LOCATION_PRIORITY = 2147483647;  | 
            ||
| 50 | const MIN_LOCATION_PRIORITY = -2147483648;  | 
            ||
| 51 | |||
| 52 | /**  | 
            ||
| 53 | * @var \eZ\Publish\SPI\Persistence\Content\Handler  | 
            ||
| 54 | */  | 
            ||
| 55 | protected $contentHandler;  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * @var \eZ\Publish\SPI\Persistence\Content\Location\Handler  | 
            ||
| 59 | */  | 
            ||
| 60 | protected $locationHandler;  | 
            ||
| 61 | |||
| 62 | /**  | 
            ||
| 63 | * @var \eZ\Publish\SPI\Persistence\Content\Type\Handler  | 
            ||
| 64 | */  | 
            ||
| 65 | protected $contentTypeHandler;  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler  | 
            ||
| 69 | */  | 
            ||
| 70 | protected $contentLanguageHandler;  | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * @var FieldTypeRegistry  | 
            ||
| 74 | */  | 
            ||
| 75 | protected $fieldTypeRegistry;  | 
            ||
| 76 | |||
| 77 | /**  | 
            ||
| 78 | * Setups service with reference to repository.  | 
            ||
| 79 | *  | 
            ||
| 80 | * @param \eZ\Publish\SPI\Persistence\Content\Handler $contentHandler  | 
            ||
| 81 | * @param \eZ\Publish\SPI\Persistence\Content\Location\Handler $locationHandler  | 
            ||
| 82 | * @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler  | 
            ||
| 83 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $contentLanguageHandler  | 
            ||
| 84 | * @param FieldTypeRegistry $fieldTypeRegistry  | 
            ||
| 85 | */  | 
            ||
| 86 | public function __construct(  | 
            ||
| 87 | ContentHandler $contentHandler,  | 
            ||
| 88 | LocationHandler $locationHandler,  | 
            ||
| 89 | TypeHandler $contentTypeHandler,  | 
            ||
| 90 | LanguageHandler $contentLanguageHandler,  | 
            ||
| 91 | FieldTypeRegistry $fieldTypeRegistry  | 
            ||
| 92 |     ) { | 
            ||
| 93 | $this->contentHandler = $contentHandler;  | 
            ||
| 94 | $this->locationHandler = $locationHandler;  | 
            ||
| 95 | $this->contentTypeHandler = $contentTypeHandler;  | 
            ||
| 96 | $this->contentLanguageHandler = $contentLanguageHandler;  | 
            ||
| 97 | $this->fieldTypeRegistry = $fieldTypeRegistry;  | 
            ||
| 98 | }  | 
            ||
| 99 | |||
| 100 | /**  | 
            ||
| 101 | * Builds a Content domain object from value object returned from persistence.  | 
            ||
| 102 | *  | 
            ||
| 103 | * @param \eZ\Publish\SPI\Persistence\Content $spiContent  | 
            ||
| 104 | * @param ContentType|SPIType $contentType  | 
            ||
| 105 | * @param array $prioritizedLanguages Prioritized language codes to filter fields on  | 
            ||
| 106 | * @param string|null $fieldAlwaysAvailableLanguage Language code fallback if a given field is not found in $prioritizedLanguages  | 
            ||
| 107 | *  | 
            ||
| 108 | * @return \eZ\Publish\Core\Repository\Values\Content\Content  | 
            ||
| 109 | */  | 
            ||
| 110 | public function buildContentDomainObject(  | 
            ||
| 111 | SPIContent $spiContent,  | 
            ||
| 112 | $contentType = null,  | 
            ||
| 113 | array $prioritizedLanguages = [],  | 
            ||
| 114 | string $fieldAlwaysAvailableLanguage = null  | 
            ||
| 115 |     ) { | 
            ||
| 116 |         if ($contentType === null) { | 
            ||
| 117 | $contentType = $this->contentTypeHandler->load(  | 
            ||
| 118 | $spiContent->versionInfo->contentInfo->contentTypeId  | 
            ||
| 119 | );  | 
            ||
| 120 | }  | 
            ||
| 121 | |||
| 122 | $prioritizedFieldLanguageCode = null;  | 
            ||
| 123 |         if (!empty($prioritizedLanguages)) { | 
            ||
| 124 | $availableFieldLanguageMap = array_fill_keys($spiContent->versionInfo->languageCodes, true);  | 
            ||
| 125 |             foreach ($prioritizedLanguages as $prioritizedLanguage) { | 
            ||
| 126 |                 if (isset($availableFieldLanguageMap[$prioritizedLanguage])) { | 
            ||
| 127 | $prioritizedFieldLanguageCode = $prioritizedLanguage;  | 
            ||
| 128 | break;  | 
            ||
| 129 | }  | 
            ||
| 130 | }  | 
            ||
| 131 | }  | 
            ||
| 132 | |||
| 133 | return new Content(  | 
            ||
| 134 | array(  | 
            ||
| 135 | 'internalFields' => $this->buildDomainFields($spiContent->fields, $contentType, $prioritizedLanguages, $fieldAlwaysAvailableLanguage),  | 
            ||
| 136 | 'versionInfo' => $this->buildVersionInfoDomainObject($spiContent->versionInfo, $prioritizedLanguages),  | 
            ||
| 137 | 'prioritizedFieldLanguageCode' => $prioritizedFieldLanguageCode,  | 
            ||
| 138 | )  | 
            ||
| 139 | );  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 | /**  | 
            ||
| 143 | * Builds a Content proxy object (lazy loaded, loads as soon as used).  | 
            ||
| 144 | */  | 
            ||
| 145 | public function buildContentProxy(  | 
            ||
| 154 | |||
| 155 | /**  | 
            ||
| 156 | * Builds a list of Content proxy objects (lazy loaded, loads all as soon as one of them loads).  | 
            ||
| 157 | *  | 
            ||
| 158 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo[] $infoList  | 
            ||
| 159 | * @param string[] $prioritizedLanguages  | 
            ||
| 160 | * @param bool $useAlwaysAvailable  | 
            ||
| 161 | *  | 
            ||
| 162 | * @return \eZ\Publish\API\Repository\Values\Content\Content[<int>]  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 163 | */  | 
            ||
| 164 | public function buildContentProxyList(  | 
            ||
| 177 | |||
| 178 | /**  | 
            ||
| 179 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo[] $infoList  | 
            ||
| 180 | * @param string[] $prioritizedLanguages  | 
            ||
| 181 | * @param bool $useAlwaysAvailable  | 
            ||
| 182 | *  | 
            ||
| 183 | * @return \Generator  | 
            ||
| 184 | */  | 
            ||
| 185 | private function generatorForContentList(  | 
            ||
| 217 | |||
| 218 | /**  | 
            ||
| 219 | * Returns an array of domain fields created from given array of SPI fields.  | 
            ||
| 220 | *  | 
            ||
| 221 | * @throws InvalidArgumentType On invalid $contentType  | 
            ||
| 222 | *  | 
            ||
| 223 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $spiFields  | 
            ||
| 224 | * @param ContentType|SPIType $contentType  | 
            ||
| 225 | * @param array $prioritizedLanguages A language priority, filters returned fields and is used as prioritized language code on  | 
            ||
| 226 | * returned value object. If not given all languages are returned.  | 
            ||
| 227 | * @param string|null $alwaysAvailableLanguage Language code fallback if a given field is not found in $prioritizedLanguages  | 
            ||
| 228 | *  | 
            ||
| 229 | * @return array  | 
            ||
| 230 | */  | 
            ||
| 231 | public function buildDomainFields(  | 
            ||
| 296 | |||
| 297 | /**  | 
            ||
| 298 | * Builds a VersionInfo domain object from value object returned from persistence.  | 
            ||
| 299 | *  | 
            ||
| 300 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $spiVersionInfo  | 
            ||
| 301 | * @param array $prioritizedLanguages  | 
            ||
| 302 | *  | 
            ||
| 303 | * @return \eZ\Publish\Core\Repository\Values\Content\VersionInfo  | 
            ||
| 304 | */  | 
            ||
| 305 | public function buildVersionInfoDomainObject(SPIVersionInfo $spiVersionInfo, array $prioritizedLanguages = [])  | 
            ||
| 347 | |||
| 348 | /**  | 
            ||
| 349 | * Builds a ContentInfo domain object from value object returned from persistence.  | 
            ||
| 350 | *  | 
            ||
| 351 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo $spiContentInfo  | 
            ||
| 352 | *  | 
            ||
| 353 | * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo  | 
            ||
| 354 | */  | 
            ||
| 355 | public function buildContentInfoDomainObject(SPIContentInfo $spiContentInfo)  | 
            ||
| 395 | |||
| 396 | /**  | 
            ||
| 397 | * Builds API Relation object from provided SPI Relation object.  | 
            ||
| 398 | *  | 
            ||
| 399 | * @param \eZ\Publish\SPI\Persistence\Content\Relation $spiRelation  | 
            ||
| 400 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $sourceContentInfo  | 
            ||
| 401 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $destinationContentInfo  | 
            ||
| 402 | *  | 
            ||
| 403 | * @return \eZ\Publish\API\Repository\Values\Content\Relation  | 
            ||
| 404 | */  | 
            ||
| 405 | public function buildRelationDomainObject(  | 
            ||
| 433 | |||
| 434 | /**  | 
            ||
| 435 | * @deprecated Since 7.2, use buildLocationWithContent(), buildLocation() or (private) mapLocation() instead.  | 
            ||
| 436 | */  | 
            ||
| 437 | public function buildLocationDomainObject(  | 
            ||
| 451 | |||
| 452 | public function buildLocation(  | 
            ||
| 469 | |||
| 470 | /**  | 
            ||
| 471 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation  | 
            ||
| 472 | * @param \eZ\Publish\API\Repository\Values\Content\Content|null $content  | 
            ||
| 473 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo|null $spiContentInfo  | 
            ||
| 474 | *  | 
            ||
| 475 | * @return \eZ\Publish\API\Repository\Values\Content\Location  | 
            ||
| 476 | *  | 
            ||
| 477 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException  | 
            ||
| 478 | */  | 
            ||
| 479 | public function buildLocationWithContent(  | 
            ||
| 500 | |||
| 501 | /**  | 
            ||
| 502 | * Builds API Location object for tree root.  | 
            ||
| 503 | *  | 
            ||
| 504 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation  | 
            ||
| 505 | *  | 
            ||
| 506 | * @return \eZ\Publish\API\Repository\Values\Content\Location  | 
            ||
| 507 | */  | 
            ||
| 508 | private function buildRootLocation(SPILocation $spiLocation): APILocation  | 
            ||
| 534 | |||
| 535 | private function mapLocation(SPILocation $spiLocation, ContentInfo $contentInfo, APIContent $content): APILocation  | 
            ||
| 554 | |||
| 555 | /**  | 
            ||
| 556 | * Build API Content domain objects in bulk and apply to ContentSearchResult.  | 
            ||
| 557 | *  | 
            ||
| 558 | * Loading of Content objects are done in bulk.  | 
            ||
| 559 | *  | 
            ||
| 560 | * @param \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $result SPI search result with SPI ContentInfo items as hits  | 
            ||
| 561 | * @param array $languageFilter  | 
            ||
| 562 | *  | 
            ||
| 563 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo[] ContentInfo we did not find content for is returned.  | 
            ||
| 564 | */  | 
            ||
| 565 | public function buildContentDomainObjectsOnSearchResult(SearchResult $result, array $languageFilter)  | 
            ||
| 602 | |||
| 603 | /**  | 
            ||
| 604 | * Build API Location and corresponding ContentInfo domain objects and apply to LocationSearchResult.  | 
            ||
| 605 | *  | 
            ||
| 606 | * This is done in order to be able to:  | 
            ||
| 607 | * Load ContentInfo objects in bulk, generate proxy objects for Content that will loaded in bulk on-demand (on use).  | 
            ||
| 608 | *  | 
            ||
| 609 | * @param \eZ\Publish\API\Repository\Values\Content\Search\SearchResult $result SPI search result with SPI Location items as hits  | 
            ||
| 610 | * @param array $languageFilter  | 
            ||
| 611 | *  | 
            ||
| 612 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] Locations we did not find content info for is returned.  | 
            ||
| 613 | */  | 
            ||
| 614 | public function buildLocationDomainObjectsOnSearchResult(SearchResult $result, array $languageFilter)  | 
            ||
| 647 | |||
| 648 | /**  | 
            ||
| 649 | * Creates an array of SPI location create structs from given array of API location create structs.  | 
            ||
| 650 | *  | 
            ||
| 651 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException  | 
            ||
| 652 | *  | 
            ||
| 653 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct  | 
            ||
| 654 | * @param \eZ\Publish\API\Repository\Values\Content\Location $parentLocation  | 
            ||
| 655 | * @param mixed $mainLocation  | 
            ||
| 656 | * @param mixed $contentId  | 
            ||
| 657 | * @param mixed $contentVersionNo  | 
            ||
| 658 | *  | 
            ||
| 659 | * @return \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct  | 
            ||
| 660 | */  | 
            ||
| 661 | public function buildSPILocationCreateStruct(  | 
            ||
| 724 | |||
| 725 | /**  | 
            ||
| 726 | * Checks if given $sortField value is one of the defined sort field constants.  | 
            ||
| 727 | *  | 
            ||
| 728 | * @param mixed $sortField  | 
            ||
| 729 | *  | 
            ||
| 730 | * @return bool  | 
            ||
| 731 | */  | 
            ||
| 732 | public function isValidLocationSortField($sortField)  | 
            ||
| 752 | |||
| 753 | /**  | 
            ||
| 754 | * Checks if given $sortOrder value is one of the defined sort order constants.  | 
            ||
| 755 | *  | 
            ||
| 756 | * @param mixed $sortOrder  | 
            ||
| 757 | *  | 
            ||
| 758 | * @return bool  | 
            ||
| 759 | */  | 
            ||
| 760 | public function isValidLocationSortOrder($sortOrder)  | 
            ||
| 770 | |||
| 771 | /**  | 
            ||
| 772 | * Checks if given $priority is valid.  | 
            ||
| 773 | *  | 
            ||
| 774 | * @param int $priority  | 
            ||
| 775 | *  | 
            ||
| 776 | * @return bool  | 
            ||
| 777 | */  | 
            ||
| 778 | public function isValidLocationPriority($priority)  | 
            ||
| 786 | |||
| 787 | /**  | 
            ||
| 788 | * Validates given translated list $list, which should be an array of strings with language codes as keys.  | 
            ||
| 789 | *  | 
            ||
| 790 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException  | 
            ||
| 791 | *  | 
            ||
| 792 | * @param mixed $list  | 
            ||
| 793 | * @param string $argumentName  | 
            ||
| 794 | */  | 
            ||
| 795 | public function validateTranslatedList($list, $argumentName)  | 
            ||
| 809 | |||
| 810 | /**  | 
            ||
| 811 | * Returns \DateTime object from given $timestamp in environment timezone.  | 
            ||
| 812 | *  | 
            ||
| 813 | * This method is needed because constructing \DateTime with $timestamp will  | 
            ||
| 814 | * return the object in UTC timezone.  | 
            ||
| 815 | *  | 
            ||
| 816 | * @param int $timestamp  | 
            ||
| 817 | *  | 
            ||
| 818 | * @return \DateTime  | 
            ||
| 819 | */  | 
            ||
| 820 | public function getDateTime($timestamp)  | 
            ||
| 827 | |||
| 828 | /**  | 
            ||
| 829 | * Creates unique hash string for given $object.  | 
            ||
| 830 | *  | 
            ||
| 831 | * Used for remoteId.  | 
            ||
| 832 | *  | 
            ||
| 833 | * @param object $object  | 
            ||
| 834 | *  | 
            ||
| 835 | * @return string  | 
            ||
| 836 | */  | 
            ||
| 837 | public function getUniqueHash($object)  | 
            ||
| 841 | |||
| 842 | /**  | 
            ||
| 843 | * Returns true if given location is a tree root.  | 
            ||
| 844 | *  | 
            ||
| 845 | * @param \eZ\Publish\SPI\Persistence\Content\Location $spiLocation  | 
            ||
| 846 | *  | 
            ||
| 847 | * @return bool  | 
            ||
| 848 | */  | 
            ||
| 849 | private function isRootLocation(SPILocation $spiLocation): bool  | 
            ||
| 853 | }  | 
            ||
| 854 | 
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.