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 ObjectStateGroupManager 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 ObjectStateGroupManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ObjectStateGroupManager extends RepositoryExecutor implements MigrationGeneratorInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected $supportedStepTypes = array('object_state_group'); |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var ObjectStateGroupMatcher |
||
| 19 | */ |
||
| 20 | protected $objectStateGroupMatcher; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param ObjectStateGroupMatcher $objectStateGroupMatcher |
||
| 24 | */ |
||
| 25 | public function __construct(ObjectStateGroupMatcher $objectStateGroupMatcher) |
||
| 26 | { |
||
| 27 | $this->objectStateGroupMatcher = $objectStateGroupMatcher; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Handle the create step of object state group migrations |
||
| 32 | * |
||
| 33 | * @todo add support for flexible defaultLanguageCode |
||
| 34 | */ |
||
| 35 | protected function create() |
||
| 36 | { |
||
| 37 | View Code Duplication | foreach (array('names', 'identifier') as $key) { |
|
|
|
|||
| 38 | if (!isset($this->dsl[$key])) { |
||
| 39 | throw new \Exception("The '$key' key is missing in a object state group creation definition"); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | $objectStateService = $this->repository->getObjectStateService(); |
||
| 44 | |||
| 45 | $objectStateGroupCreateStruct = $objectStateService->newObjectStateGroupCreateStruct($this->dsl['identifier']); |
||
| 46 | $objectStateGroupCreateStruct->defaultLanguageCode = self::DEFAULT_LANGUAGE_CODE; |
||
| 47 | |||
| 48 | foreach ($this->dsl['names'] as $languageCode => $name) { |
||
| 49 | $objectStateGroupCreateStruct->names[$languageCode] = $name; |
||
| 50 | } |
||
| 51 | if (isset($this->dsl['descriptions'])) { |
||
| 52 | foreach ($this->dsl['descriptions'] as $languageCode => $description) { |
||
| 53 | $objectStateGroupCreateStruct->descriptions[$languageCode] = $description; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | $objectStateGroup = $objectStateService->createObjectStateGroup($objectStateGroupCreateStruct); |
||
| 58 | |||
| 59 | $this->setReferences($objectStateGroup); |
||
| 60 | |||
| 61 | return $objectStateGroup; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Handle the update step of object state group migrations |
||
| 66 | * |
||
| 67 | * @todo add support for defaultLanguageCode |
||
| 68 | */ |
||
| 69 | View Code Duplication | protected function update() |
|
| 70 | { |
||
| 71 | $objectStateService = $this->repository->getObjectStateService(); |
||
| 72 | |||
| 73 | $groupsCollection = $this->matchObjectStateGroups('update'); |
||
| 74 | |||
| 75 | if (count($groupsCollection) > 1 && isset($this->dsl['references'])) { |
||
| 76 | throw new \Exception("Can not execute Object State Group update because multiple groups match, and a references section is specified in the dsl. References can be set when only 1 state group matches"); |
||
| 77 | } |
||
| 78 | |||
| 79 | if (count($groupsCollection) > 1 && isset($this->dsl['identifier'])) { |
||
| 80 | throw new \Exception("Can not execute Object State Group update because multiple groups match, and an identifier is specified in the dsl."); |
||
| 81 | } |
||
| 82 | |||
| 83 | foreach ($groupsCollection as $objectStateGroup) { |
||
| 84 | $objectStateGroupUpdateStruct = $objectStateService->newObjectStateGroupUpdateStruct(); |
||
| 85 | |||
| 86 | if (isset($this->dsl['identifier'])) { |
||
| 87 | $objectStateGroupUpdateStruct->identifier = $this->dsl['identifier']; |
||
| 88 | } |
||
| 89 | if (isset($this->dsl['names'])) { |
||
| 90 | foreach ($this->dsl['names'] as $languageCode => $name) { |
||
| 91 | $objectStateGroupUpdateStruct->names[$languageCode] = $name; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | if (isset($this->dsl['descriptions'])) { |
||
| 95 | foreach ($this->dsl['descriptions'] as $languageCode => $description) { |
||
| 96 | $objectStateGroupUpdateStruct->descriptions[$languageCode] = $description; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | $objectStateGroup = $objectStateService->updateObjectStateGroup($objectStateGroup, $objectStateGroupUpdateStruct); |
||
| 100 | |||
| 101 | $this->setReferences($objectStateGroup); |
||
| 102 | } |
||
| 103 | |||
| 104 | return $groupsCollection; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Handle the delete step of object state group migrations |
||
| 109 | */ |
||
| 110 | protected function delete() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $action |
||
| 125 | * @return ObjectStateGroupCollection |
||
| 126 | * @throws \Exception |
||
| 127 | */ |
||
| 128 | View Code Duplication | protected function matchObjectStateGroups($action) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 153 | */ |
||
| 154 | protected function setReferences($objectStateGroup) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param array $matchCondition |
||
| 182 | * @param string $mode |
||
| 183 | * @throws \Exception |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public function generateMigration(array $matchCondition, $mode) |
||
| 258 | } |
||
| 259 |
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.