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 LocationManager 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 LocationManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class LocationManager extends RepositoryExecutor |
||
| 11 | { |
||
| 12 | protected $supportedStepTypes = array('location'); |
||
| 13 | |||
| 14 | protected $contentMatcher; |
||
| 15 | |||
| 16 | public function __construct(ContentMatcher $contentMatcher) |
||
| 17 | { |
||
| 18 | $this->contentMatcher = $contentMatcher; |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Method to handle the create operation of the migration instructions |
||
| 23 | */ |
||
| 24 | protected function create() |
||
| 25 | { |
||
| 26 | $locationService = $this->repository->getLocationService(); |
||
| 27 | |||
| 28 | if (!isset($this->dsl['parent_location_id'])) { |
||
| 29 | throw new \Exception('Missing parent location id. This is required to create the new location.'); |
||
| 30 | } |
||
| 31 | if (!is_array($this->dsl['parent_location_id'])) { |
||
| 32 | $this->dsl['parent_location_id'] = array($this->dsl['parent_location_id']); |
||
| 33 | } |
||
| 34 | foreach ($this->dsl['parent_location_id'] as $id => $parentLocationId) { |
||
| 35 | if ($this->referenceResolver->isReference($parentLocationId)) { |
||
| 36 | $this->dsl['parent_location_id'][$id] = $this->referenceResolver->getReferenceValue($parentLocationId); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | $contentCollection = $this->matchContents('create'); |
||
| 41 | |||
| 42 | $locations = null; |
||
| 43 | foreach ($contentCollection as $content) { |
||
|
|
|||
| 44 | $contentInfo = $content->contentInfo; |
||
| 45 | |||
| 46 | foreach ($this->dsl['parent_location_id'] as $parentLocationId) { |
||
| 47 | $locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId); |
||
| 48 | |||
| 49 | $locationCreateStruct->hidden = isset($this->dsl['is_hidden']) ?: false; |
||
| 50 | |||
| 51 | if (isset($this->dsl['priority'])) { |
||
| 52 | $locationCreateStruct->priority = $this->dsl['priority']; |
||
| 53 | } |
||
| 54 | |||
| 55 | $locationCreateStruct->sortOrder = $this->getSortOrder(); |
||
| 56 | $locationCreateStruct->sortField = $this->getSortField(); |
||
| 57 | |||
| 58 | $locations[] = $locationService->createLocation($contentInfo, $locationCreateStruct); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->setReferences(new LocationCollection($locations)); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Updates basic information for a location like priority, sort field and sort order. |
||
| 67 | * Updates the visibility of the location when needed. |
||
| 68 | * Can move a location and its children to a new parent location or swap two locations. |
||
| 69 | * |
||
| 70 | * @todo add support for flexible matchers |
||
| 71 | */ |
||
| 72 | protected function update() |
||
| 73 | { |
||
| 74 | $locationService = $this->repository->getLocationService(); |
||
| 75 | |||
| 76 | if (!isset($this->dsl['location_id'])) { |
||
| 77 | throw new \Exception('No location set for update.'); |
||
| 78 | } |
||
| 79 | |||
| 80 | if (isset($this->dsl['swap_with_location']) && isset($this->dsl['patent_location_id'])) { |
||
| 81 | throw new \Exception('Cannot move location to a new parent and swap location with another location at the same time.'); |
||
| 82 | } |
||
| 83 | |||
| 84 | $locationId = $this->dsl['location_id']; |
||
| 85 | if ($this->referenceResolver->isReference($locationId)) { |
||
| 86 | $locationId = $this->referenceResolver->getReferenceValue($locationId); |
||
| 87 | } |
||
| 88 | |||
| 89 | $location = $locationService->loadLocation($locationId); |
||
| 90 | |||
| 91 | if (array_key_exists('priority', $this->dsl) |
||
| 92 | || array_key_exists('sort_field', $this->dsl) |
||
| 93 | || array_key_exists('sort_order', $this->dsl) |
||
| 94 | || array_key_exists('remote_id', $this->dsl) |
||
| 95 | ) { |
||
| 96 | $locationUpdateStruct = $locationService->newLocationUpdateStruct(); |
||
| 97 | |||
| 98 | if (isset($this->dsl['priority'])) { |
||
| 99 | $locationUpdateStruct->priority = $this->dsl['priority']; |
||
| 100 | } |
||
| 101 | |||
| 102 | if (isset($this->dsl['sort_field'])) { |
||
| 103 | $locationUpdateStruct->sortField = $this->getSortField($location->sortField); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (isset($this->dsl['sort_order'])) { |
||
| 107 | $locationUpdateStruct->sortOrder = $this->getSortOrder($location->sortOrder); |
||
| 108 | } |
||
| 109 | |||
| 110 | if (isset($this->dsl['remote_id'])) { |
||
| 111 | $locationUpdateStruct->remoteId = $this->dsl['remote_id']; |
||
| 112 | } |
||
| 113 | |||
| 114 | $locationService->updateLocation($location, $locationUpdateStruct); |
||
| 115 | } |
||
| 116 | |||
| 117 | // Check if visibility needs to be updated |
||
| 118 | if (isset($this->dsl['is_hidden'])) { |
||
| 119 | if ($this->dsl['is_hidden']) { |
||
| 120 | $locationService->hideLocation($location); |
||
| 121 | } else { |
||
| 122 | $locationService->unhideLocation($location); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | // Move or swap location |
||
| 127 | if (isset($this->dsl['parent_location_id'])) { |
||
| 128 | // Move the location and all it's children to a new parent |
||
| 129 | $parentLocationId = $this->dsl['parent_location_id']; |
||
| 130 | if ($this->referenceResolver->isReference($parentLocationId)) { |
||
| 131 | $parentLocationId = $this->referenceResolver->getReferenceValue($parentLocationId); |
||
| 132 | } |
||
| 133 | $newParentLocation = $locationService->loadLocation($parentLocationId); |
||
| 134 | $locationService->moveSubtree($location, $newParentLocation); |
||
| 135 | View Code Duplication | } elseif (isset($this->dsl['swap_with_location'])) { |
|
| 136 | //Swap locations |
||
| 137 | $swapLocationId = $this->dsl['swap_with_location']; |
||
| 138 | if ($this->referenceResolver->isReference($swapLocationId)) { |
||
| 139 | $swapLocationId = $this->referenceResolver->getReferenceValue($swapLocationId); |
||
| 140 | } |
||
| 141 | $locationToSwap = $locationService->loadLocation($swapLocationId); |
||
| 142 | |||
| 143 | $locationService->swapLocation($location, $locationToSwap); |
||
| 144 | } |
||
| 145 | |||
| 146 | $this->setReferences($location); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Delete locations identified by their ids. |
||
| 151 | * |
||
| 152 | * @todo add support for flexible matchers |
||
| 153 | */ |
||
| 154 | protected function delete() |
||
| 155 | { |
||
| 156 | $locationService = $this->repository->getLocationService(); |
||
| 157 | |||
| 158 | if (!isset($this->dsl['location_id'])) { |
||
| 159 | throw new \Exception('No location provided for deletion'); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!is_array($this->dsl['location_id'])) { |
||
| 163 | $this->dsl['location_id'] = array($this->dsl['location_id']); |
||
| 164 | } |
||
| 165 | |||
| 166 | foreach ($this->dsl['location_id'] as $locationId) { |
||
| 167 | $location = $locationService->loadLocation($locationId); |
||
| 168 | $locationService->deleteLocation($location); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * NB: weirdly enough, it returns contents, not locations |
||
| 174 | * |
||
| 175 | * @param string $action |
||
| 176 | * @return ContentCollection |
||
| 177 | * @throws \Exception |
||
| 178 | */ |
||
| 179 | View Code Duplication | protected function matchContents($action) |
|
| 180 | { |
||
| 181 | if (!isset($this->dsl['object_id']) && !isset($this->dsl['remote_id']) && !isset($this->dsl['match'])) { |
||
| 182 | throw new \Exception("The ID or remote ID of an object or a Match Condition is required to $action a new location."); |
||
| 183 | } |
||
| 184 | |||
| 185 | // Backwards compat |
||
| 186 | if (!isset($this->dsl['match'])) { |
||
| 187 | if (isset($this->dsl['object_id'])) { |
||
| 188 | $this->dsl['match'] = array('content_id' => $this->dsl['object_id']); |
||
| 189 | } elseif (isset($this->dsl['remote_id'])) { |
||
| 190 | $this->dsl['match'] = array('content_remote_id' => $this->dsl['remote_id']); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | $match = $this->dsl['match']; |
||
| 195 | |||
| 196 | // convert the references passed in the match |
||
| 197 | foreach ($match as $condition => $values) { |
||
| 198 | if (is_array($values)) { |
||
| 199 | foreach ($values as $position => $value) { |
||
| 200 | if ($this->referenceResolver->isReference($value)) { |
||
| 201 | $match[$condition][$position] = $this->referenceResolver->getReferenceValue($value); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } else { |
||
| 205 | if ($this->referenceResolver->isReference($values)) { |
||
| 206 | $match[$condition] = $this->referenceResolver->getReferenceValue($values); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | return $this->contentMatcher->matchContent($match); |
||
| 212 | } |
||
| 213 | |||
| 214 | protected function getSortField($currentValue = null) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get the sort order based on the current value and the value in the DSL definition. |
||
| 235 | * |
||
| 236 | * If no current value is set and there is no value in the DSL it will default to Location::SORT_ORDER_ASC |
||
| 237 | * |
||
| 238 | * @see \eZ\Publish\API\Repository\Values\Content\Location::SORT_ORDER_* |
||
| 239 | * |
||
| 240 | * @param int $currentValue |
||
| 241 | * @return int |
||
| 242 | */ |
||
| 243 | protected function getSortOrder($currentValue = null) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Sets references to object attributes |
||
| 263 | * |
||
| 264 | * The Location Manager currently supports setting references to location id. |
||
| 265 | * |
||
| 266 | * @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute. |
||
| 267 | * @param \eZ\Publish\API\Repository\Values\Content\Location|LocationCollection $location |
||
| 268 | * @return boolean |
||
| 269 | */ |
||
| 270 | protected function setReferences($location) |
||
| 298 | } |
||
| 299 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.